Distinguish several concepts
<1> "main Menu" and "Top level menu" is a meaning.
<2> the items in the main menu are called "Pop-up menus" or "submenus".
The <3> popup menu item can be another popup menu.
<4> Status of the menu: enabled, disabled. Invalid, invalid The difference between the two is gray display text.
(1) Menu message
<1>wm_initmenu
WParam, //Handle to Menu (HMENU) LParam //Not used
<2>wm_menuselect
When the menu item is selected
WParam, //menu item (UINT) and flags (UINT) lParam //handle to menu (HMENU)
Of
LoWord (WParam)//Selected item: Menu ID or pop-up menu handle HIWORD (WParam)//SELECT flag
Flags are collections of mf_grayed, mf_disabled, mf_checked, Mf_bitmap, Mf_popup, Mf_help, Mf_sysmenu, and Mf_mouseselect.
<3>wm_initmenupopup
This message is sent when the drop-down menu is activated.
WPARAM WPARAM, //Handle to Menu (HMENU) LPARAM LPARAM //Item position and indicator
LoWord (LParam) Represents the menu item index, HIWORD (LParam) represents true, or false, the menu is the System menu when it represents true, non-system menu is the time to indicate false.
<4>wm_command
Indicates that the consumer has selected an enabled menu item from the menu.
LoWord (wParam): menu command ID
HiWord (WParam): 0
lparam:0
<5>wm_menuchar
(2) underline the letters in the menu item
Put the letter in front of the & character, you can appear the effect of the letter underline, when using the ALT key + character, can quickly pop up the submenu, or Run menu item command.
corresponding property
(3) The selected and unchecked status of the menu item
Checkmenuitem (HMenu, iselection, mf_unchecked); Checkmenuitem (HMenu, iselection, mf_checked);
(4) Function on menu
About the operation of the menu from the general direction to see no outside the deletion and change to check four kinds of operations.
4.1 Hmenucreatemenu (VOID);
4.2 BOOL AppendMenu (HMENU HMENU,//Handle to Menu
UINT uflags,//menu-item options
Uint_ptr Uidnewitem,//identifier, menu, or submenu
LPCTSTR lpnewitem//menu-item content);
Among them, uflags can be:
Mf_bitmap. Mf_ownerdraw,mf_string
Demo Sample:
AppendMenu (Hmenupopup, mf_string, Idm_app_exit, "e&xit");
AppendMenu (HMenu, Mf_popup, Hmenupopup, "&file");
4.3 Boolinsertmenu (HMENU HMENU,//Handle to Menu
UINT uposition,//item that new item precedes
UINT uflags,//Options
Uint_ptr Uidnewitem,//identifier, menu, or submenu
LPCTSTR Lpnewitem//menu item content);
This function compared to the appendmenu, except for the uposition, the rest is completely the same
Uposition: Represents the interpolated position index or menu ID, in detail which depends on whether the uflags is Mf_bycommand or mf_byposition.
Uidnewitem: The insert item is a command item that represents the ID, and the Insert item is the menu handle that is represented when the menu is inserted.
Lpnewitem: Inserts the contents of the item, in detail depending on the mf_bitmap included in the uflags. Mf_string. Mf_ownerdraw
Note: Combinations of commands that cannot be put together
- Mf_bycommand and Mf_byposition
- Mf_disabled, mf_enabled, and mf_grayed
- Mf_bitmap, Mf_string, Mf_ownerdraw, and Mf_separator
- Mf_menubarbreak and Mf_menubreak
- Mf_checked and mf_unchecked
4.4 Boolinsertmenuitem (HMENU HMENU,//Handle to Menu
UINT Uitem,//identifier or position
BOOL fbyposition,//Meaning of Uitem
Lpcmenuiteminfo lpmii//menu item information);
typedef struct TAGMENUITEMINFO { UINT cbsize; UINT fmask; UINT FType; UINT fstate; UINT WID; HMENU Hsubmenu; Hbitmap hbmpchecked; Hbitmap hbmpunchecked; Ulong_ptr Dwitemdata; LPTSTR Dwtypedata; UINT CCH; Hbitmap Hbmpitem;} Menuiteminfo, *lpmenuiteminfo;
4.5 BOOL Removemenu (HMENU HMENU,//Handle to menu
UINT uposition,//menu item identifier or position
UINT uflags//options);
4.6 Boolmodifymenu (HMENU hmnu,//Handle to menu
UINT uposition,//menu item to modify
Uintuflags,//Options
Uint_ptr Uidnewitem,//identifier, menu, or submenu
LPCTSTR Lpnewitem//menu item content);
4.7 Booldeletemenu (HMENU HMENU,//Handle to Menu
UINT uposition,//menu item identifier or position
UINT uflags//option);
Deletes a menu item, assuming that the menu item is popup. The memory that the menu handle points to is released.
4.7 Booldrawmenubar (HWND hwnd//handle to window);
4.8 Hmenugetsubmenu (HMENU HMENU,//Handle to Menu
int NPos//menu item position);
When the menu bar has changed. This function must be called again to draw the menu//often validate. Not necessarily
(6) Right-click menu
The right-click menu is no different from the normal menu. Just when it pops up. Need to use this function
Booltrackpopupmenu ( HMENU HMENU, //Handle to shortcut menu UINT uflags, //options int x,/ / horizontal position int y, //Vertical position int nreserved, //reserved, must be zero HWND hwnd, //Handle to owner window CONST RECT *prcrect //ignored);
The uflags parameter specifies the alignment of menus in the menu, left-right button to select menu items
Tpm_centeralign. Tmp_leftalign. Tmp_rightalign
Tpm_bottomalign, Tpm_topalign, tpm_vcenteralign
Tpm_leftbuttpon,tpm_rightbutton
Tpm_nonotify, Tpm_returncmd
(7) System Menu
Get system Menu Handle
HMENU GetSystemMenu (
Hwndhwnd,//Handle to Window
BOOL brevert//reset option);
When Brevert = False. Said. Copies the menu of the system and returns a handle to the Copy menu, which can be changed. When Brevert = TRUE, sets the system menu to the default original state. And the function return value is NULL.
(8) Accelerator key
The accelerator key is also a resource that can be used to quickly open command items using shortcut keys. The accelerator key can be added to the resource itself, and can be written directly using the program.
The basic change in the accelerator key is the code with
while (GetMessage (&msg,null,0,0))
{
if (! TranslateAccelerator (hwnd,haccel,&msg))
{
TranslateMessage (&MSG);
DispatchMessage (&MSG);
}
}
Here the TranslateAccelerator function translates some key messages into WM_COMMAND, or wm_syscommand messages.
(9) Code Demo sample
The Demo sample is a menu demo that relies entirely on code to create resources, including menu bars. Right-click menu, System menu, accelerator key can change the menu dynamically, delete. Join the operation.
#define oemresource# include<windows.h> #define Idm_file_open100#define idm_file_new101#define IDM_FILE_PIC102 #define Idm_file_exit103#define Idm_menu_add104#defineidm_menu_rem105#define Idm_menu_del106#define IDM_MENU_ Mod107#defineidm_about108#define Idm_version109#define Idm_menu_new110#defineidm_pop_one200#define IDM_POP_TWO201 #define Idm_sys_one300#define idm_sys_two301lresult CALLBACK WndProc (HWND hwnd,uint msg,wparam wparam,lparam LPARAM); Hmenucreateownmenu (HWND hwnd); Hmenucreatepopmenu (HWND hwnd); Haccelcreateaccelerator (HWND hwnd); tchar* szappname = TEXT ("Menuclass"); tchar* szwndname = TEXT ("Changemenu"); Haccelhaccel = Null;int WINAPI WinMain (hinstance hinstance,hinstance hpreinstance,lpstr lpcmdline,int iCmdShow) { Wndclasswndcls; Hwndhwnd; Msgmsg;wndcls.cbclsextra = 0;wndcls.cbwndextra = 0;wndcls.lpfnwndproc = Wndproc;wndcls.style = CS_HREDRAW | Cs_vredraw;wndcls.hbrbackground = (hbrush) getstockobject (white_brush); wndcls.hcursor = LoadCursor (NULL,IDC_ARROW); Wndcls.hicon = LoaDiCon (null,idi_application); wndcls.hinstance = Hinstance;wndcls.lpszclassname = Szappname;wndcls.lpszmenuname = NULL ; if (! RegisterClass (&WNDCLS)) {MessageBox (Null,text ("Register window Failed"), TEXT ("Error"), MB_OK);} HWnd = CreateWindow (szappname,szwndname,ws_overlappedwindow,cw_usedefault,cw_usedefault,cw_usedefault,cw_ Usedefault,null,null,hinstance,null); UpdateWindow (HWND); ShowWindow (Hwnd,sw_normal); while (GetMessage (&msg,null,0,0)) {if (! TranslateAccelerator (hwnd,haccel,&msg)) TranslateMessage (&msg);D ispatchmessage (&msg);} return Msg.wparam;} LRESULT CALLBACK WndProc (HWND hwnd,uint msg,wparam wparam,lparam LPARAM) {static int nadd = 1; HMENU Hmenu,hsonmenu,htmpmenu; HMENU Htrack,hson; HMENU Hsysmenu; Point Pt;switch (msg) {Case wm_create:hmenu = Createownmenu (hwnd); SetMenu (hwnd,hmenu); hsysmenu = GetSystemMenu (Hwnd,false); AppendMenu (Hsysmenu,mf_separator,0,null); AppendMenu (Hsysmenu,mf_string,idm_sys_one,text ("Sysone")); AppendMenu (Hsysmenu,mf_string,idm_sys_two,text ("Systwo")); HACcel = Createaccelerator (hwnd), Break;case Wm_syscommand:switch (LoWord (WParam)) {case Idm_sys_one:messagebox (NULL, TEXT ("This is added system menu Item1"), NULL,MB_OK); Break;case Idm_sys_two:messagebox (Null,text ("This is added system Menu item2 "), NULL,MB_OK); Break;case Wm_command:hmenu = GetMenu (hwnd), switch (LOWORD (wParam)) {Case Idm_file_open:messagebox (Null,text ("FILE Open selected "), TEXT (" Test check "), MB_OK), break;case idm_menu_add:htmpmenu = GetSubMenu (hmenu,2); AppendMenu (Htmpmenu,mf_string,idm_menu_new+nadd,text ("NEW Item")), nadd++;D Rawmenubar (hwnd); Break;case IDM_MENU_ Rem:htmpmenu = GetSubMenu (hmenu,2); if (Nadd >1) {nadd--; Removemenu (Htmpmenu,idm_menu_new+nadd,mf_bycommand);} When the menu item pops up, only the popup menu is disconnected from the menu, but the object/*getsubmenu (htmpmenu,2) is not destroyed; Removemenu (htmpmenu,2,mf_byposition); */drawmenubar (hwnd); Break;case Idm_menu_mod:htmpmenu = GetSubMenu (hMenu,2); Modifymenu (Htmpmenu,0,mf_byposition,idm_about,text ("Modified Item"));D Rawmenubar (HWND); Break;case Idm_menu_del: Htmpmenu = GETSUBMENu (hmenu,2);D eletemenu (htmpmenu,2,mf_byposition);D Rawmenubar (HWND); Break;case wm_rbuttondown:htrack= Createpopmenu (hwnd), hson= GetSubMenu (htrack,0);p t.x = LoWord (lParam);p t.y = HIWORD ( LParam); ClientToScreen (HWND,&PT); TrackPopupMenu (hson,tpm_leftbutton| Tpm_rightalign,pt.x,pt.y,0,hwnd,null); Break;case wm_destroy:postquitmessage (0); break;} return DefWindowProc (Hwnd,msg,wparam,lparam);} HMENU Createownmenu (HWND hwnd) {HMENU hmenu= createmenu (); HMENU hpopmenu= CreateMenu ();//mf_byposiotion,mf_bycommand is isn't useful here in Appendmenuappendmenu (hpopmenu,mf_ String| mf_checked| Mf_grayed,idm_file_open,text ("&open")); InsertMenu (hpopmenu,0,mf_byposition| Mf_string| Mf_disabled,idm_file_new,text ("&new")); HINSTANCE hinstance = (hinstance) GetWindowLong (hwnd,gwl_hinstance); Hbitmap hbmp = (hbitmap) loadimage (Hinstance,text ("Bitmap1.bmp"), image_bitmap,0,0,lr_loadfromfile); AppendMenu (Hpopmenu,mf_bitmap,idm_file_pic, (LPCWSTR) hbmp); AppendMenu (Hpopmenu,mf_separator,null,null); AppendMenu (hpopmEnu,mf_string,idm_file_exit,text ("&exit")); SetMenuItemBitmaps (hpopmenu,idm_file_pic,mf_bycommand,hbmp,hbmp); AppendMenu (Hmenu,mf_popup, (uint_ptr) hpopmenu,text ("File"); Hpopmenu = CreateMenu (); AppendMenu (Hpopmenu,mf_string,idm_menu_add,text ("&add")); AppendMenu (Hpopmenu,mf_string,idm_menu_rem,text ("&remove")); AppendMenu (Hpopmenu,mf_string,idm_menu_mod,text ("&modify")); AppendMenu (Hpopmenu,mf_string,idm_menu_del,text ("&delete")); AppendMenu (Hmenu,mf_popup, (uint_ptr) hpopmenu,text ("menu")); Hpopmenu = CreateMenu (); AppendMenu (Hpopmenu,mf_string,idm_about,text ("about")); AppendMenu (Hpopmenu,mf_string,idm_version,text ("VERSION")); HMENU Hsonmenu = CreateMenu (); AppendMenu (Hsonmenu,mf_string,idm_menu_new,text ("Son")); AppendMenu (Hpopmenu,mf_popup, (uint_ptr) Hsonmenu,text ("Son menu"); AppendMenu (Hmenu,mf_popup, (uint_ptr) hpopmenu,text ("Change"); return hMenu;} HMENU Createpopmenu (HWND hwnd) {HMENU HMENU = CreateMenu (); HMENU hpop= CreateMenu (); AppendMenu (Hpop,mf_string,idm_pop_one,text ("ONe ")); AppendMenu (Hpop,mf_string,idm_pop_two,text ("Twod")); AppendMenu (Hmenu,mf_popup, (uint_ptr) hpop,text ("Pop"); return hMenu;} Haccelcreateaccelerator (HWND hwnd) {ACCEL Acce[2];acce[0].fvirt = Fcontrol | Fnoinvert | Fvirtkey;acce[0].key = ' A '; acce[0].cmd = Idm_menu_add;acce[1].fvirt = Fcontrol | Fnoinvert | Fvirtkey;acce[1].key = ' R '; acce[1].cmd = Idm_menu_rem; Haccel haccel = createacceleratortable (acce,2); return haccel;}
Windows Programming Menu Actions