[英]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. 我在WPF中有一个“无边界”窗口。 It can be dragged from any part of the window that does not handle the Click event, using this code:
可以使用以下代码将其从不处理Click事件的窗口的任何部分拖动:
// 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) (注意:在这种情况下,AllowDrag始终设置为true)
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). 这段代码可以正常工作,除了当我单击窗口并按住鼠标左键而不移动光标(=不拖动窗口)时 ,窗口冻结约2-3秒(即所有动画暂停,进度条停止)移动)。 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?
有没有解决方案,或者这是预期的Windows行为?
EDIT: Things that don't solve the problem: 编辑:不能解决问题的事情:
https://stackoverflow.com/a/3275712/2719183 https://stackoverflow.com/a/3275712/2719183
https://stackoverflow.com/a/5494769/2719183 https://stackoverflow.com/a/5494769/2719183
http://www.codeproject.com/Articles/11114 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. DragMove()
是麻烦制造者,它使用一种非常怪异的方式来实现移动。 And that causes the problem you describe, the WPF team is well-aware of the issue but chose to not fix it. 这就是导致您描述的问题的原因,WPF团队非常了解此问题,但选择不解决此问题。 You can read about it in this connect article .
您可以在此连接文章中阅读有关它的内容 。 Vote if you are not pleased.
如果您不满意,请投票。
So you need to avoid DragMove()
. 因此,您需要避免
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.
这需要对winapi的工作方式有所了解。 Whenever a window is clicked, Windows sends the
WM_NCHITTEST
message to ask your app what part of the window was clicked. 每当单击窗口时,Windows都会发送
WM_NCHITTEST
消息,询问您的应用程序单击了窗口的哪个部分。 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. 当您返回HTCAPTION时,即使您没有标题,Windows也照此行事,并实现您单击并拖动其标题的窗口时通常发生的情况。
That has been done, you don't have to be an expert in the winapi to get that going. 这样做已经完成,您不必成为winapi的专家就可以做到这一点。 Google "wpf wm_nchittest" to find code.
在谷歌“ wpf wm_nchittest”中找到代码。 The top hit is an existing SO question , Tergiver's code looks good.
最热门的是现有的SO问题 ,Tergiver的代码看起来不错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.