Disabling "X" button in WinForm
-
ControlBox = false
-
Depend on how you would like for it to behave 1. Handle form closing event, set e.Cancel to true Or 2. Use Win32 API
// constants for menu manipulation const int SC_CLOSE = 0xF060; const int MF_BYCOMMAND = 0x0; [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern IntPtr RemoveMenu(IntPtr menuHwnd, int position, int flags ); public static void EnableApplicationClose( System.Windows.Forms.Form form, bool val ) { IntPtr menu = GetSystemMenu( form.Handle, val ? 1 : 0 ); if ( !val && menu != IntPtr.Zero ) RemoveMenu( menu, SC_CLOSE, MF_BYCOMMAND ); else { // needed when enabling the app close box to force it to redraw. form.Enabled = false; form.Enabled = true; } } [System.Runtime.InteropServices.DllImport("user32.dll")] public static extern IntPtr GetSystemMenu(IntPtr hwnd, int bRevert);
Jup -- modified at 9:03 Friday 7th July, 2006