Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Disable Close Button from Title bar of a Window

0.00/5 (No votes)
8 Dec 2009 2  
As a developer, you must have found few things that are really haven't thought of earlier. On that note, Say you want to disable Close Button of a window. It is easier to disable other button (using properties available with Form) from control box of the window, or even remove the control box...
As a developer, you must have found few things that are really haven't thought of earlier. On that note, Say you want to disable Close Button of a window. It is easier to disable other button (using properties available with Form) from control box of the window, or even remove the control box altogether. But If you still like to disable the Close button Just follow the steps :

1. Add this using Statement in the header :
using System.Runtime.InteropServices;


2. In the class add :
private const int MF_BYPOSITION = 0x400;

[DllImport("User32")]
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);

[DllImport("User32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[DllImport("User32")]
private static extern int GetMenuItemCount(IntPtr hWnd);



3. Finally to disable the close button during runtime Use :
IntPtr hMenu = GetSystemMenu(this.Handle, false);
int menuItemCount = GetMenuItemCount(hMenu);
RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);


You are done. You will actually see a window with disabled close button.

Cheers. :rose:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here