Operations on the system menu of the window
Zhou yinhui
If you want to disable/remove/Add some system menu items, you may use the following API. The C # platform call is used as an example:
[Dllimport ("user32.dll")]
Internal static extern intptr getsystemmenu (intptr hwnd, int32 brevert );
This function is used to obtain the system menu handle. The hwnd parameter is the window handle, indicating the system menu of the window you want to obtain; brevert, bool value. If it is false, the entire function will return the copy of the System menu handle of the original window; if it is true, the system menu of the original window will be reset, and the function will return 0. Obviously, this function will become "Reset Window System Menu.
[Dllimport ("user32.dll")]
Internal static extern int getmenuitemcount (intptr hmenu );
This function is used to obtain the number of items in the System menu. hmenu is the system menu handle.
[Dllimport ("user32.dll")]
Internal static extern bool enablemenuitem (intptr hmenu, int32 uidenableitem, int32 uenable );
This function is used to enable or disable system menus. Hmenu is the menu handle. Uidenableitem indicates the ID or position of the Operation item (depending on the third parameter ). Uenable, which can be:
Internal const int32 umf_enabled = 0x00000000;
Internal const int32 umf_grayed = 0x00000001;
Internal const int32 umf_disabled = 0x00000002;
Internal const int32 umf_bycommand = 0x00000000;
Internal const int32 umf_byposition = 0x00000400;
If uenable is mf_byposition, uidenableitem should pass in index information (0, 1...). If it is mf_bycommand, it should pass in ID
[Dllimport ("user32.dll", charset = charset. Auto)]
Internal static extern int appendmenu (INT hmenu, int flagsw, int idnewitem, string lpnewitem );
Append a project to the system menu. Hmenu is the menu handle. The idnewitem parameter requires the id you created for the newly added item. lpnewitem is the menu text, but it is some flag:
Flagsw
/*
The following flag can be set in uflags:
Mf_bitmap: uses a bitmap as a menu item. The lpnewltem parameter contains the bitmap handle.
Mf_checked: place a selection Tag next to the menu item. If the application Program Provides a selection tag. Bitmap (see setmenultembitmaps) places the selection tag bitmap next to the menu item.
Mf_disabled: the menu item is invalid, so that the item cannot be selected, but the menu item is not dimmed.
Mf_enabled: Make the menu item valid so that the item can be selected and restored from the dimmed state.
Mf_grayed: Make the menu item invalid and gray, so that it cannot be selected.
Mf_menubarbreak: the function of the menu bar is the same as that of the mf_menubreak flag. For drop-down menus, submenus, or shortcut menus, new columns and old columns are separated by vertical lines.
Mf_menubreak: place a menu item on a new line (for a menu bar) or a new column (for a drop-down menu, sub menu, or shortcut menu) without a split column.
Mf_ownerdraw: specify this menu item as a self-drawn menu item. Before the menu is displayed for the first time, the window with the menu receives a message wm_measureitem to get the width and height of the menu item. Then, as long as the menu item is modified, the wm_drawitem message will be sent to the window program of the menu owner.
Mf_popup: specify a drop-down menu or sub-menu to open. The handle of the uidnewltem drop-down menu or sub-menu. This flag is used to add a name to a menu bar, open a drop-down menu, or menu item, sub menu, or shortcut menu of the menu.
Mf_separator: draws a horizontal line. This flag is only used by drop-down menus, menus, or shortcut menus. This line cannot be dimmed, invalid, or highlighted. The ipnewltem and uidnewltem parameters are useless.
Mf_string: specifies that the menu item is a body string. The lpnewltem parameter points to this string.
Mf_unchecked: do not place the selection Tag next to the menu item (default ). If the application provides a select tag Bitmap (see setmenultembitmaps), the Select tag bitmap is placed next to the menu item.
The following flag groups cannot be used together:
Mf_disabled, mf_enabled, and mf_grayed; mf_bitmap, mf_string, and mf_ownerdraw
Mf_menubarbreak and mf_menubreak; mf_checked and mf_unchecked
In Windows CE, The fuflags parameter cannot use the following flag:
Mf_bitmap; mf_dosable; mf_grayed
Mf_grayed can be used to replace mf_disabled and mfs_grayed.
Windows CE 1.0 does not support stacked menus. When using Windows CE 1.0, one mf_popup menu cannot be inserted into another drop-down menu. Windows CE 1.0 does not support the following labels:
Mf_popup; mf_menubreak; mf_menubarbreak
In Windows CE 2.0 or later versions, the above labels and stacked menus are supported.
*/
[Dllimport ("user32.dll")]
Internal static extern bool deletemenu (intptr hmenu, uint uposition, uint uflags );
This function is used to delete system menu items. Hmenu is the menu handle. Uposition indicates the ID or position of the Operation item (depending on the third parameter ). Uflags, which can be:
Internal const int32 umf_disabled = 0x00000002;
Internal const int32 umf_bycommand = 0x00000000;
If uenable is mf_byposition, uidenableitem should pass in index information (0, 1...). If it is mf_bycommand, it should pass in ID
[Dllimport ("user32.dll")]
Internal static extern int drawmenubar (intptr hwnd );
This function is used to repaint the menu, hwnd menu handle. After disabling/removing/adding some system menu items, do not forget to call this function to re-paint it.
The following is an example.Code:
Internal Static Bool Removeunwantedsystemmenuitems ( This Window window)
{
If (Window = Null )
{
Return False ;
}
VaR winhelper= NewWindowinterophelper (window );
Intptr hwnd=Winhelper. Handle;
Intptr sysmenu=Getsystemmenu (hwnd,0);
// 6 close, 5 splite line, 4 maximize, 3 minimize, 2 size, 1 move, 0 restore
// Notes that, delete from bigger index to smaller
Bool Deletedall = Deletemenu (sysmenu, 5 , Mf_byposition) &
Deletemenu (sysmenu, 4 , Mf_byposition) &
Deletemenu (sysmenu, 3 , Mf_byposition) &
Deletemenu (sysmenu, 0 , Mf_byposition );
Drawmenubar (sysmenu );
ReturnDeletedall;
}