WindowStyle=None 的不可调整大小、有边框的 WPF 窗口

发布于 2024-09-03 18:36:21 字数 581 浏览 4 评论 0原文

基本上,我需要一个如下图所示的窗口: http://screenshots.thex9 .net/2010-05-31_2132.png

(不可调整大小,但保留玻璃边框)

我已设法使其与 Windows 窗体一起使用,但我需要使用 WPF。为了让它在 Windows 窗体中工作,我使用了以下代码:

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84 /* WM_NCHITTEST */)
        {
            m.Result = (IntPtr)1;
            return;
        }
        base.WndProc(ref m);
    }

这完全符合我的要求,但我找不到 WPF 等效项。我用 WPF 实现的最接近结果导致窗口忽略任何鼠标输入。

任何帮助将不胜感激:)

Basically, I need a window to look like the following image: http://screenshots.thex9.net/2010-05-31_2132.png

(Is NOT resizeable, yet retains the glass border)

I've managed to get it working with Windows Forms, but I need to be using WPF. To get it working in Windows Forms, I used the following code:

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x84 /* WM_NCHITTEST */)
        {
            m.Result = (IntPtr)1;
            return;
        }
        base.WndProc(ref m);
    }

This does exactly what I want it to, but I can't find a WPF-equivalent. The closest I've managed to get with WPF caused the Window to ignore any mouse input.

Any help would be hugely appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-09-10 18:36:21

一个非常简单的解决方案是在窗口构造函数中将每个窗口的最小和最大大小设置为彼此相等并设置为固定数字。就像这样:

public MainWindow()
{
    InitializeComponent();

    this.MinWidth = this.MaxWidth = 300;
    this.MinHeight = this.MaxHeight = 300;
}

这样用户就不能改变窗口的宽度和高度。您还必须设置“WindowStyle=None”属性才能获取玻璃边框。

A very simple solution is to set the Min and Max size of each window equal to each other and to a fix number in the window constructor. just like this:

public MainWindow()
{
    InitializeComponent();

    this.MinWidth = this.MaxWidth = 300;
    this.MinHeight = this.MaxHeight = 300;
}

this way the user can not change the width and height of the window. also you must set the "WindowStyle=None" property in order the get the glass border.

恏ㄋ傷疤忘ㄋ疼 2024-09-10 18:36:21

您需要为消息循环添加一个钩子:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var interopHelper = new WindowInteropHelper(this);
    var hwndSource = HwndSource.FromHwnd(interopHelper.Handle);
    hwndSource.AddHook(WndProcHook);
}

private IntPtr WndProcHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == 0x84 /* WM_NCHITTEST */)
    {
         handled = true;
         return (IntPtr)1;
    }
}

You need to add a hook for the message loop :

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    var interopHelper = new WindowInteropHelper(this);
    var hwndSource = HwndSource.FromHwnd(interopHelper.Handle);
    hwndSource.AddHook(WndProcHook);
}

private IntPtr WndProcHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == 0x84 /* WM_NCHITTEST */)
    {
         handled = true;
         return (IntPtr)1;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文