Thursday 15 November 2007

Disabling the Window Close Button and System Close menu in a Windows Form

A useful snippet for disabling the X button on a window form in c#:

1. Use an api call to user32.api

Basically, this call uses GetSystemMenu() to get a handle to the menu with the close, maximize and minimize buttons. Then, the EnableMenuItem() call comes along and sets it so that the close button is greyed out (i.e. disabled).

NB. The code needs to go in the Paint handler because otherwise the changes to the close button will not be retained when the form is repainted (e.g. when you minimise and then restore).

C# Source:


Use these constants

///
/// Windows constant that represents the X button on a window
///
private const int SC_CLOSE = 0xF060;

///
/// Menu API Constant: Enables the menu item so that it can be selected and restores it from its grayed state.
///
private const int MF_ENABLED = 0;

///
/// Menu API Constant: Disables the menu item and grays it so it cannot be selected.
///
private const int MF_GREYED = 0x1;

///
/// Menu API Constant: Identifies menu item position by command.
///
private const int MF_BYCOMMAND = 0;

Then add references to the api windows methods:

///
/// Windows api call to get the system menu
///
/// hWnd Handle of window

/// revert Specifies the action to be taken. If this parameter is FALSE, GetSystemMenu returns a handle to the copy of the window menu currently in use. The copy is initially identical to the window menu, but it can be modified. If this parameter is TRUE, GetSystemMenu resets the window menu back to the default state. The previous window menu, if any, is destroyed.
/// returns variable: if the bRevert parameter is FALSE, the return value is a handle to a copy of the window menu. If the bRevert parameter is TRUE, the return value is NULL.
[DllImport("user32")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, int revert);

///
/// Windows api call to enabling a menu item
///
/// hWndMenu handle to the window menu
/// itemID item to enable
/// enable enable/disable/greyed
/// returns variable: The return value specifies the previous state of the menu item (it is either MF_DISABLED, MF_ENABLED, or MF_GRAYED). If the menu item does not exist, the return value is -1.
[DllImport("user32")]
private static extern int EnableMenuItem(IntPtr hWndMenu, int itemID, int enable);

This private field to represent if the close x is enabled/disabled

///
/// Flag to represent if a form window close x is enabled/disabled
///
private bool closeEnabled = false;

and finally a public property - so we can access this outside the form if required.

///
/// Property manages the visibility of the X close button on this form window
///
public bool CloseEnabled
{
get { return closeEnabled; }
set
{
//Decide on the state to pass based on the value being set
int setting = (value) ? MF_BYCOMMAND | MF_ENABLED : MF_BYCOMMAND | MF_GREYED;

try
{
EnableMenuItem(GetSystemMenu(this.Handle, 0), SC_CLOSE, setting);
}
catch { }
closeEnabled = value;
}
}


2) Set the 'ControlBox' property on the form to false

this.ControlBox = false;

However this disables the entire system menu strip - i.e. icon drop down etc. so may not be of use.

Also by holding down ALT-F4 - this often closes an application window and therefore you would need to capture this in the form_closing event - and set event args e to cancel.

i.e. e.cancel = false;

No comments: