5Windows_system_right_menu右键菜单

Posted 养老保险年审

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了5Windows_system_right_menu右键菜单相关的知识,希望对你有一定的参考价值。

5Windows_system_right_menu右键菜单
  1. // 5Windows_system_right_menu右键菜单.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "5Windows_system_right_menu右键菜单.h"
  5. // 4Windows_System_menu.cpp : 定义应用程序的入口点。
  6. //
  7. #include <iostream>
  8. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  9. HINSTANCE g_hInst = NULL; //窗口句柄
  10. HANDLE g_hStdout = NULL; //控制台句柄
  11. CHAR szText[256] = { 0 };
  12. void OnRButtonUp(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  13. {
  14. //获取菜单句柄(建立菜单)
  15. HMENU hPopMenu = CreatePopupMenu();
  16. //增加菜单
  17. AppendMenu(hPopMenu, MF_STRING, 1001, "复制");
  18. AppendMenu(hPopMenu, MF_STRING, 1002, "粘贴");
  19. AppendMenu(hPopMenu, MF_STRING, 1003, "剪切");
  20. AppendMenu(hPopMenu, MF_SEPARATOR, 1004, NULL);
  21. AppendMenu(hPopMenu, MF_STRING, 1005, "退出");
  22. //显示菜单
  23. POINT point = { 0 }; //转换时需要POINT数据结构
  24. point.x = LOWORD(lParam); //客户区坐标
  25. point.y = HIWORD(lParam);
  26. //转换成屏幕坐标
  27. ClientToScreen(hWnd, &point);
  28. TrackPopupMenu(hPopMenu, TPM_LEFTALIGN, point.x, point.y, 0, hWnd, NULL);
  29. }
  30. //OnCommand
  31. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  32. {
  33. int CMDId = LOWORD(wParam);
  34. switch (CMDId)
  35. {
  36. case 1001:
  37. //TD//...
  38. PrintLog("复制已完成\n");
  39. break;
  40. case 1002:
  41. //TD//...
  42. PrintLog("粘贴已完成\n");
  43. break;
  44. case 1003:
  45. //TD//...
  46. PrintLog("剪切已完成\n");
  47. break;
  48. case 1005:
  49. //TD//...
  50. PostQuitMessage(0);
  51. break;
  52. }
  53. //打印日志
  54. sprintf_s(szText, 256, "OnCommand ID:%d\n", CMDId);
  55. PrintLog(szText);
  56. }
  57. //OnConTextMenu,右键菜单专用函数
  58. void OnConTextMenu(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  59. {
  60. //获取菜单句柄(建立菜单)
  61. HMENU hPopMenu = CreatePopupMenu();
  62. //增加菜单
  63. AppendMenu(hPopMenu, MF_STRING, 1006, "一式");
  64. AppendMenu(hPopMenu, MF_STRING, 1007, "二式");
  65. AppendMenu(hPopMenu, MF_STRING, 1008, "三式");
  66. AppendMenu(hPopMenu, MF_SEPARATOR, 1009, NULL);
  67. AppendMenu(hPopMenu, MF_STRING, 1010, "极招");
  68. //显示菜单
  69. //POINT point = { 0 }; //转换时需要POINT数据结构
  70. //point.x = LOWORD(lParam); //客户区坐标
  71. //point.y = HIWORD(lParam);
  72. ////转换成屏幕坐标
  73. //ClientToScreen(hWnd, &point);
  74. int nX = LOWORD(lParam);
  75. int nY = HIWORD(lParam);
  76. TrackPopupMenu(hPopMenu, TPM_LEFTALIGN, nX, nY, 0, hWnd, NULL);
  77. //删除菜单
  78. DestroyMenu(hPopMenu);
  79. }
  80. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  81. {
  82. switch (nMsg)
  83. {
  84. case WM_RBUTTONUP:
  85. //OnRButtonUp(hWnd, nMsg, wParam, lParam);
  86. break;
  87. case WM_CONTEXTMENU:
  88. OnConTextMenu(hWnd, nMsg, wParam, lParam);
  89. break;
  90. case WM_COMMAND:
  91. OnCommand(hWnd, nMsg, wParam, lParam);
  92. break;
  93. case WM_DESTROY:
  94. PostQuitMessage(0);
  95. break;
  96. }
  97. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  98. }
  99. BOOL RegisterWnd(LPSTR pszClassName)
  100. {
  101. WNDCLASSEX wce = { 0 };
  102. wce.cbSize = sizeof(wce);
  103. wce.cbClsExtra = 0;
  104. wce.cbWndExtra = 0;
  105. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  106. wce.hCursor = NULL;
  107. wce.hIcon = NULL;
  108. wce.hIconSm = NULL;
  109. wce.hInstance = g_hInst;
  110. wce.lpfnWndProc = WndProc;
  111. wce.lpszClassName = pszClassName;
  112. wce.lpszMenuName = NULL;
  113. wce.style = CS_HREDRAW | CS_VREDRAW;
  114. ATOM atom = RegisterClassEx(&wce);
  115. if (atom == NULL)
  116. {
  117. return FALSE;
  118. }
  119. else
  120. {
  121. return TRUE;
  122. }
  123. }
  124. HWND CreateWnd(LPSTR pszClassName)
  125. {
  126. HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  127. CW_USEDEFAULT, NULL, NULL, g_hInst, 0);
  128. return hWnd;
  129. }
  130. void ShowWnd(HWND hWnd)
  131. {
  132. ShowWindow(hWnd, SW_SHOW);
  133. UpdateWindow(hWnd);
  134. }
  135. void Msg()
  136. {
  137. MSG msg = { 0 };
  138. while (GetMessage(&msg, NULL, 0, 0))
  139. {
  140. TranslateMessage(&msg);
  141. DispatchMessage(&msg);
  142. }
  143. }
  144. void ConsoleWnd()
  145. {
  146. AllocConsole();
  147. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  148. CHAR szText[] = "Debug start:\n";
  149. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  150. }
  151. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  152. {
  153. g_hInst = hInstance;
  154. ConsoleWnd();
  155. RegisterWnd("oooo");
  156. HWND hWnd = CreateWnd("oooo");
  157. ShowWnd(hWnd);
  158. Msg();
  159. return 0;
  160. }





以上是关于5Windows_system_right_menu右键菜单的主要内容,如果未能解决你的问题,请参考以下文章