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);
		}
6
Contributors
6
Replies
7
Views
11 Years
Discussion Span
Last Post by eURe
0

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?

0

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);
        }

Edited by Nick Evan: Fixed formatting

0

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;

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.