How to implement in WPF window sticking to the edge of the screen? Here is the code found for WinForms. PS For those who do not understand the assignment: sticking to the edge is when you move the form to the edge, and it at some distance is magnetized to the edge. ScreenSnap in VCL is called.

protected override void WndProc ( ref Message m ) { if (m.Msg == 0x0046 /* WM_WINDOWPOSCHANGING */) { Rectangle workArea = SystemInformation.WorkingArea; Rectangle rect = (Rectangle)Marshal.PtrToStructure((IntPtr)(IntPtr.Size * 2 + m.LParam.ToInt64()), typeof(Rectangle)); if (rect.X <= workArea.Left + DISTANCE) Marshal.WriteInt32(m.LParam, IntPtr.Size * 2, workArea.Left); if (rect.X + rect.Width >= workArea.Width - DISTANCE) Marshal.WriteInt32(m.LParam, IntPtr.Size * 2, workArea.Right - rect.Width); if (rect.Y <= workArea.Top + DISTANCE) Marshal.WriteInt32(m.LParam, IntPtr.Size * 2 + 4, workArea.Top); if (rect.Y + rect.Height >= workArea.Height - DISTANCE) Marshal.WriteInt32(m.LParam, IntPtr.Size * 2 + 4, workArea.Bottom - rect.Height); } base.WndProc(ref m); } 
  • What does "sticking the window to the edge of the screen" mean? - Align
  • Describe the problem in more detail! - Yurii Manziuk
  • I see another problem here: this code takes WorkingArea, which is normal for a system with a single screen, but bad for several screens, when in reality we have several rectangles connected to each other. - nzeemin

1 answer 1

Here is a link on a foreign stack. The essence is the same as in winforms. Events just come from the xaml window.