4Windows_System_menu

  1. // 4Windows_System_menu.cpp : 定义应用程序的入口点。
  2. //
  3. #include "stdafx.h"
  4. #include "4Windows_System_menu.h"
  5. #include <iostream>
  6. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  7. HINSTANCE g_hInst = NULL; //窗口句柄
  8. HANDLE g_hStdout = NULL; //控制台句柄
  9. CHAR szText[256] = { 0 };
  10. void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  11. {
  12. HMENU hSysMenu = GetSystemMenu(hWnd, FALSE);
  13. //删除菜单项
  14. RemoveMenu(hSysMenu, 0, MF_BYPOSITION); //挨个删除
  15. //RemoveMenu(hSysMenu, 0, MF_BYPOSITION); //这个执行就会移动不了了
  16. //RemoveMenu(hSysMenu, 0, MF_BYPOSITION);
  17. //RemoveMenu(hSysMenu, 0, MF_BYPOSITION);
  18. //RemoveMenu(hSysMenu, 0, MF_BYPOSITION);
  19. //增加菜单项
  20. InsertMenu(hSysMenu, 0, MF_BYPOSITION | MF_STRING, 1001, "测试1");
  21. InsertMenu(hSysMenu, 0, MF_BYPOSITION | MF_STRING, 1002, "测试2");
  22. }
  23. //OnSysCommand
  24. void OnSysCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  25. {
  26. //LOWORD(wParam) 同样为系统菜单的ID
  27. switch (LOWORD(wParam))
  28. {
  29. case 1001:
  30. PrintLog("测试1被触发\n");
  31. break;
  32. case 1002:
  33. PrintLog("测试2被触发\n");
  34. break;
  35. }
  36. sprintf_s(szText, 256, "OnSysCommand wParam:%08X, lParam:%08X\n", wParam, lParam);
  37. PrintLog(szText);
  38. }
  39. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  40. {
  41. switch (nMsg)
  42. {
  43. case WM_CREATE:
  44. OnCreate(hWnd, nMsg, wParam, lParam);
  45. break;
  46. case WM_SYSCOMMAND:
  47. OnSysCommand(hWnd, nMsg, wParam, lParam);
  48. break;
  49. case WM_DESTROY:
  50. PostQuitMessage(0);
  51. break;
  52. }
  53. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  54. }
  55. BOOL RegisterWnd(LPSTR pszClassName)
  56. {
  57. WNDCLASSEX wce = { 0 };
  58. wce.cbSize = sizeof(wce);
  59. wce.cbClsExtra = 0;
  60. wce.cbWndExtra = 0;
  61. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  62. wce.hCursor = NULL;
  63. wce.hIcon = NULL;
  64. wce.hIconSm = NULL;
  65. wce.hInstance = g_hInst;
  66. wce.lpfnWndProc = WndProc;
  67. wce.lpszClassName = pszClassName;
  68. wce.lpszMenuName = NULL;
  69. wce.style = CS_HREDRAW | CS_VREDRAW;
  70. ATOM atom = RegisterClassEx(&wce);
  71. if (atom == NULL)
  72. {
  73. return FALSE;
  74. }
  75. else
  76. {
  77. return TRUE;
  78. }
  79. }
  80. HWND CreateWnd(LPSTR pszClassName)
  81. {
  82. HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  83. CW_USEDEFAULT, NULL, NULL, g_hInst, 0);
  84. return hWnd;
  85. }
  86. void ShowWnd(HWND hWnd)
  87. {
  88. ShowWindow(hWnd, SW_SHOW);
  89. UpdateWindow(hWnd);
  90. }
  91. void Msg()
  92. {
  93. MSG msg = { 0 };
  94. while (GetMessage(&msg,NULL,0,0))
  95. {
  96. TranslateMessage(&msg);
  97. DispatchMessage(&msg);
  98. }
  99. }
  100. void ConsoleWnd()
  101. {
  102. AllocConsole();
  103. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  104. CHAR szText[] = "Debug start:\n";
  105. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  106. }
  107. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  108. {
  109. g_hInst = hInstance;
  110. ConsoleWnd();
  111. RegisterWnd("oooo");
  112. HWND hWnd = CreateWnd("oooo");
  113. ShowWnd(hWnd);
  114. Msg();
  115. return 0;
  116. }





posted @ 2016-06-10 07:55  -刀狂剑痴-  阅读(199)  评论(0编辑  收藏  举报