Theory
To create a normal menu, follow these steps:
- 1) create a menu, for example, createmenu.
- 2) Add menu items such as appendmenu
- 3) load the menu loadmenu
- 4) set menu to window setmenu
- 5) process menu response
- 6) Remove menu destroymenu
To create a shortcut menu, follow these steps:
- 1) create a menu, for example, createmenu.
- 2) Add menu items such as appendmenu
- 3) Use trackpopupmenu to load the menu.
- 4) process menu response
- 5) Remove menu destroymenu
Of course, if you select to edit an existing menu, you can also skip some of the steps. The example in this section is to add a custom menu item in an existing menu. You can also select an existing menu bar as the pop-up menu.
After a menu is used, the deletion is required. When a menu handle connected to the window is destroyed, it is automatically released by windows and does not need to be manually released, however, if the menu is not connected to the window, the program will release it by using the destroymenu function. For example, if it is not connected to the window, it will only use the menu handle popped up by trackpopupmenu. In this example, we use a window-related menu handle, so we do not need to call destroymenu to remove menu resources.
Menu message: When you select a menu item, windwos usually sends several different messages to the window process. In most cases, your program can ignore most of the messages. You only need to pass them to defwindowproc.
- Wm_initmenu message, wparam: Main menu handle, lparam: 0; this message is sent when a menu is to be activated. When you click an item on the menu bar or use the menu key to select. This message allows the application to modify the menu before display. You can modify the top menu at this time.
- Wm_menuselect: loword (wparam): selected item: menu ID or pop-up menu handle. Hiword (wparam): select the label; lparam: contains the menu handle of the selected item. When you select a menu item, the message is sent to the window to which the menu belongs. This message is a menu tracking message. wparam tells you which item is selected currently. The "select flag" in the wparam high text can be a combination of the following: mf_grayed, mf_disabled, mf_checked, mf_bitmap, mf_popup, mf_help, mf_sysmenu, and mf_mouseselect. if you need to change the content of the window customer area according to the menu item, you can use this message.
- Wm_initmenupopup: wparam: pop-up menu handle; loword (lparam): pop-up menu index; hiword (lparam): The system menu is 1, and the others are 0; this message is important if you want to enable or disable a menu item before the pop-up menu is displayed.
- Wm_command: loword (wparam): menu ID; hiwoed (wparam): 0; lparam: 0; if the message is generated by the subwindow control, if the button is generated, loword (wparam ): control ID hiword (lparam): Notification code lparam: subwindow handle. If the message is generated by a subwindow or shortcut key, the notification code is 1, generated by the menu, and the notification code is 0. The parameter can be used to identify whether the message comes from a control, shortcut key, or menu.
- Wm_syscommand: similar to a wm_command message, wm_syscommand indicates that the user selects a start menu item from the system menu or selects the maximize, minimize, and close button. Loword (wparam): menu ID hiwoed (wparam): 0 lparam: 0; if a message is generated by mouse, loword (wparam) and hiword (lparam) the cursor position X and Y are included. Wparam is used for pre-defined Windows System menu items. SC _close, SC _maximize, SC _minimize, SC _move, and so on.
For existing menus, you can use getsystemmenu, getmenu, and getsubmenu to obtain the menu handle. You can use the following API to change the menu. Appendmenu, deletemenu, insertmenu, modifymenu, removemenu, and so on.
Instance resources: menus and shortcut keys
//////////////////////////////////////// //////////////////////////////////////// /Menuidm_main menubegin popup "file (& F) "Begin menuitem" open the file (& O )... ", idm_open menuitem" close the file (& C )... ", idm_option menuitem separator menuitem" exit (& X) ", idm_exit end popup" View (& V) "begin menuitem" font (& F )... \ talt + F ", idm_setfont menuitem" background color (& B )... \ tctrl + ALT + B ", idm_setcolor menuitem separator menuitem" disabled menu item ", idm_ina CT, inactive menuitem "dimmed menu items", idm_gray, grayed menuitem separator menuitem "Big icon (& G)", idm_big menuitem "small icon (& M )", idm_small menuitem "list (& L)", idm_list menuitem "details (& D)", idm_detail menuitem separator popup "toolbar (& T) "Begin menuitem" Standard button (& S) ", idm_toolbar menuitem" text tag (& C) ", idm_toolbartext menuitem" command bar (& I )", idm_inputbar end menuitem "status bar (& U)", idm_statusbar end popup "Help (& H)" begin menuit Em "help topic (& H) \ TF1", idm_help menuitem separator menuitem "about (& )... ", idm_about endend ////////////////////////////////////// //////////////////////////////////////// /// accelerator // acceleration key, in the resource view, add the shortcut key resource idc_sdkpopupmenu acceleratorsbegin "/", idm_about, ASCII, ALT, noinvert "? ", Idm_about, ASCII, ALT, noinvert vk_f1, idm_help, primary key, noinvert" B ", idm_setcolor, primary key, control, ALT, noinvert" F ", idm_setfont, primary key, ALT
Key code
Resouces. h
#define IDC_MYICON 2#define IDD_SDKPOPUPMENU_DIALOG 102#define IDS_APP_TITLE 103#define IDD_ABOUTBOX 103#define IDI_SDKPOPUPMENU 107#define IDI_SMALL 108#define IDC_SDKPOPUPMENU 109#define IDR_MAINFRAME 128#define IDM_MAIN 0x2000#define IDA_MAIN 0x2000#define IDM_OPEN 0x4101#define IDM_OPTION 0x4102#define IDM_EXIT 0x4103#define IDM_SETFONT 0x4201#define IDM_SETCOLOR 0x4202#define IDM_INACT 0x4203#define IDM_GRAY 0x4204#define IDM_BIG 0x4205#define IDM_SMALL 0x4206#define IDM_LIST 0x4207#define IDM_DETAIL 0x4208#define IDM_TOOLBAR 0x4209#define IDM_TOOLBARTEXT 0x4210#define IDM_INPUTBAR 0x4211#define IDM_STATUSBAR 0x4212#define IDM_HELP 0x4301#define IDM_ABOUT 0x4302
Tchar szclassname [] = l "menu example"; tchar szcaptionmain [] = l "menu"; tchar szmenuhelp [] = l "help topic (& H )"; tchar szmenuabout [] = l "about this program (& )... "; hwnd g_hwinmain; hmenu g_hmenu; hmenu g_hsubmenu; void displaymenuitem (DWORD dwcommandid) {tchar szbuffer [256] = {0}; wsprintf (szbuffer, l" you have selected the menu command: % 08x ", dwcommandid); MessageBox (g_hwinmain, szbuffer, l" menu selection ", mb_ OK);} bool initinstance (hinstance, int ncmd Show) {hwnd; hinst = hinstance; // store the instance handle in the global variable g_hmenu = loadmenu (hinstance, makeintresource (idm_main); // load the menu resource, return menu handle g_hsubmenu = getsubmenu (g_hmenu, 1); // obtain the 2nd sub menu of the main menu, used as the shortcut menu hwnd = createwindow (szwindowclass, sztitle, ws_overlappedwindow, cw_usedefault, 0, cw_usedefault, 0, null, g_hmenu, hinstance, null); // put the menu in the window g_hwinmain = hwnd; If (! Hwnd) {return false;} showwindow (hwnd, ncmdshow); updatewindow (hwnd); Return true;} lresult callback wndproc (hwnd, uint message, wparam, lparam) {int wmid, wmevent; paintstruct pS; HDC; point stpos; hmenu hsysmenu; Switch (Message) {Case wm_create: hsysmenu = getsystemmenu (hwnd, false ); // obtain the system menu handle appendmenu (hsysmenu, mf_separator, 0, null); // appendmenu (hsysmenu, 0, idm_help, SZ Menuhelp); // append the menu item help to the System Menu appendmenu (hsysmenu, 0, idm_about, szmenuabout); // append the menu item about to the break in the System menu; case wm_command: // message displaymenuitem (wparam) of the user menu item command; // display dialog box // identify a single item (single item, double choice) if (loword (wparam) = idm_exit) {destroywindow (hwnd);} else if (loword (wparam)> = idm_toolbar & loword (wparam) <= idm_statusbar) // standard button, command bar, text label {uint ustate = getmenustate (g_hmenu, loword (wparam), mf_bycommand); If (US Tate = mf_checked) ustate = mf_unchecked; elseustate = mf_checked; checkmenuitem (g_hmenu, loword (wparam), ustate ); // check or undo the menu entry of the specified menu} else if (loword (wparam)> = idm_big & loword (wparam) <= idm_detail) // large and small single-choice icons, list, details {checkmenuradioitem (g_hmenu, idm_big, idm_detail, loword (wparam), mf_bycommand); // one of the multiple choice [idm_big, idm_detail]} // checkmenuitem, checkmenuradioitem is used to change the menu item status. The menu item status can be obtained through the getmenustate function. Break; Case wm_syscommand: // Command Message of the System menu item if (loword (wparam) = idm_help | loword (wparam) = idm_about) // help appended to the system menu, displaymenuitem (wparam) of the about menu item; elsereturn defwindowproc (hwnd, message, wparam, lparam); break; Case wm_rbuttondown: getcursorpos (& stpos); // obtain the cursor position trackpopupmenu (g_hsubmenu, tpm_leftalign, stpos. x, stpos. y, null, hwnd, null); // display the shortcut menu at the specified position and track the menu option break; Case wm_paint: HDC = beginpaint (hwnd, & PS ); endpaint (hwnd, & PS); break; Case wm_destroy: postquitmessage (0); break; default: Return defwindowproc (hwnd, message, wparam, lparam);} return 0 ;}