Simultaneous movement of 2 forms

I'm a little stuck here. I am trying to move 2 forms at the same time without using OnMove, LocationChanged, Docking etc.

The only way to interact with their locations is to override WndProc. Something that might be useful is that form A is the owner of form B. Therefore, whenever A moves, I also want to move B. Not in the same place, but at the same distance.

protected override void WndProc(ref Message m) { if (m.Msg == 0x0084) { Form[] temp = this.OwnedForms; if(temp.Length > 0) { /* moving temp[0] to the same ratio as this form */ } m.Result = (IntPtr)2; return; } base.WndProc(ref m); } 

Both A and B have the same WndProc, as they are 2 objects from the same class.

+4
source share
2 answers

I managed to solve the problem:

 protected override void WndProc(ref Message m) { Form temp = this.Owner; if (m.Msg == 0x0084) { m.Result = (IntPtr)2; return; } if (m.Msg == 0x0216 && temp != null) { if (!movedonce) { oldlocationx = this.Location.X; oldlocationy = this.Location.Y; movedonce = true; } temp.Location = new Point(temp.Location.X + this.Location.X - oldlocationx, temp.Location.Y + this.Location.Y - oldlocationy); oldlocationx = this.Location.X; oldlocationy = this.Location.Y; } base.WndProc(ref m); } 
-1
source

It makes no sense to avoid using the LocationChanged event:

  private Point lastPos; protected override void OnLoad(EventArgs e) { base.OnLoad(e); lastPos = this.Location; } protected override void OnLocationChanged(EventArgs e) { base.OnLocationChanged(e); foreach (var frm in this.OwnedForms) { frm.Location = new Point(frm.Location.X + this.Left - lastPos.X, frm.Location.Y + this.Top - lastPos.Y); } lastPos = this.Location; } protected override void WndProc(ref Message m) { // Move borderless window with click-and-drag on client window if (m.Msg == 0x84) m.Result = (IntPtr)2; else base.WndProc(ref m); } 
+3
source

Source: https://habr.com/ru/post/1381930/


All Articles