Getting Started with Win32 development (6): Creating a Right button menu

Source: Internet
Author: User
Tags header resource win32

shortcut menu, easy to understand, is the right menu, when we click in a region right mouse button, will pop up some menu items. This type of menu is ubiquitous, we right-click on the desktop and a menu pops up.

The advantage of a right-click menu is that it is often associated with a UI element that we are working on. For example, we are using text boxes to enter text, we right-click in the text box, we see that there may be "copy" "Empty" "Select All" options, so the right menu is also known as the "contextual menu" (context menu) ".

In general, you create and use a shortcut menu by following these steps:

1, use the resource Editor to create a menu.

2, when we press the right mouse button on the window, when the system processing Wm_rbuttonup will send a WM_CONTEXTMENU message to our application, we respond to this message to decide whether pop-up menu.

3, the Calculation menu pop-up position, generally in our mouse pointer to the lower right, the coordinates are based on the screen, not the window.

4, call the TrackPopupMenu function to display the shortcut menu.

5, because this kind of menu is not belong to a window, its memory resources will not be reclaimed when the window destroys, therefore, after TrackPopupMenu returns to call DestroyMenu to destroy the menu resources, releases the memory.

OK, the basic idea has, we follow this train of thought to try, see can realize a right key menu.

First, use the resource Editor to create a menu, because our pop-up menu usually shows only a series of items, and is not a menu header, unlike a menu bar. So we made the menu this way:

The shortcut menu will only show the part I circled with the brush, and the "ABC" above is not displayed, so you can leave it blank or enter some content randomly.

Then you set the ID for each menu item, and the resource editor sometimes produces a bunch of unused ID macros that we can delete manually, and of course it doesn't affect the program's compilation, because the header file is not compiled. When we compile, we just compile the. cpp file.

The next step is to capture the Wm_contextmenu message. Displays the menu.

Case Wm_contextmenu:     
    {     
        //Load menu resource     
        hmenu hroot = LoadMenu ((hinstance) Getwindowlongptr (hwnd, gwlp_hinstance ), 

Makeintresource (Idr_context));     
        if (hroot)     
        {     
            //Get first pop-up menu     
            hmenu hpop = GetSubMenu (hroot,0);     
            Get the right mouse click is the coordinate     
            int px = Get_x_lparam (LPARAM);     
            int py = Get_y_lparam (LPARAM);     
            Show shortcut menu     
            trackpopupmenu (hpop,     
                tpm_leftalign | Tpm_topalign | Tpm_rightbutton,     
                px,     
                py,     
                0,     
                (HWND) WParam,     
                NULL);     
            Destroy the menu resource     
            destroymenu (hroot) When you are done with it;     
    Break

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.