简体   繁体   中英

Window.DragMove() causes the window to freeze temporarily if holding down the mouse left button without moving the cursor

I have a "borderless" window in WPF. It can be dragged from any part of the window that does not handle the Click event, using this code:

// Drag the window:
private void GlassWindow_MouseDown(object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton != MouseButton.Left) return;
    if (AllowDrag) DragMove();
}

(Note: AllowDrag is always set to true in this case)

This code works fine, except that when I click on the window and hold down the left mouse button, without moving the cursor (= not dragging the window) , the window freezes for about 2-3 seconds (ie all animations pause, progressbar stops moving). This behaviour is consistent, and does not happen when I click on a button or when I drag the window, only when I hold left click. Is there any solution for this or is this intended windows behavior?

EDIT: Things that don't solve the problem:

https://stackoverflow.com/a/3275712/2719183

https://stackoverflow.com/a/5494769/2719183

http://www.codeproject.com/Articles/11114

   if (AllowDrag) DragMove();

DragMove() is the trouble-maker, it is uses a pretty hacky way to implement the move. And that causes the problem you describe, the WPF team is well-aware of the issue but chose to not fix it. You can read about it in this connect article . Vote if you are not pleased.

So you need to avoid DragMove() . The best way is to do it the way it is normally done, you minimize the risk of reproducing the exact same trouble that way. That requires knowing a little about the way the winapi works. Whenever a window is clicked, Windows sends the WM_NCHITTEST message to ask your app what part of the window was clicked. When you return HTCAPTION, even if you don't have a caption, then Windows takes your word for it and implements what normally happens when you click and drag a window by its caption.

That has been done, you don't have to be an expert in the winapi to get that going. Google "wpf wm_nchittest" to find code. The top hit is an existing SO question , Tergiver's code looks good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM