How do I remove minimize and maximize from a resizable window in WPF?

后端 未结 7 1184
我寻月下人不归
我寻月下人不归 2020-12-04 12:05

WPF doesn\'t provide the ability to have a window that allows resize but doesn\'t have maximize or minimize buttons. I\'d like to able to make such a window so I can have re

7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 12:38

    I've stolen some code I found on the MSDN forums and made an extension method on the Window class, like this:

    internal static class WindowExtensions
    {
        // from winuser.h
        private const int GWL_STYLE      = -16,
                          WS_MAXIMIZEBOX = 0x10000,
                          WS_MINIMIZEBOX = 0x20000;
    
        [DllImport("user32.dll")]
        extern private static int GetWindowLong(IntPtr hwnd, int index);
    
        [DllImport("user32.dll")]
        extern private static int SetWindowLong(IntPtr hwnd, int index, int value);
    
        internal static void HideMinimizeAndMaximizeButtons(this Window window)
        {
            IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(window).Handle;
            var currentStyle = GetWindowLong(hwnd, GWL_STYLE);
    
            SetWindowLong(hwnd, GWL_STYLE, (currentStyle & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX));
        }
    }
    

    The only other thing to remember is that for some reason this doesn't work from a window's constructor. I got around that by chucking this into the constructor:

    this.SourceInitialized += (x, y) =>
    {
        this.HideMinimizeAndMaximizeButtons();
    };
    

    Hope this helps!

提交回复
热议问题