1 #include <windows.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "resource.h"
5 
6 LRESULT WINAPI MainWndProc(HWND, UINT, WPARAM, LPARAM);
7 
8 int WINAPI
9 WinMain(HINSTANCE hInstance,
10 	HINSTANCE hPrevInstance,
11 	LPSTR lpszCmdLine,
12 	int nCmdShow)
13 {
14   WNDCLASS wc;
15   MSG msg;
16   HWND hWnd;
17 
18   wc.lpszClassName = "MenuTestClass";
19   wc.lpfnWndProc = MainWndProc;
20   wc.style = CS_VREDRAW | CS_HREDRAW;
21   wc.hInstance = hInstance;
22   wc.hIcon = LoadIcon(NULL, (LPCTSTR)IDI_APPLICATION);
23   wc.hCursor = LoadCursor(NULL, (LPCTSTR)IDC_ARROW);
24   wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
25   wc.lpszMenuName = (LPCTSTR)IDM_MAINMENU;
26   wc.cbClsExtra = 0;
27   wc.cbWndExtra = 0;
28   if (RegisterClass(&wc) == 0)
29     {
30       fprintf(stderr, "RegisterClass failed (last error 0x%lX)\n",
31 	      GetLastError());
32       return(1);
33     }
34 
35   hWnd = CreateWindow("MenuTestClass",
36 		      "PopupMenu Test",
37 		      WS_OVERLAPPEDWINDOW | WS_VISIBLE,
38 		      0,
39 		      0,
40 		      CW_USEDEFAULT,
41 		      CW_USEDEFAULT,
42 		      NULL,
43 		      NULL,
44 		      hInstance,
45 		      NULL);
46   if (hWnd == NULL)
47     {
48       fprintf(stderr, "CreateWindow failed (last error 0x%lX)\n",
49 	      GetLastError());
50       return(1);
51     }
52 
53   while(GetMessage(&msg, NULL, 0, 0))
54   {
55     TranslateMessage(&msg);
56     DispatchMessage(&msg);
57   }
58 
59   return msg.wParam;
60 }
61 
62 LRESULT CALLBACK MainWndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
63 {
64   switch(msg)
65   {
66     case WM_COMMAND:
67     {
68       switch(LOWORD(wParam))
69       {
70         case IDM_EXIT:
71           PostQuitMessage(0);
72           break;
73       }
74       break;
75     }
76     case WM_RBUTTONUP:
77     {
78       POINT pos;
79       HMENU Menu;
80 
81       pos.x = LOWORD(lParam);
82       pos.y = HIWORD(lParam);
83       ClientToScreen(hWnd, &pos);
84 
85       if((Menu = GetMenu(hWnd)) && (Menu = GetSubMenu(Menu, 1)))
86       {
87         TrackPopupMenu(Menu, 0, pos.x, pos.y, 0, hWnd, NULL);
88       }
89       break;
90     }
91     case WM_DESTROY:
92     {
93       PostQuitMessage(0);
94       break;
95     }
96     default:
97     {
98       return DefWindowProc(hWnd, msg, wParam, lParam);
99     }
100   }
101   return 0;
102 }
103