Disable close button

tayspen 0 Tallied Votes 399 Views Share

Demonstrates how to disable the close button in C#.

private const int SC_CLOSE = 0xF060;
		private const int MF_GRAYED = 0x1;

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

		[DllImport("user32.dll")]
		private static extern int EnableMenuItem(IntPtr hMenu, int wIDEnableItem, int wEnable);

		private void Form1_Load(object sender, System.EventArgs e)
		{
			EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_GRAYED);
		}

Dani AI

Generated

’s demo shows the common Win32 route (EnableMenuItem); expanded that with a toggle and asked the right question about MF_ENABLED. A few important clarifications that aren’t always obvious: graying the Close menu item alters the system menu but does not by itself cover every close path (Alt+F4, programmatic Close, or OS shutdown). Two more robust approaches are usually preferable depending on the goal — prevent closing, or remove the X visually.

To reliably block user-initiated closes, handle the FormClosing event and cancel when appropriate. This covers the title‑bar X and Alt+F4 in a simple, managed way:

private bool allowClose = false;

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!allowClose && e.CloseReason == CloseReason.UserClosing)
    {
        e.Cancel = true;
    }
}

Set allowClose = true before calling Close() when an actual programmatic close is desired. Checking e.CloseReason avoids cancelling system-initiated shutdowns.

To hide the close button at creation time without P/Invoke, override CreateParams and clear the WS_SYSMENU bit so the X never appears:

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        const int WS_SYSMENU = 0x00080000;
        cp.Style &= ~WS_SYSMENU;
        return cp;
    }
}

To toggle the X at runtime, the usual path is SetWindowLong/SetWindowLongPtr to add/remove WS_SYSMENU and then force a redraw (SetWindowPos with SWP_FRAMECHANGED or call DrawMenuBar). Beware 64‑bit: use the Ptr variants on x64. Finally, no client-side trick will stop a forced process kill or some OS shutdowns — combine menu/style changes with FormClosing checks for the most reliable behavior.

wolflake 0 Unverified User

:eek: :-| :rolleyes:

stribijev 0 Newbie Poster

All right, but this is only good for disabling the button for good. And what if I want to enable it later, after clicking a button on my form?

v_sirigiri 0 Newbie Poster

Here is the code for enabling and disabling the close "x" button, Use a flag variable to toggle the state.

        if (flag == true)
        {
            EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_ENABLED);
        }
        else
        {
            EnableMenuItem(GetSystemMenu(this.Handle, false), SC_CLOSE, MF_GRAYED);
        }
alegn 0 Newbie Poster

And what about MF_ENABLED? Is MF_ENABLED=0x0 ?

v_sirigiri 0 Newbie Poster

Here is what you are looking for

internal const UInt32 MF_ENABLED =0x00000000;
internal const UInt32 MF_GRAYED =0x00000001;
internal const UInt32 MF_DISABLED =0x00000002;

eURe 0 Newbie Poster

For your reference...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.