码迷,mamicode.com
首页 > Windows程序 > 详细

12Windows_Modeless_Dialog对话框

时间:2016-06-10 09:37:08      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:

12Windows_Modeless_Dialog对话框

  1. #include <windows.h>
  2. #include <iostream>
  3. #include "resource.h"
  4. CHAR szText[256] = { 0 };
  5. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL)
  6. HINSTANCE g_hInst = NULL; //窗口句柄
  7. HANDLE g_hStdout = NULL; //控制台句柄
  8. //查找窗口处理函数
  9. BOOL CALLBACK FindDlgProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  10. {
  11. switch (nMsg)
  12. {
  13. //可以看出子窗口的创建销毁和初始化都与主窗口不同
  14. //1、创建用CreateDialog
  15. //2、初始化用WM_INITDIALOG消息
  16. //3、销毁用SC_COLOSE消息
  17. //WM_CREATE消息在这里被屏蔽了
  18. case WM_CREATE: //这个消息在这里没有用
  19. MessageBox(NULL, "Create", "WinDlg", MB_OK);
  20. break;
  21. case WM_INITDIALOG: //大量的初始化是在这里做的
  22. MessageBox(NULL, "WM_INITDIALOG", "WinDlg", MB_OK);
  23. break;
  24. case WM_SYSCOMMAND:{
  25. switch (wParam)
  26. {
  27. case SC_CLOSE:
  28. EndDialog(hWnd, 0);
  29. break;
  30. }
  31. }
  32. break;
  33. }
  34. return 0;
  35. }
  36. //查找函数
  37. void Find(HWND hWnd)
  38. {
  39. //创建对话框
  40. HWND hDlg = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_FIND), hWnd, FindDlgProc);
  41. //显示对话框
  42. ShowWindow(hDlg,SW_SHOW);
  43. }
  44. //OnCommand
  45. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  46. {
  47. //获取命令ID
  48. int nCmdId = LOWORD(wParam);
  49. switch (nCmdId)
  50. {
  51. case ID_EXIT:
  52. PostQuitMessage(0);
  53. break;
  54. case ID_FIND:
  55. Find(hWnd);
  56. break;
  57. }
  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_DESTROY:
  67. PostQuitMessage(0);
  68. break;
  69. }
  70. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  71. }
  72. BOOL RegisterWnd(LPSTR pszClassName)
  73. {
  74. WNDCLASSEX wce = { 0 };
  75. wce.cbSize = sizeof(wce);
  76. wce.cbClsExtra = 0;
  77. wce.cbWndExtra = 0;
  78. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  79. wce.hCursor = NULL;
  80. wce.hIcon = NULL;
  81. wce.hIconSm = NULL;
  82. wce.hInstance = g_hInst;
  83. wce.lpfnWndProc = WndProc;
  84. wce.lpszClassName = pszClassName;
  85. wce.lpszMenuName = NULL;
  86. wce.style = CS_HREDRAW | CS_VREDRAW;
  87. ATOM atom = RegisterClassEx(&wce);
  88. if (atom == NULL)
  89. {
  90. return FALSE;
  91. }
  92. else
  93. {
  94. return TRUE;
  95. }
  96. }
  97. HWND CreateWnd(LPSTR pszClassName)
  98. {
  99. HMENU hMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MAIN));
  100. HWND hWnd = CreateWindowEx(0, pszClassName, "灭天", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  101. CW_USEDEFAULT, NULL, hMenu, g_hInst, 0);
  102. return hWnd;
  103. }
  104. void ShowWnd(HWND hWnd)
  105. {
  106. ShowWindow(hWnd, SW_SHOW);
  107. UpdateWindow(hWnd);
  108. }
  109. void Msg()
  110. {
  111. MSG msg = { 0 };
  112. while (GetMessage(&msg, NULL, 0, 0))
  113. {
  114. TranslateMessage(&msg);
  115. DispatchMessage(&msg);
  116. }
  117. }
  118. void ConsoleWnd()
  119. {
  120. AllocConsole();
  121. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  122. CHAR szText[] = "Debug start:\n";
  123. WriteConsole(g_hStdout, szText, strlen(szText), NULL, NULL);
  124. }
  125. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  126. {
  127. g_hInst = hInstance; //ConsoleWnd();
  128. RegisterWnd("oooo");
  129. HWND hWnd = CreateWnd("oooo");
  130. ShowWnd(hWnd);
  131. Msg();
  132. return 0;
  133. }





12Windows_Modeless_Dialog对话框

标签:

原文地址:http://www.cnblogs.com/nfking/p/5573172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!