(reprint) Win32 SDK Programming Series--menu (shortcut menu)--Dynamic loading

Source: Internet
Author: User

Theory

To create a normal menu, follow these steps:

    • 1) Create a menu such as: CreateMenu
    • 2) Add menu items such as: AppendMenu
    • 3) Load Menu LoadMenu
    • 4) Set menu to Window SetMenu
    • 5) Handling Menu Responses
    • 6) Eliminate menu DestroyMenu

To create the right-click menu, follow these steps:

    • 1) Create a menu such as: CreateMenu
    • 2) Add menu items such as: AppendMenu
    • 3) Use TrackPopupMenu to load the menu.
    • 4) Handling Menu Responses
    • 5) Eliminate Menu DestroyMenu

Of course, if you choose to edit on an existing menu, it is also possible to omit some of these steps. The example in this section is to add your own defined menu items to the existing menu. For the right-click menu, you can also select a column from an existing menu as a popup menu. After using the menu, to deal with the problem of clearing, and the window is connected to the menu handle when the window is destroyed by the Windows automatically released, do not need to release manually, but no window-linked menu is to be released by the program itself, by using the DestroyMenu function, For example, there is no connection to the window and only trackpopupmenu popup menu handle. In this case, we're using a window-associated menu handle, so we don't need to call DestroyMenu to eliminate the menu resource.

Menu message: When a user selects a menu item, Windwos typically sends several different messages to the window procedure. In most cases, your program can ignore most of the messages, just pass them to DefWindowProc.

  1. Wm_initmenu message, WParam: Main course single handle, lparam:0; This message is emitted when a menu is about to be activated. When the user clicks on the item on the menu bar or uses the menu key to select. This message allows the application to modify the menu before it is displayed. You can modify the top-level menu at this point.
  2. Wm_menuselect:loword (WParam): checked: Menu ID or pop-up menu handle. HiWord (WParam): Select the mark; LParam: The menu handle that contains the selected item.   When the user selects a menu item, the message is sent to the window to which the menu belongs. This message is a menu trace message, wparam tells you which item in the menu is currently selected, and the "select Flag" in WParam's high word can be a combination of these flags: 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 client area according to the menu item selection, then you can use this message.
  3. Wm_initmenupopup:wparam: Pop-up menu handle;   LoWord (LParam): Pop-up menu index;   HiWord (LParam): The system menu is 1, the other is 0; This message is important if you want to enable or disable menu items before the pop-up menu is displayed.
  4. Wm_command:loword (WParam): Menu ID;     Hiwoed (WParam): 0;  lparam:0; If this message is generated by child window control, such as button generation: LoWord (WParam): Control ID HiWord (lParam): Notification Code lParam: child window handle. If the message is generated by a child window or shortcut key, the notification code is 1, generated by the menu, and the notification code is 0. With parameters, you can distinguish whether the source of this message comes from a control, shortcut, or menu.
  5. Wm_syscommand: Similar to WM_COMMAND message, just wm_syscommand indicates that the user selects a boot menu item from the System menu or the user chooses to maximize, minimize, and close the button. LoWord (WParam): Menu ID hiwoed (wParam): 0 lparam:0; If the message is generated by mouse, LoWord (WParam) and HiWord (LParam) will contain the position of the mouse cursor, x, y screen coordinates. For Windows Pre-defined system menu items, denoted by wparam. Sc_close,sc_maximize,sc_minimize,sc_move and so on.

For an existing menu, we can use Getsystemmenu,getmenu, GetSubMenu to get the menu handle. When you need to change the menu, you can use the following API to make changes. AppendMenu, Deletemenu,insertmenu,modifymenu,removemenu and so on.

Instance resources: menus and shortcut keys
Menuidm_main menubegin POPUP "file                 (&f) "BEGIN MENUITEM" Open file (&o) ... ", Idm_open MENUITEM" close 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_inact, INACTIVE                     MENUITEM "grayed out menu item", Idm_gray, grayed MENUITEM SEPARATOR MENUITEM "large icon (&g)",                      Idm_big MENUITEM "small icon (&m)", Idm_small MENUITEM "list (&l)", Idm_list MENUITEM "Detailed information (&d)", Idm_detail MENUITEM SEPARATOR popu P "toolbar (&t)" BEGIN           MENUITEM "standard button (&s)", Idm_toolbar MENUITEM "text label (&C)", Idm_toolbartext MENUITEM "command bar (&i)", Idm_inputbar END MENUITEM "status bar (&u                ) ", Idm_statusbar END POPUP Help (&h)" BEGIN MENUITEM "Help topic (&H) \TF1", Idm_help MENUITEM SEPARATOR MENUITEM "About (&a) ...", Idm_about endend//////////// Acceleratoridc_sdkpopupmenu              Acceleratorsbegin "/", Idm_about, ASCII, ALT, Noinvert "?", Idm_about,           ASCII, ALT, Noinvert vk_f1, Idm_help, VIRTKEY, Noinvert "B", Idm_setcolor, VIRTKEY, CONTROL, Alt, Noinvert "F", Idm_setfont, VIRTKEY, Alt, Noinvertend
Key Code Resouces.h
#defineIdc_myicon 2#defineIdd_sdkpopupmenu_dialog 102#defineIds_app_title 103#defineIdd_aboutbox 103#defineIdi_sdkpopupmenu 107#defineIdi_small 108#defineIdc_sdkpopupmenu 109#defineIdr_mainframe 128#defineIdm_main 0x2000#defineIda_main 0x2000#defineIdm_open 0x4101#defineIdm_option 0x4102#defineIdm_exit 0x4103#defineIdm_setfont 0x4201#defineIdm_setcolor 0x4202#defineIdm_inact 0x4203#defineIdm_gray 0x4204#defineIdm_big 0x4205#defineIdm_small 0x4206#defineIdm_list 0x4207#defineIdm_detail 0x4208#defineIdm_toolbar 0x4209#defineIdm_toolbartext 0x4210#defineIdm_inputbar 0x4211#defineIdm_statusbar 0x4212#defineIdm_help 0x4301#defineIdm_about 0x4302TCHAR szclassname[]= L"Menu Example"; TCHAR szcaptionmain[]= L"Menu"; TCHAR szmenuhelp[]= L"Help Topics (&h)"; TCHAR szmenuabout[]= L"about this procedure (&a) ..."; HWND G_hwinmain; HMENU G_hmenu; HMENU G_hsubmenu;voidDisplaymenuitem (DWORD dwcommandid) {TCHAR szbuffer[ the] = {0}; wsprintf (Szbuffer, L"you have selected the menu command:%08x", Dwcommandid); MessageBox (G_hwinmain,szbuffer, L"Menu Selection", MB_OK);} BOOL InitInstance (hinstance hinstance,intncmdshow)   {HWND hwnd; HInst= HInstance;//storing an instance handle in a global variableG_hmenu = LoadMenu (Hinstance,makeintresource (Idm_main));//Load Menu resource, return menu handleG_hsubmenu = GetSubMenu (G_hmenu,1);//gets the 2nd submenu of the main menu, which is used as a shortcut menuHWnd =CreateWindow (Szwindowclass, SzTitle, Ws_overlappedwindow, Cw_usedefault,0, Cw_usedefault,0, NULL, G_hmenu, HINSTANCE, NULL);//Put the menu in the windowg_hwinmain=hWnd; if(!hWnd) {      returnFALSE;   } ShowWindow (HWnd, ncmdshow);   UpdateWindow (HWND); returnTRUE;} LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM) {intWmid, wmevent;    Paintstruct PS;    HDC hdc;    Point Stpos;    HMENU Hsysmenu; Switch(message) { CaseWm_create:hsysmenu= GetSystemMenu (Hwnd,false);//get system Menu handleAppendMenu (Hsysmenu,mf_separator,0, NULL);//attaching a split line to the system menuAppendMenu (Hsysmenu,0, idm_help,szmenuhelp);//Attach menu item Help to the system menuAppendMenu (Hsysmenu,0, idm_about,szmenuabout);//Attach the menu item about to the system menu         Break;  CaseWM_COMMAND://message for the User menu item commandDisplaymenuitem (WParam);//Show dialog Box//Identification of individual items (single, double selected)        if(LoWord (wParam) = =idm_exit)         {DestroyWindow (hWnd); }         Else if(LoWord (WParam) >= idm_toolbar && loword (wParam) <= Idm_statusbar)//Multi-Select toolbar: Standard button, command bar, text label{UINT ustate=getmenustate (G_hmenu,loword (WParam), Mf_bycommand); if(Ustate = =mf_checked) Ustate=mf_unchecked; Elseustate=mf_checked; Checkmenuitem (G_hmenu,loword (WParam), ustate);//to check or revoke a menu entry for a specified menu        }        Else if(LoWord (WParam) >= idm_big && loword (wParam) <= idm_detail)//single Large icons, small icons, lists, detailed information{Checkmenuradioitem (G_hmenu,idm_big,idm_detail,loword (WParam), mf_bycommand);//one of the radio [Idm_big,idm_detail]        }        //Checkmenuitem,checkmenuradioitem are used to change the status of the menu item respectively. The status of the menu item can be obtained through the getmenustate function.          Break;  CaseWm_syscommand://command messages for system menu items        if(LoWord (wParam) = = Idm_help | | LoWord (wParam) = = idm_about)//help,about menu item attached to the system menuDisplaymenuitem (WParam); Else            returnDefWindowProc (Hwnd,message,wparam,lparam);  Break;  CaseWm_rbuttondown:getcursorpos (&stpos);//Get cursor PositionTrackPopupMenu (G_hsubmenu,tpm_leftalign,stpos.x,stpos.y,null,hwnd,null);//Displays the shortcut menu at the specified location and tracks menu options         Break;  CaseWM_PAINT:HDC= BeginPaint (HWnd, &PS); EndPaint (HWnd,&PS);  Break;  CaseWm_destroy:postquitmessage (0);  Break; default:        returnDefWindowProc (hWnd, message, WParam, LParam); }    return 0;}

(reprint) Win32 SDK Programming Series--menu (shortcut menu)--Dynamic loading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.