2

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 (i.e. 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

Community
  • 1
  • 1
OronDF343
  • 528
  • 1
  • 3
  • 14

1 Answers1

2
   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.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536