Screen binding window in WPF

I decided to teach myself C # by writing a music player in Visual Studio 2010. I went with WPF because from what I hear, it looks like it will be a good base for skins.

I want to program my window with a behavior that, if the window approaches the edge of the screen (within 10 pixels or so), will snap to the edge of the screen. What is the best way to do this?

+3
source share
1 answer

Well, there are a few areas that you need to address. First, get notifications that the edge is approaching the screen:

  • Receive notifications that the window size is changing. It's easy - just use the Window.SizeChangedevent.
  • , . , , , , P/Invoke Win32 API.

TODO , .

, :

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
}

}

+3

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


All Articles