Posted by: R Manimaran | February 2, 2007

DISABLE X button in C# Windows Application

using System.Runtime.InteropServices;

// …

[DllImport(“user32.dll”, EntryPoint=”GetSystemMenu”)]
private static extern IntPtr GetSystemMenu(IntPtr hwnd, int revert);

[DllImport(“user32.dll”, EntryPoint=”GetMenuItemCount”)]
private static extern int GetMenuItemCount(IntPtr hmenu);

[DllImport(“user32.dll”, EntryPoint=”RemoveMenu”)]
private static extern int RemoveMenu(IntPtr hmenu, int npos, int wflags);

[DllImport(“user32.dll”, EntryPoint=”DrawMenuBar”)]
private static extern int DrawMenuBar(IntPtr hwnd);

private const int MF_BYPOSITION = 0x0400;
private const int MF_DISABLED = 0x0002;

// …

IntPtr hmenu = GetSystemMenu(this.Handle, 0);
int cnt = GetMenuItemCount(hmenu);

// remove ‘close’ action
RemoveMenu(hmenu, cnt-1, MF_DISABLED | MF_BYPOSITION);

// remove extra menu line
RemoveMenu(hmenu, cnt-2, MF_DISABLED | MF_BYPOSITION);

Advertisements

Responses

  1. Hi, i was searching for this this was what i searched for But i found a better solution and just wanted to let you know…

    protected override CreateParams CreateParams
    {
    get
    {
    CreateParams cp = base.CreateParams;
    const int CS_NOCLOSE = 0x200;
    cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
    return cp;
    }
    }


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: