Well there are a few areas you need to address. First to get notifications that the edge is coming close to the screen:
- Get notifications that window's size is changing. This one is easy - just use
Window.SizeChanged
event.
- Get notifications that window position is changing. This one is a bit tricky and I am not sure how to achieve it, might need to P/Invoke into Win32 API.
Then, there is a list of TODOs to work out if the window edge is close to the screen edge.
Whether or not there are multiple monitors and if the window is solely contained within on monitor. This answer will help you get the monitor information.
Handle the action of snapping the edge. Will need a bit of rect arithmetic acrobatics for this one. Then you either set Window.Top
, Window.Left
, Window.Height
or Window.Width
.
You will need conditional code for each edge but it will look something like this:
void SnapWindow(Window window, Size monitorSize) {
if (window.Left < c_SnapThreshold && window.Left > 0)
window.Left = 0;
if (window.Left + window.Width > (monitorSize.Width - SnapThreshold) && window.Left + window.Width < monitorSize.Width)
window.Width = monitorSize.Width - window.Left; //docks the right edge
//..etc
}
}