Removing the Close Button on a WPF window

Today I was building a simple simulator to test some events to a new piece of hardware I’m working on.  Of course, I’m using WPF to show the simulator – and I wanted to create a topmost window that did not have a Close button on it.  Imagine my surprise when I realized there was not a set of flags you could supply to the Window object to actually achieve this result!

However, with a little Win32 mojo we can get the desired effect:

public partial class MainWindow
    public MainWindow()
        SourceInitialized += MainWindow_SourceInitialized;

    void MainWindow_SourceInitialized(object sender, EventArgs e)
        WindowInteropHelper wih = new WindowInteropHelper(this);
        int style = GetWindowLong(wih.Handle, GWL_STYLE);
        SetWindowLong(wih.Handle, GWL_STYLE, style & ~WS_SYSMENU);

    private const int GWL_STYLE = -16;
    private const int WS_SYSMENU = 0x00080000;

    private extern static int SetWindowLong(IntPtr hwnd, int index, int value);
    private extern static int GetWindowLong(IntPtr hwnd, int index);

The key here is hooking the SourceInitialized event and then using the SetWindowLong function to strip off the WS_SYSMENU bit.  You can just cut/paste this code right into your solution.

About Mark Smith

Windows systems developer with low-level operating system, threading and .NET experience. For the past several years I have been involved with WPF/Silverlight and most recently iOS and Android.

I have worked as an architect and designer for several companies and am particularly interested in client/server and graphical solutions.

Specialties:.NET, WPF, Silverlight, Windows, iOS