1. // WindowsProject1.cpp : 定义应用程序的入口点。
    2. //
    3. #include "framework.h"
    4. #include "WindowsProject1.h"
    5. #define MAX_LOADSTRING 100
    6. // 全局变量:
    7. HINSTANCE hInst; // 当前实例
    8. WCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
    9. WCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
    10. // 此代码模块中包含的函数的前向声明:
    11. ATOM MyRegisterClass(HINSTANCE hInstance);
    12. BOOL InitInstance(HINSTANCE, int);
    13. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    14. INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
    15. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    16. _In_opt_ HINSTANCE hPrevInstance,
    17. _In_ LPWSTR lpCmdLine,
    18. _In_ int nCmdShow)
    19. {
    20. UNREFERENCED_PARAMETER(hPrevInstance);
    21. UNREFERENCED_PARAMETER(lpCmdLine);
    22. // TODO: 在此处放置代码。
    23. // 初始化全局字符串
    24. LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    25. LoadStringW(hInstance, IDC_WINDOWSPROJECT1, szWindowClass, MAX_LOADSTRING);
    26. MyRegisterClass(hInstance);
    27. // 执行应用程序初始化:
    28. if (!InitInstance (hInstance, nCmdShow))
    29. {
    30. return FALSE;
    31. }
    32. HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WINDOWSPROJECT1));
    33. MSG msg;
    34. // 主消息循环:
    35. while (GetMessage(&msg, nullptr, 0, 0))
    36. {
    37. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
    38. {
    39. TranslateMessage(&msg);
    40. DispatchMessage(&msg);
    41. }
    42. }
    43. return (int) msg.wParam;
    44. }
    45. //
    46. // 函数: MyRegisterClass()
    47. //
    48. // 目标: 注册窗口类。
    49. //
    50. ATOM MyRegisterClass(HINSTANCE hInstance)
    51. {
    52. WNDCLASSEXW wcex;
    53. wcex.cbSize = sizeof(WNDCLASSEX);
    54. wcex.style = CS_HREDRAW | CS_VREDRAW;
    55. wcex.lpfnWndProc = WndProc;
    56. wcex.cbClsExtra = 0;
    57. wcex.cbWndExtra = 0;
    58. wcex.hInstance = hInstance;
    59. wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINDOWSPROJECT1));
    60. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
    61. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    62. wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_WINDOWSPROJECT1);
    63. wcex.lpszClassName = szWindowClass;
    64. wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
    65. return RegisterClassExW(&wcex);
    66. }
    67. //
    68. // 函数: InitInstance(HINSTANCE, int)
    69. //
    70. // 目标: 保存实例句柄并创建主窗口
    71. //
    72. // 注释:
    73. //
    74. // 在此函数中,我们在全局变量中保存实例句柄并
    75. // 创建和显示主程序窗口。
    76. //
    77. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    78. {
    79. hInst = hInstance; // 将实例句柄存储在全局变量中
    80. HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
    81. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
    82. if (!hWnd)
    83. {
    84. return FALSE;
    85. }
    86. ShowWindow(hWnd, nCmdShow);
    87. UpdateWindow(hWnd);
    88. return TRUE;
    89. }
    90. //
    91. // 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
    92. //
    93. // 目标: 处理主窗口的消息。
    94. //
    95. // WM_COMMAND - 处理应用程序菜单
    96. // WM_PAINT - 绘制主窗口
    97. // WM_DESTROY - 发送退出消息并返回
    98. //
    99. //
    100. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    101. {
    102. switch (message)
    103. {
    104. case WM_COMMAND:
    105. {
    106. int wmId = LOWORD(wParam);
    107. // 分析菜单选择:
    108. switch (wmId)
    109. {
    110. case IDM_ABOUT:
    111. DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
    112. break;
    113. case IDM_EXIT:
    114. DestroyWindow(hWnd);
    115. break;
    116. default:
    117. return DefWindowProc(hWnd, message, wParam, lParam);
    118. }
    119. }
    120. break;
    121. case WM_PAINT:
    122. {
    123. PAINTSTRUCT ps;
    124. HDC hdc = BeginPaint(hWnd, &ps);
    125. // TODO: 在此处添加使用 hdc 的任何绘图代码...
    126. EndPaint(hWnd, &ps);
    127. }
    128. break;
    129. case WM_DESTROY:
    130. PostQuitMessage(0);
    131. break;
    132. default:
    133. return DefWindowProc(hWnd, message, wParam, lParam);
    134. }
    135. return 0;
    136. }
    137. // “关于”框的消息处理程序。
    138. INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    139. {
    140. UNREFERENCED_PARAMETER(lParam);
    141. switch (message)
    142. {
    143. case WM_INITDIALOG:
    144. return (INT_PTR)TRUE;
    145. case WM_COMMAND:
    146. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
    147. {
    148. EndDialog(hDlg, LOWORD(wParam));
    149. return (INT_PTR)TRUE;
    150. }
    151. break;
    152. }
    153. return (INT_PTR)FALSE;
    154. }