DEMO code on how to disable the X button in menu bar in a window. I found a lot of posts on this in VB, but none for C #. so if you are a C # fan like me, this is for you...
1. There is no direct way to disbale the X button, like there is a property for maximize button called maximizebox = false.
2. This is implemented by importing Unmanaged DLL "USER32" and calling it's functions.
3. before you use this code, make sure to add a close button in your form so that you can close your app. see the attached screen shot disabled.jpg. below is the code for this form.
Add the following Library
Using system. runtime. interopservices;
Declare the following as class level variable
Const int mf_byposition = 0x400;
[Dllimport ("USER32")]
Private Static extern int removemenu (intptr hmenu, int nposition, int wflags );
[Dllimport ("USER32")]
Private Static extern intptr getsystemmenu (intptr hwnd, bool brevert );
[Dllimport ("USER32")]
Private Static extern int getmenuitemcount (intptr hwnd );
In the form_load () event, write the following code:
Private void form1_load (Object sender, eventargs E)
{
Intptr hmenu = getsystemmenu (this. Handle, false );
Int menuitemcount = getmenuitemcount (hmenu );
Removemenu (hmenu, menuitemcount-1, mf_byposition );
}
4. Run it. Voila! You are done. If you know of a ay to do it without calling unmanaged code, please do let me know.
First, you must declare and assign some fixed variables.Value, Their names andValueThe list is as follows:
Mf_popup: = 16
Mf_bycommand: = 0
Mf_byposition: = 1024
Mf_separator: = 2048
Mf_enabled: = 0
Mf_grayed: = 1
Mf_disabled: = 2
Mf_unchecked: = 0
Mf_checked: = 8
Mf_usecheckbitmaps: = 512
Mf_string: = 0
Mf_bitmap: = 4
Mf_ownerdraw: = 256
Mf_menubarbreak: = 32
Mf_menubreak: = 64
Mf_unhilite: = 0
Mf_hilite := 128
Function: This function makes the specified menu item valid, invalid, or grayed out.
Function prototype: bool enablemenutem (hmenu, uint uldenablttem, uint uenable;
Parameters
Hmenu: menu handle.
Uldenableltem: Specify the menu items that will make them valid, invalid, or grayed out, which are determined by the uenable parameter. This parameter can be used to specify menu items, menus, or menu items in sub-menus.
Uenable: specifies how the uidenableltem parameter interprets the flag, indicating that the menu item is valid, invalid, or grayed out. This parameter must be a combination of mf_bycommand or mf_byposition, mf_enabled, mf_disable, or mf_grayed.
Mf_bycommand: indicates that the uidenableltem parameter provides the identifier of the menu item. If mf_bycommand and mf_position are not specified, mf_bycommand is the default flag.
Mf_byposition: indicates that the uidenableltem parameter provides the relative position of the menu item based on zero.
Mf_disabled: indicates that the menu item is invalid but not grayed out, so it cannot be selected.
Mf_enabled: indicates that the menu item is valid and restored from the dimmed State. Therefore, it can be selected.
Mf_grayed: indicates that the menu item is invalid and grayed out, so it cannot be selected.
Return Value: the return value specifies the previous state of the menu item (mf_disabled, mf_enabled or mf_grayed ). If this menu item does not exist, the return value is oxffffffff.
Note: An application must use mf_byposition to specify the correct menu handle. If the menu handle of a menu bar is specified, the top menu item (the menu item on the menu bar) will be affected. To set the menu item or sub-menu status in the drop-down menu according to the position, the application specifies the drop-down menu or sub-menu handle. When the application specifies the mf_bycommand flag, the system selects the menu items that open the sub-menu in the menu marked by the specified menu handle. Therefore, unless you want to copy the menu item, the handle of the specified menu bar is enough. The insertmenu, insertmenultem, loadmenulndirect, modifymenu, and setmenultemlnfo functions can also set the status of menu items (valid, invalid, or grayed out ). Windows CE: Windows CE does not support the mf_disabled flag for the uenable parameter. If the menu item is not grayed out, the menu item cannot be invalid. To invalidate the menu item, use the mf_rayed flag.
Quick query: Windows NT: 3.1 and later; windows: 95 and later; Windows CE: 1.0 and later; header file: winuser. h; input Library: user32.lib.
Http://support.microsoft.com/kb/300688/zh-cn
If (this. Modal. Equals (false ))
{
Intptr hmenu = getsystemmenu (this. Handle, 0 );
// Removemenu (hmenu, SC _close, mf_bycommand );
Uint32 menuitemcount = getmenuitemcount (hmenu );
// Removemenu (hmenu, menuitemcount-1, mf_byposition );
Enablemenuitem (hmenu, menuitemcount-1, mf_byposition | mf_disabled );
}
Else
{
Intptr hmenu = getsystemmenu (this. Handle, 0 );
// Removemenu (hmenu, SC _close, mf_bycommand );
Uint32 menuitemcount = getmenuitemcount (hmenu );
// Removemenu (hmenu, menuitemcount-1, mf_byposition );
Enablemenuitem (hmenu, menuitemcount-1, mf_byposition | mf_disabled );
}