Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
673 views
in Technique[技术] by (71.8m points)

c# - Determining when a WPF window is being moved

I'm working on a class derived from the WPF Window class that behaves as an application toolbar window called AppBarWindow. I have been able to find various WinForms implementations, but no WPF implementations.

I've got a lot of the code working, but I need to know when the user starts dragging the window around the screen and when they stop, as the window's behavior will differ. The default WPF handling isn't quite right, so I've implemented my own Window Procedure and installed it using an HwndSource object.

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

But I'm working now on a general class that won't use the DragMove method. I can add another LeftMouseButtonDown handler for the window, but I don't believe that will get called if the mouse is in the non-client area.

How am I going to detect that the user is dragging the window and when they've stopped in this case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've found what I needed to know by monitoring the messages sent to my window from Win32 while dragging it.

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

WM_ENTERSIZEMOVE

Next, Windows sends the following messages to my window procedure in sequence:

  • WM_MOVING
  • WM_WINDOWPOSCHANGING
  • WM_GETMINMAXINFO
  • WM_WINDOWPOSCHANGED
  • WM_MOVE

These are followed with a message whose code is 0xc310. This is not documented anywhere, so I'm guessing this is used internally by .NET / WPF.

These 6 messages are sent repeatedly as the mouse moves and the window follows after it.

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

  • WM_EXITSIZEMOVE

So I need to listen for the WM_ENTERSIZEMOVE and WM_EXITSIZEMOVE messages.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...