In this snippet, i will show to How to Disable Close Button in Title bar of a Windows Form.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
2. In the class add .
private const int MF_BYPOSITION = 0x400;
private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
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. Thank you reading.