天天看點

WPF 通過Win32SDK修改視窗樣式

使用函數為

SetWindowLong

GetWindowLong

注冊函數

[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);      

使用函數

private void DisableSizebox()
        {
            int GWL_STYLE = -16;
            int WS_THICKFRAME = 0x00040000;
            IntPtr handle = new WindowInteropHelper(this).Handle;
            int  nStyle = GetWindowLong(handle, GWL_STYLE);
            //禁用調整視窗大小
            nStyle &= ~WS_THICKFRAME;
            SetWindowLong(handle, GWL_STYLE,nStyle);
        }      

注意 啟用視窗樣式為

nStyle|=WS_THICKFRAME      

如果是多個樣式啟用或者禁止為

//禁用
nStyle &= ~WS_CAPTION&~WS_MINIMIZEBOX;
//啟用
nStyle |= WS_MAXIMIZEBOX| WS_CAPTION;      

具體樣式請參考具體Windows Style

注意:msdn中均為Long型,去掉L即可為int,

本函數均為32位,對應64位請參考網頁的GetWindowLongPtrA,SetWindowLongPtrA使用方法不變