[英]Draggable WPF window with no border
I'm using WindowStyle=None
to remove the border of a WPF window. 我正在使用
WindowStyle=None
来删除WPF窗口的边框。 In the MainWindow.xaml.cs file I just added the following line to the constructor: 在MainWindow.xaml.cs文件中,我刚刚将以下行添加到构造函数中:
this.MouseLeftButtonDown += delegate { this.DragMove(); };
This somewhat works and lets me drag the window around wherever I left click inside the MainWindow, as long as it's not on any control. 这有点工作,让我可以将窗口拖动到我在MainWindow内部左键单击的位置,只要它不在任何控件上。 Because that's where I get problems.
因为那是我遇到问题的地方。 I have a textbox that takes up all the space inside the window, and as soon as I do this I can no longer move the window around when left clicking inside the textbox.
我有一个占据窗口内所有空间的文本框,一旦我这样做,我就不能再在文本框中左键单击时移动窗口了。
How can I make the window move around if the user left clicks inside the window and drags the mouse no matter what control the user is hitting? 如果用户在窗口内部发出咔嗒声并拖动鼠标,无论用户使用何种控制方式,如何让窗口四处移动?
Or simpler, how can I make the window move when the user left clicks and drags inside the textbox control? 或者更简单,当用户在文本框控件内点击并拖动时,如何让窗口移动?
Use the tunneled MouseDown event, ie, the PreviewMouseLeftButtonDown
event of the Window. 使用隧道MouseDown事件,即Window的
PreviewMouseLeftButtonDown
事件。 This will ensure that the event occurs both on the Window and its child controls: 这将确保事件在Window及其子控件上发生:
this.PreviewMouseLeftButtonDown += (s, e) => DragMove();
You can also add an event to the TextBox manually: 您还可以手动将事件添加到TextBox:
textBox.MouseDown += (s, e) => DragMove();
But : 但是 :
Doing what you want has its inherent problems. 做你想做的事有其固有的问题。 It will not let you select text in the TextBox.
它不允许您在TextBox中选择文本。 There is a workaround - use a
Key + MouseDrag
input like this: 有一个解决方法 - 使用
Key + MouseDrag
输入,如下所示:
bool isKeyPressed = false;
public MainWindow()
{
InitializeComponent();
this.PreviewKeyDown += (s1, e1) => { if (e1.Key == Key.LeftCtrl) isKeyPressed = true; };
this.PreviewKeyUp += (s2, e2) => { if (e2.Key == Key.LeftCtrl) isKeyPressed = false; };
this.PreviewMouseLeftButtonDown += (s, e) => { if (isKeyPressed) DragMove(); };
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.