Non-resizeable, bordered WPF Windows with WindowStyle=None

瘦欲@ 提交于 2019-12-24 02:35:08

问题


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 :)


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



回答2:


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.



来源:https://stackoverflow.com/questions/2943248/non-resizeable-bordered-wpf-windows-with-windowstyle-none

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!