0

I would like to remove the close button (x on top right) in my WPF application. I found some hints in the web and do it like that:

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

[DllImport("user32.dll")]
private extern static int SetWindowLong(IntPtr hwnd, int index, int value);
[DllImport("user32.dll")]
private extern static int GetWindowLong(IntPtr hwnd, int index);

public MainWindow()
{
    SourceInitialized += MainWindow_SourceInitialized;
    InitializeComponent();
}

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

That works fine so far, except that it also removes my minimize/maximize buttons. But i only want to remove the close button. Is there a way just to remove the close button only and keep the maximize and minimize button?

For those who think it is an bad idea to remove the x button because i have read this here before: Its not due to bad UI design or whatever. It just a matter of fact that not all user levels should be allowed to close an application in an productive environment.

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
ck84vi
  • 1,556
  • 7
  • 27
  • 49
  • I would suggest to go for custom `WindowChrome` for your app. It will give you more flexibility for redefining the interface. – pushpraj Aug 18 '14 at 02:47
  • 1
    http://stackoverflow.com/questions/6792275/how-to-create-custom-window-chrome-in-wpf – Colin Smith Aug 18 '14 at 02:55
  • Here it is. http://stackoverflow.com/questions/743906/how-to-hide-close-button-in-wpf-window – Joy Hyuk Lee Aug 18 '14 at 04:17
  • thanks for the hint to chrome, have never heard about that. the link that should be a duplicate isnt a duplicate. since it does not solve my particular problem as i described before. anyway windowchrome does. – ck84vi Aug 20 '14 at 13:34

0 Answers0