在 wpf 中移动无边框窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20623837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move a borderless window in wpf
提问by Harry Boy
In my C# WinForms app I have a main window that has its default controls hidden.
在我的 C# WinForms 应用程序中,我有一个隐藏了默认控件的主窗口。
So to allow me to move it around I added the following to the main window:
所以为了让我移动它,我在主窗口中添加了以下内容:
private const int WM_NCHITTEST = 0x84;
private const int HTCLIENT = 0x1;
private const int HTCAPTION = 0x2;
private const int WM_NCLBUTTONDBLCLK = 0x00A3;
protected override void WndProc(ref Message message)
{
if (message.Msg == WM_NCLBUTTONDBLCLK)
{
message.Result = IntPtr.Zero;
return;
}
base.WndProc(ref message);
//Allow window to move
if (message.Msg == WM_NCHITTEST && (int)message.Result == HTCLIENT)
message.Result = (IntPtr)HTCAPTION;
}
I have a WPF App where I have also hidden the default controls and I want to do the same. I see that the main window is derived from a 'Window' so the above code does not work. How do I do this in WPF?
我有一个 WPF 应用程序,我也隐藏了默认控件,我也想这样做。我看到主窗口是从“窗口”派生的,所以上面的代码不起作用。我如何在 WPF 中做到这一点?
回答by
To do this you will want to attach an event handler to the MouseDownevent of the window, check that the left mouse button was pressed and call the DragMovemethod on the window.
为此,您需要将事件处理程序附加到MouseDown窗口的事件,检查是否按下了鼠标左键并调用DragMove窗口上的方法。
Here is a sample of a window with this functionality:
以下是具有此功能的窗口示例:
public partial class MyWindow : Window
{
public MyWindow()
{
InitializeComponent();
MouseDown += Window_MouseDown;
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
}

