Determining when a WPF window is moving

I am working on a class derived from the WPF Window class, which behaves like an application toolbar window called AppBarWindow . I was able to find various implementations of WinForms, but not WPF implementations.

I have a lot of work with the code, but I need to know when the user starts to drag the window around the screen and when it stops, as the behavior of the window will be different. WPF handling by default is not entirely correct, so I applied my own Window procedure and set it using the HwndSource object.

It works for me in the application at work, which does not have a non-client area. In this case, there is a LeftMouseButtonDown event handler that sets the flag to true, then calls the DragMove method, which drags the window around. When this method returns, I set the flag to false. Everything works.

But now I'm working on a generic class that will not use the DragMove method. I can add another LeftMouseButtonDown handler for the window, but I don’t think it will be called if the mouse is in a non-client area.

How do I know if a user is dragging a window and when did it stop in this case?

+4
source share
3 answers

I found what I need to know by tracking messages sent to my window from Win32 by dragging and dropping it.

In short, Windows sends the following message when the window starts to move:

WM_ENTERSIZEMOVE

Then, Windows sequentially sends the following messages to my window procedure:

  • WM_MOVING
  • WM_WINDOWPOSCHANGING
  • WM_GETMINMAXINFO
  • WM_WINDOWPOSCHANGED
  • WM_MOVE

They are followed by a message with code 0xc310. This is not documented anywhere, so I assume it is used internally by .NET / WPF.

These 6 messages are resent when the mouse moves and the window follows it.

Finally, when you release the left mouse button, Windows sends:

  • WM_EXITSIZEMOVE

Therefore, I need to listen to the WM_ENTERSIZEMOVE and WM_EXITSIZEMOVE messages.

+5
source

The wont Window.LocationChanged event will help you in this case.

http://msdn.microsoft.com/en-us/library/system.windows.window.locationchanged.aspx

+1
source

As Tony points out, there are several window messages in dragging windows. Here's an listing that might help:

 internal enum WindowsMessage { /// <summary>Sent after a window has been moved.</summary> WM_MOVE = 0x0003, /// <summary> /// Sent to a window when the size or position of the window is about to change. /// An application can use this message to override the window default maximized size and position, /// or its default minimum or maximum tracking size. /// </summary> WM_GETMINMAXINFO = 0x0024, /// <summary> /// Sent to a window whose size, position, or place in the Z order is about to change as a result /// of a call to the SetWindowPos function or another window-management function. /// </summary> WM_WINDOWPOSCHANGING = 0x0046, /// <summary> /// Sent to a window whose size, position, or place in the Z order has changed as a result of a /// call to the SetWindowPos function or another window-management function. /// </summary> WM_WINDOWPOSCHANGED = 0x0047, /// <summary> /// Sent to a window that the user is moving. By processing this message, an application can monitor /// the position of the drag rectangle and, if needed, change its position. /// </summary> WM_MOVING = 0x0216, /// <summary> /// Sent once to a window after it enters the moving or sizing modal loop. The window enters the /// moving or sizing modal loop when the user clicks the window title bar or sizing border, or /// when the window passes the WM_SYSCOMMAND message to the DefWindowProc function and the wParam /// parameter of the message specifies the SC_MOVE or SC_SIZE value. The operation is complete /// when DefWindowProc returns. /// <para /> /// The system sends the WM_ENTERSIZEMOVE message regardless of whether the dragging of full windows /// is enabled. /// </summary> WM_ENTERSIZEMOVE = 0x0231, /// <summary> /// Sent once to a window once it has exited moving or sizing modal loop. The window enters the /// moving or sizing modal loop when the user clicks the window title bar or sizing border, or /// when the window passes the WM_SYSCOMMAND message to the DefWindowProc function and the /// wParam parameter of the message specifies the SC_MOVE or SC_SIZE value. The operation is /// complete when DefWindowProc returns. /// </summary> WM_EXITSIZEMOVE = 0x0232 } 
+1
source

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


All Articles