Win32简单模板

Win32简单模板

  1. // 2Window_Timer.cpp : 定义应用程序的入口点。
  2. //
  3. #include <windows.h>
  4. #include <iostream>
  5. #include "resource1.h"
  6. //宏定义输出函数
  7. #define PrintLog(x) WriteConsole(g_hStdout, x, strlen(x), NULL, NULL);
  8. HINSTANCE g_hInst;
  9. HANDLE g_hStdout = NULL;
  10. CHAR szText[256] = { 0 };
  11. void OnCommand(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  12. {
  13. int hMenuID = LOWORD(wParam);
  14. switch (hMenuID)
  15. {
  16. case ID_EXIT:
  17. PostQuitMessage(0);
  18. break;
  19. }
  20. }
  21. LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  22. {
  23. switch (nMsg)
  24. {
  25. case WM_COMMAND:
  26. OnCommand(hWnd, nMsg, wParam, lParam);
  27. break;
  28. case WM_DESTROY:
  29. PostQuitMessage(0);
  30. break;
  31. }
  32. return DefWindowProc(hWnd, nMsg, wParam, lParam);
  33. }
  34. BOOL RegisterWnd(LPSTR pszClassName)
  35. {
  36. WNDCLASSEX wce = { 0 };
  37. wce.cbSize = sizeof(wce);
  38. wce.cbClsExtra = 0;
  39. wce.cbWndExtra = 0;
  40. wce.hbrBackground = HBRUSH(COLOR_BTNFACE + 1);
  41. wce.hCursor = NULL;
  42. wce.hIcon = NULL;
  43. wce.hIconSm = NULL;
  44. wce.hInstance = g_hInst;
  45. wce.lpfnWndProc = WndProc;
  46. wce.lpszClassName = pszClassName;
  47. wce.lpszMenuName = NULL;
  48. wce.style = CS_VREDRAW | CS_HREDRAW;
  49. ATOM aTom = RegisterClassEx(&wce);
  50. if (aTom == 0)
  51. {
  52. return FALSE;
  53. }
  54. else
  55. {
  56. return TRUE;
  57. }
  58. }
  59. HWND CreateWnd(LPSTR pszClassName)
  60. {
  61. //加入菜单
  62. HMENU hMainMenu = LoadMenu(g_hInst, MAKEINTRESOURCE(IDR_MAIN));
  63. HWND hWnd = CreateWindowEx(0, pszClassName, "风火中原", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  64. NULL, hMainMenu, g_hInst, NULL);
  65. return hWnd;
  66. }
  67. void showWnd(HWND hWnd)
  68. {
  69. ShowWindow(hWnd, SW_SHOW);
  70. UpdateWindow(hWnd);
  71. }
  72. void msg()
  73. {
  74. MSG msg = { 0 };
  75. while (GetMessage(&msg, NULL, 0, 0))
  76. {
  77. TranslateMessage(&msg);
  78. DispatchMessage(&msg);
  79. }
  80. }
  81. //控制台
  82. void ConsoleWnd()
  83. {
  84. AllocConsole();
  85. g_hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
  86. CHAR szText[] = "Debug start:\n";
  87. PrintLog(szText);
  88. }
  89. int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd)
  90. {
  91. g_hInst = hInstance;
  92. ConsoleWnd();
  93. RegisterWnd("OOOO");
  94. HWND hWnd = CreateWnd("OOOO");
  95. showWnd(hWnd);
  96. msg();
  97. return 0;
  98. }





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