How to hide close button and disable resizing in WPF window?

92 Views Asked by At

I'm writing a wpf window which have a custom minmize button and close button instead of the default one, at mean while it should not be resizable. Simply set WindowStyle=None and ResizeMode=NoResize did work, but I hope other default window behaviors retained as well (such as shadows, animations, corner radius etc..)

After many search I found this P/Invoke method,

private const int GWL_STYLE = -16;
private const int WS_SYSMENU = 0x80000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

then

var hwnd = new WindowInteropHelper(this).Handle;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);

It works fine for me, and use code below could just disable window resizing.

private const int WS_SIZEBOX = 0x00040000;
SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SIZEBOX);

Now the problem is that ~WS_SYSMENU and ~WS_SIZEBOX could not valid at the same time. Once I set ~WS_SYSMENU, the window is resizable whether ever ~WS_SIZEBOX is set. Could anyone help me? Any advices are grateful.

Forgot to mention that I did set them together as a bro just commented:

SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~(WS_SIZEBOX | WS_SYSMENU));
1

There are 1 best solutions below

0
On

Finally I found out that was a WindowChrome issue. In my xaml code have codes like:

<WindowChrome.WindowChrome>
    <WindowChrome GlassFrameThickness="-1" CaptionHeight="0"/>
</WindowChrome.WindowChrome>

After set the ResizeBorderThickness property to zero the issue resolved.

<WindowChrome GlassFrameThickness="-1" CaptionHeight="0" ResizeBorderThickness="0"/>