Disable close button

tayspen 0 Tallied Votes 366 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);
		}
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
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.