在WPF中移动无边界窗口 [英] Move a borderless window in wpf

查看:45
本文介绍了在WPF中移动无边界窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#WinForms应用程序中,我有一个主窗口,其中隐藏了其默认控件.

In my C# WinForms app I have a main window that has its default controls hidden.

因此,为了让我可以移动它,我在主窗口中添加了以下内容:

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;
}

我有一个WPF应用程序,其中还隐藏了默认控件,我也想这样做.我看到主窗口是从窗口"派生的,因此上面的代码不起作用.如何在WPF中执行此操作?

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?

推荐答案

为此,您需要将事件处理程序附加到窗口的 MouseDown 事件上,请检查鼠标左键是否为按下并在窗口上调用 DragMove 方法.

To do this you will want to attach an event handler to the MouseDown event of the window, check that the left mouse button was pressed and call the DragMove method on the window.

以下是具有此功能的窗口的示例:

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();
    }
}

这篇关于在WPF中移动无边界窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆