天天看點

WPF窗體禁用最大化按鈕

禁用WPF窗體的最大化按鈕可以使用Windows API改變按鈕狀态的方法實作。

使用GetWindowLong可以得到目前按鈕的狀态。使用SetWindowLong可以設定按鈕的狀态。

使用SetWindowPos進行界面的更新。

下面是這幾個API的聲明。

        [DllImport("user32.dll", EntryPoint = "GetWindowLong")]

        public static extern int GetWindowLong(IntPtr hwnd, int nIndex);

        [DllImport("user32.dll", EntryPoint = "SetWindowLong")]

        public static extern int SetWindowLong(IntPtr hMenu, int nIndex, int dwNewLong);

        [DllImport("user32.dll")]

        private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);

進行禁用後必須進行界面的重新整理,否則禁用狀态不會立即顯示在界面上。

        private void DisableMaxmizebox(bool isDisable)

        {

            int GWL_STYLE = -16;

            int WS_MAXIMIZEBOX = 0x00010000;

            int SWP_NOSIZE = 0x0001;

            int SWP_NOMOVE = 0x0002;

            int SWP_FRAMECHANGED = 0x0020;

            IntPtr handle = new WindowInteropHelper(this).Handle;

            int nStyle = GetWindowLong(handle, GWL_STYLE);

            if (isDisable)

            {

                nStyle &= ~(WS_MAXIMIZEBOX);

            }

            else

            {

                nStyle |= WS_MAXIMIZEBOX;

            }

            SetWindowLong(handle, GWL_STYLE, nStyle);

            SetWindowPos(handle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_FRAMECHANGED);

        }

這個函數可以通過參數确定是否禁用。

轉載于:https://www.cnblogs.com/moonlight-zjb/p/3442304.html