3Windows_menu

  1. // 3Windows_menu.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "3Windows_menu.h"
  5. #include <iostream>
  6. HINSTANCE g_hInst;
  7. HANDLE g_hStdout = NULL;
  8. BOOL g_bCheckCut = FALSE; //是看Cut项有没有被选择上
  9. //宏定义输入函数
  10. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL);
  11. void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  12. {
  13. PrintLog("OnCreate!\n");
  14. HMENU hMainMenu = CreateMenu();
  15. //增加菜单项,用AppendMenu
  16. //下面这三句如果不注释掉会多产生一块母菜单
  17. //AppendMenu(hMainMenu, MF_STRING, 2001, "文件");
  18. //AppendMenu(hMainMenu, MF_STRING, 2002, "编辑");
  19. //AppendMenu(hMainMenu, MF_STRING, 2003, "帮助");
  20. //增加弹出菜单项抽象
  21. HMENU hFileMenu = CreatePopupMenu();
  22. HMENU hEditMenu = CreatePopupMenu();
  23. HMENU hHelpMenu = CreatePopupMenu();
  24. //增加弹出菜单项实体
  25. //新建菜单
  26. AppendMenu(hFileMenu, MF_STRING|MF_CHECKED, 3001, "新建(&N)"); //&N 加快捷键N
  27. AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
  28. AppendMenu(hFileMenu, MF_STRING, 3002, "退出(&X)"); //Disabled无法被选择
  29. //编辑菜单
  30. AppendMenu(hEditMenu, MF_STRING, 4001, "剪切(&T)");
  31. AppendMenu(hEditMenu, MF_STRING, 4002, "复制(&C)");
  32. AppendMenu(hEditMenu, MF_STRING, 4003, "粘贴(&P)");
  33. //帮助
  34. AppendMenu(hHelpMenu, MF_STRING, 5001, "关于(&A)");
  35. AppendMenu(hHelpMenu, MF_STRING | MF_DISABLED, 5002, "注册(&R)");
  36. AppendMenu(hHelpMenu, MF_STRING, 5003, "帮助(&H)");
  37. //增加弹出方法
  38. AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT)hFileMenu, "文件(&F)");//文件弹出
  39. AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT)hEditMenu, "编辑(&E)"); //编辑弹出
  40. AppendMenu(hMainMenu, MF_STRING | MF_POPUP, (UINT)hHelpMenu, "帮助(&H)"); //帮助弹出
  41. //显示菜单
  42. SetMenu(hWnd, hMainMenu);
  43. //SetMenu(hWnd, hFileMenu); //err
  44. }
  45. //命令处理
  46. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  47. {
  48. CHAR szText[256] = { 0 };
  49. sprintf_s(szText, 256, "OnCommand ID:%d\n", LOWORD(wParam));
  50. PrintLog(szText);
  51. switch (LOWORD(wParam))
  52. {
  53. case 3002:
  54. PostQuitMessage(0);
  55. break;
  56. case 3001:
  57. PrintLog("新建菜单!\n");
  58. break;
  59. case 4001: //取返一下,来改变剪切前的勾
  60. g_bCheckCut = !g_bCheckCut;
  61. break;
  62. }
  63. }
  64. void OnInitMenuPopup(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  65. {
  66. CHAR szText[256] = { 0 };
  67. sprintf_s(szText, 256, "OnInitMenuPopup: WPARAM=%08X, LPARAM=%08X\n", wParam, lParam);//打成十六进制
  68. PrintLog(szText);
  69. //给剪切前加对勾
  70. if (TRUE == g_bCheckCut)
  71. {
  72. HMENU hMenu = (HMENU)wParam; //传过来的参数就是HMENU句柄
  73. CheckMenuItem(hMenu, 4001, MF_CHECKED | MF_BYCOMMAND);
  74. }
  75. else
  76. {
  77. HMENU hMenu = (HMENU)wParam; //传过来的参数就是HMENU句柄
  78. CheckMenuItem(hMenu, 4001, MF_UNCHECKED | MF_BYCOMMAND);
  79. }
  80. }
  81. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  82. {
  83. switch (nMsg)
  84. {
  85. case WM_CREATE:
  86. OnCreate(hWnd, nMsg, wParam, lParam);
  87. break;
  88. case WM_COMMAND:
  89. OnCommand(hWnd, nMsg, wParam, lParam);
  90. break;
  91. case WM_INITMENUPOPUP:
  92. OnInitMenuPopup(hWnd, nMsg, wParam, lParam);
  93. break;
  94. case WM_DESTROY:
  95. PostQuitMessage(0);
  96. break;
  97. }
  98. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  99. }
  100. BOOL RegisterWnd(LPSTR pszClassName)
  101. {
  102. WNDCLASSEX wce = { 0 };
  103. wce.cbSize = sizeof(wce);
  104. wce.cbClsExtra = 0;
  105. wce.cbWndExtra = 0;
  106. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  107. wce.hCursor = NULL;
  108. wce.hIcon = NULL;
  109. wce.hIconSm = NULL;
  110. wce.hInstance = g_hInst;
  111. wce.lpfnWndProc = WndProc;
  112. wce.lpszClassName = pszClassName;
  113. wce.lpszMenuName = NULL;
  114. wce.style = CS_VREDRAW | CS_HREDRAW;
  115. ATOM aTom = RegisterClassEx(&wce);
  116. if (aTom == 0)
  117. {
  118. return FALSE;
  119. }
  120. else
  121. {
  122. return TRUE;
  123. }
  124. }
  125. HWND CreateWnd(LPSTR pszClassName)
  126. {
  127. HWND hWnd = CreateWindowEx(0, pszClassName, "ttttt", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  128. NULL, NULL, g_hInst, NULL);
  129. return hWnd;
  130. }
  131. void showWnd(HWND hWnd)
  132. {
  133. ShowWindow(hWnd, SW_SHOW);
  134. UpdateWindow(hWnd);
  135. }
  136. void msg()
  137. {
  138. MSG msg = { 0 };
  139. while (GetMessage(&msg, NULL, 0, 0))
  140. {
  141. TranslateMessage(&msg);
  142. DispatchMessage(&msg);
  143. }
  144. }
  145. //控制台
  146. void ConsoleWnd()
  147. {
  148. AllocConsole();
  149. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  150. CHAR szText[] = "Debug start:\n";
  151. PrintLog(szText);
  152. }
  153. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  154. {
  155. g_hInst = hInstance;
  156. ConsoleWnd();
  157. RegisterWnd("OOOO");
  158. HWND hWnd = CreateWnd("OOOO");
  159. showWnd(hWnd);
  160. msg();
  161. return 0;
  162. }

来自为知笔记(Wiz)

时间: 2024-10-27 06:56:05

3Windows_menu的相关文章