Into the world of Windows programming-----Message Processing functions (4)

Source: Internet
Author: User

A right-click menu
1 Right-click menu
When the right mouse button is clicked in the window, the menu pops up.
2 use of the right-click menu
2.1 Creating a Menu
CreatePopupMenu
2.2 Menu Additions
AppendMenu
2.3 Menu Display,

BOOL TrackPopupMenu (    HMENU HMENU,//menu handle UINT uflags,//Display the    way  int x,//menu x screen coordinates  int y,//Menu Y-screen coordinates  int nreserved,//reserved, must be 0  HWND hwnd,//Handle the window handle of the menu command  CONST RECT *prcrect); Ignore

2.4 Menu command handling

Wm_command
2.5 Where to use the right-click menu
2.5.1 Wm_rbuttonup News
In Wm_rbuttonup, add menu creation and display,
Right-click Message coordinates, converted to screen coordinates used.
ClientToScreen.
2.5.2 Wm_contextmenu News
The message that is used to display the right-click menu.
WPARAM-corresponding window handle when right-click Lift
LPARAM-the screen coordinate position of the mouse when the right button is lifted
LoWord (LParam)-X screen coordinates
HiWord (LParam)-y screen coordinates
2.5.3 Wm_rbuttonup and Wm_contextmenu contrast
1) coordinate system, Wm_rbuttonup client area coordinates, wm_contextmenu screen coordinates
2) First Wm_rbuttonup message, after Wm_contextmenu message

/* File:winPopMenu.cpp * Auth:sjin * date:20140706 * Mail: [email protected] */#include &LT;WI ndows.h> #include <stdio.h>hinstance g_hinst = null;void Onrbuttonup (HWND hwnd, UINT nmsg, WPARAM wPa    RAM, LPARAM LPARAM) {//Create pop-up menu HMENU Hpopmenu = CreatePopupMenu ();    Add menu item AppendMenu (Hpopmenu, mf_string, 1001, "Test 1");    AppendMenu (Hpopmenu, Mf_separator, 0, NULL);    AppendMenu (Hpopmenu, mf_string, 1002, "exit");    Gets the menu position, point, "{0}";    Point.x = LoWord (LParam);    Point.y = HiWord (LParam);    ClientToScreen (HWnd, &point); Display Menu TrackPopupMenu (Hpopmenu, Tpm_leftalign, Point.x, Point.y, 0, HWnd, NULL);} void OnContextMenu (HWND hwnd, UINT nmsg, WPARAM WPARAM, LPARAM LPARAM) {//Create pop-up menu HMENU Hpopmenu = createpopup    Menu ();    Add menu item AppendMenu (Hpopmenu, mf_string, 1001, "Test 2");    AppendMenu (Hpopmenu, Mf_separator, 0, NULL); AppendMenu (Hpopmenu, mf_string, 1002, "Exit");    coordinates get int nX = LOWORD (LParam);    int NY = HiWord (LParam);    Display Menu TrackPopupMenu (Hpopmenu, Tpm_leftalign, NX, NY, 0, HWnd, NULL); Delete Menu DestroyMenu (hpopmenu);}    void OnCommand (HWND hwnd, UINT nmsg, WPARAM WPARAM, LPARAM LPARAM) {int ncmdid = LOWORD (WPARAM);        Switch (ncmdid) {case 1001:messagebox (NULL, "Hello popmenu", "Popmenu", MB_OK);    Break        Case 1002:postquitmessage (0);    Break                          }}lresult CALLBACK WndProc (HWND hwnd, UINT nmsg, WPARAM WPARAM, LPARAM LPARAM) {switch (nmsg) {case wm_rbuttonup://onrbuttonup (hWnd, nmsg, WPa        RAM, LParam);    Break        Case Wm_contextmenu:oncontextmenu (HWnd, Nmsg, WParam, LParam);    Break        Case Wm_command:oncommand (HWnd, Nmsg, WParam, LParam);    Break    Case Wm_destroy:postquitmessage (0);    return 0; } return DefWindowProc (HWnd, Nmsg, WParam, LParam);}    BOOL Registerwnd (LPSTR pszclassname) {wndclassex WCE = {0};    wce.cbsize = sizeof (WCE);    Wce.cbclsextra = 0;    Wce.cbwndextra = 0;    Wce.hbrbackground = Hbrush (Color_window);    Wce.hcursor = NULL;    Wce.hicon = NULL;    WCE.HICONSM = NULL;    Wce.hinstance = G_hinst;    Wce.lpfnwndproc = WndProc;    Wce.lpszclassname = Pszclassname;    Wce.lpszmenuname = NULL;    Wce.style = Cs_hredraw|cs_vredraw;    ATOM Natom = RegisterClassEx (&AMP;WCE);    if (0 = = Natom) {return FALSE; } return TRUE; HWND Createwnd (LPSTR pszclassname) {hwnd hwnd = CREATEWINDOWEX (0, Pszclassname, "Mywnd", Ws_overlappe    Dwindow, Cw_usedefault, Cw_usedefault, Cw_usedefault, cw_usedefault, NULL, NULL, G_HINST, NULL); return hWnd;}    void Displaywnd (HWND hwnd) {ShowWindow (hwnd, sw_show); UpdateWindow (hWnd);}    void Message () {msg msg = {0};        while (GetMessage (&msg, NULL, 0, 0)) {translatemessage (&msg);    DispatchMessage (&AMP;MSG); }}int apientry WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR LpC    mdline, int ncmdshow) {g_hinst = hinstance;    Registerwnd ("Mywnd");    HWND hwnd = Createwnd ("Mywnd");    Displaywnd (HWND);    Message (); return 0;}

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.