13windows_static静态标签

13windows_static静态标签

  1. #include <windows.h>
  2. #include <iostream>
  3. #include "resource.h"
  4. /**
  5. *dat : 2015-05-22 10:59:53
  6. *file : F:\long\win32 study\Win32_Project\13Windows_static\13windows_static.cpp
  7. *file base: 13windows_static
  8. *say: 本函数说明了静态框如果添加文字及图片,如何使用STN_CLICK消息
  9. */
  10. CHAR szText[256] = { 0 };
  11. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  12. HINSTANCE g_hInst = NULL; //窗口句柄
  13. HANDLE g_hStdout = NULL; //控制台句柄
  14. //OnCommand
  15. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  16. {
  17. int nCmdID = HIWORD(wParam);//父窗口的ID
  18. int nCtrlID = LOWORD(wParam);//控件ID
  19. switch (nCtrlID)
  20. {
  21. //变换图标
  22. case ID_CHANGICON:
  23. {
  24. HICON hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_ICON1));
  25. HWND hStatic = GetDlgItem(hWnd, 1002); //根据子控件ID获得ID所对应的句柄
  26. SendMessage(hStatic, STM_SETICON, (WPARAM)hIcon, 0);
  27. }
  28. break;
  29. //静态框截获点击消息
  30. case 1001:
  31. switch (nCmdID)
  32. {
  33. case STN_CLICKED:
  34. MessageBox(NULL, "nCmdID", "static", MB_OK);
  35. break;
  36. }
  37. break;
  38. case 1002:
  39. switch (nCmdID)
  40. {
  41. case STN_CLICKED:
  42. MessageBox(NULL, "nCmdID_pic", "static_pic", MB_OK);
  43. break;
  44. }
  45. }
  46. }
  47. //OnCreate静态框
  48. void OnCreate(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  49. {
  50. //文字风格
  51. CreateWindow("STATIC", "哈喽!静态框",
  52. WS_CHILD/*子窗口*/ | WS_VISIBLE | SS_SUNKEN | SS_SIMPLE/*独有的风格*/|SS_NOTIFY/*让窗口能点击风格*/,
  53. 50, 50, 100, 100, hWnd, (HMENU)1001, g_hInst, NULL);
  54. //图片风格
  55. CreateWindow("STATIC", "#101"/*把对应的ID资源号弄进来*/,
  56. WS_CHILD | WS_VISIBLE | SS_ICON | SS_SUNKEN|SS_NOTIFY/*让窗口能点击风格*/,
  57. 200, 50, 100, 100, hWnd, (HMENU)1002, g_hInst, NULL);
  58. }
  59. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  60. {
  61. switch (nMsg)
  62. {
  63. case WM_COMMAND:
  64. OnCommand(hWnd, nMsg, wParam, lParam);
  65. break;
  66. case WM_CREATE://静态框要在WM_CREATE消息中创建
  67. OnCreate(hWnd, nMsg, wParam, lParam);
  68. break;
  69. case WM_DESTROY:
  70. PostQuitMessage(0);
  71. break;
  72. }
  73. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  74. }
  75. BOOL RegisterWnd(LPSTR pszClassName)
  76. {
  77. WNDCLASSEX wce = { 0 };
  78. wce.cbSize = sizeof(wce);
  79. wce.cbClsExtra = 0;
  80. wce.cbWndExtra = 0;
  81. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  82. wce.hCursor = NULL;
  83. wce.hIcon = NULL;
  84. wce.hIconSm = NULL;
  85. wce.hInstance = g_hInst;
  86. wce.lpfnWndProc = WndProc;
  87. wce.lpszClassName = pszClassName;
  88. wce.lpszMenuName = NULL;
  89. wce.style = CS_HREDRAW | CS_VREDRAW;
  90. ATOM atom = RegisterClassEx(&wce);
  91. if (atom == NULL)
  92. {
  93. return FALSE;
  94. }
  95. else
  96. {
  97. return TRUE;
  98. }
  99. }
  100. HWND CreateWnd(LPSTR pszClassName)
  101. {
  102. HMENU hMen = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MENU1));
  103. HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  104. CW_USEDEFAULT, NULL, hMen, g_hInst, 0);
  105. return hWnd;
  106. }
  107. void ShowWnd(HWND hWnd)
  108. {
  109. ShowWindow(hWnd, SW_SHOW);
  110. UpdateWindow(hWnd);
  111. }
  112. void Msg()
  113. {
  114. MSG msg = { 0 };
  115. while (GetMessage(&msg, NULL, 0, 0))
  116. {
  117. TranslateMessage(&msg);
  118. DispatchMessage(&msg);
  119. }
  120. }
  121. void ConsoleWnd()
  122. {
  123. AllocConsole();
  124. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  125. CHAR szText[] = "Debug start:\n";
  126. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  127. }
  128. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  129. {
  130. g_hInst = hInstance;
  131. //ConsoleWnd();
  132. RegisterWnd("oooo");
  133. HWND hWnd = CreateWnd("oooo");
  134. ShowWnd(hWnd);
  135. Msg();
  136. return 0;
  137. }





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