EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

#Previous question #Index of VC++/WIN32/MFC Questions #Next question

Popup menu or context menus are those which generally appear when user right clicks on graphical widgets. These menus are aligned to the context of the child windows like edit menu for textbox, and graphical toolbox for graphic editors.

TrackPopupMenu() Win32 API is used to display a context menu in a specific position of the screen. This API takes a handle to a popup menu. We can load an popup menu from existing resource or we can create popup menu dynamically with CreatePopupMenu() Win32 call.

BOOL TrackPopupMenu(
        HMENU hMenu,         /* Handle to a popup menu */
        UINT uFlags,         /* flags for display menu */
        int x,               /* Absolute X co-ordinate in screen */
        int y,               /* Absolute Y co-ordinate in screen */
        int nReserved,       /* must be zero */
        HWND hWnd,           /* Handle to parent window */
        const RECT *prcRect  /* Optional*/
);
10  HMENU CreatePopupMenu(); /* Creates a new handle to popup menu */
11 

Context/Popup menu example using TrackPopupMenu()

Source Code

#include <windows.h>
#include <stdio.h>
/* Context Menu IDs */
#define IDM_CONTEXT_LINE   1000
#define IDM_CONTEXT_RECTAN 1001
#define IDM_CONTEXT_CIRCLE 1002
#define IDM_CONTEXT_HELP   1003
10  LRESULT CALLBACK WndProc(HWND hWnd,
11                           UINT message, 
12                           WPARAM wParam,
13                           LPARAM lParam);
14 
15  int APIENTRY WinMain(HINSTANCE hInstance,
16                       HINSTANCE hPrevInstance,
17                       LPSTR     lpCmdLine,
18                       int       nCmdShow)
19  {
20    WNDCLASS wc;
21    MSG msg;
22    HWND hWnd;
23    ZeroMemory(&wc, sizeof(WNDCLASS));
24 
25    wc.style           = CS_HREDRAW | CS_VREDRAW;
26    wc.lpfnWndProc     = (WNDPROC)WndProc;
27    wc.hInstance       = hInstance;
28    wc.hCursor         = LoadCursor(NULL, IDC_ARROW);
29    wc.hbrBackground   = (HBRUSH)(COLOR_BACKGROUND);
30    wc.lpszClassName   = (LPCTSTR)"MyWin32Popup";
31 
32 
33    RegisterClass(&wc);
34 
35     hWnd = CreateWindow("MyWin32Popup", "Popup/Context Menu", WS_OVERLAPPEDWINDOW,
36        CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
37 
38    ShowWindow(hWnd, nCmdShow);
39    UpdateWindow(hWnd);
40    while (GetMessage(&msg, NULL, 0, 0)) 
41    {
42      DispatchMessage(&msg);
43    }
44    return 0;
45  }
46 
47  LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
48  {
49    char * context_menu[] = { "Line", "Rectangle", "Circle", "Help"};
50    char msg[50];
51    switch (message) 
52    {
53      case WM_COMMAND:
54      {
55        if(wParam >= IDM_CONTEXT_LINE && wParam <= IDM_CONTEXT_HELP) {
56           sprintf(msg, "Clicked on %s", context_menu[wParam -IDM_CONTEXT_LINE]);
57           MessageBox(hWnd, msg, "Popup Menu", MB_OK);
58        }
59        DefWindowProc(hWnd, message, wParam, lParam);
60        break;
61      }
62      case WM_RBUTTONDOWN:
63      {
64        POINT pt;
65        HMENU hMenuPopup = CreatePopupMenu();
66        AppendMenu(hMenuPopup, MF_STRING, IDM_CONTEXT_LINE, "Line");
67        AppendMenu(hMenuPopup, MF_STRING, IDM_CONTEXT_RECTAN, "Rectangle");
68        AppendMenu(hMenuPopup, MF_STRING, IDM_CONTEXT_CIRCLE, "Circle");
69        AppendMenu(hMenuPopup, MF_SEPARATOR, 0, NULL);
70        AppendMenu(hMenuPopup, MF_STRING, IDM_CONTEXT_HELP, "Help");
71        pt.x= GET_X_LPARAM(lParam);
72        pt.y= GET_Y_LPARAM(lParam);
73        ClientToScreen(hWnd, &pt);
74        TrackPopupMenu(hMenuPopup, 
75                       TPM_LEFTALIGN | TPM_RIGHTBUTTON, 
76                       pt.x, pt.y, 0, hWnd, NULL);
77        DestroyMenu(hMenuPopup);
78        break;
79      }
80      case WM_DESTROY:
81        PostQuitMessage(0);
82        break;
83      default:
84        return DefWindowProc(hWnd, message, wParam, lParam);
85     }
86     return 0;
87  }

You have viewed 1 page out of 109. Your VC++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

#Programming Windows, Fifth Edition (Developer Reference)
#Programming Applications for Microsoft Windows (Microsoft Programming Series)
#Programming Windows with MFC, Second Edition
#Visual C++ Programming
#Programming Microsoft Visual C++, Fifth Edition

* #1 webmaster Sat 26 Apr/2014 18:10:07(GMT)  Like 0 Unlike 0

Dear Users,

We are pleased to inform you that a forum/blog has been incorporated with www.mybestnotes.co.in. You are welcomed to add your comments, requests, codes, solutions and feedback to it. Please login(if already a member) or signup(for free) to avail to this facility.

Regards,
Webmaster

Your message goes here:

Name:Guest
Email:anonymous@unknown.com
My Post:*
Secutiry Code: ******  *
Preview this compose before posting this in discussion forum.
 
Note:
  1. Use [Sxx] code to show smiles. Example [S02]
  2. Use [URL ], example [URL http://www.google.com/]
  3. To display code or un formatted text use [CODE] [/CODE]. Example: [CODE] printf("Hello world"); [/CODE]
 [S01]   [S02]   [S03]   [S04]   [S05]   [S06]   [S07]   [S08] 
 [S09]   [S10]   [S11]   [S12]   [S13]   [S14]   [S15]   [S16] 
 [S17]   [S18]   [S19]   [S20]   [S21]   [S22]   [S23]   [S24] 

Note: Only members are allowed, * fields are mandatory.