1. #include <Windows.h>
    2. #include "resource.h"
    3. // 窗口是通过代码实现的,不是资源
    4. // 对话框是作为资源存在的,可以直接使用
    5. INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
    6. {
    7. switch (uMsg)
    8. {
    9. // 对话框的关闭事件和窗口一致,但是内部的处理不同
    10. case WM_CLOSE:
    11. {
    12. EndDialog(hDlg, 0);
    13. break;
    14. }
    15. // 所有没有处理的消息,都返回FALSE
    16. default:
    17. {
    18. return FALSE;
    19. }
    20. }
    21. // 对于对话框的回调函数,如果消息需要让系统继续处理,就返回 FALSE,否则返回 TRUE
    22. return TRUE;
    23. }
    24. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdline, int nCmdShow)
    25. {
    26. // 以[模态]对话框的方式使用对话框资源,模态对话框有以下特点:
    27. // 1. 由于内建消息循环,所以 DialogBox 是阻塞的函数
    28. // 2. 一旦创建了模态对话框,那么父类的消息会被忽略
    29. // 3. 函数的内部自己调用了 ShowWindow, 不需要手动编写
    30. // 4. 必须使用 EndDialog 关闭模态对话框
    31. DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
    32. return 0;
    33. }