CreateDialogParam非模态对话框

恩, 非模态对话框有时候还是有些应用的.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// WinTest.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"

BOOL InitDialog(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    HWND hWndDlg = NULL;
    MSG msg;

    hWndDlg = CreateDialogParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DialogProc, NULL);
    if(hWndDlg == NULL)
    {
        MessageBox(NULL, "创建对话框失败.", "", MB_OK);
        return 0;
    }

    RECT rtDlg;
    GetWindowRect(hWndDlg, &rtDlg);

    int nScreenX = GetSystemMetrics(SM_CXSCREEN);
    int nScreenY = GetSystemMetrics(SM_CYSCREEN);

    SetWindowPos(hWndDlg,
                HWND_TOP,
                nScreenX / 2 - rtDlg.right / 2,
                nScreenY / 2 - rtDlg.bottom / 2,
                0,
                0,
                SWP_NOSIZE | SWP_SHOWWINDOW); //SWP_NOSIZE)

    while(GetMessage(&msg, NULL, NULL, NULL))
    {
        if(msg.message == WM_KEYDOWN)
        {
            SendMessage(hWndDlg, msg.message, msg.wParam, msg.lParam);
        }
        else if(!IsDialogMessage(hWndDlg, &msg))// 如果消息没有被处理, 返回值为0
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}

BOOL InitDialog(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
/*
        响应WM_INITDIALOG消息时,如果函数调用SetFocus设置对话
    框中控制中的一个焦点, 则对话框应用程序应该返回零值,否则对
    话框应用程序应该返回非零值在,这种情况下系统对能够有焦点的
    对话框中的第一个控制设置焦点。
*/
    return TRUE;
}

BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_INITDIALOG:
        return InitDialog(hWnd, uMsg, wParam, lParam);

    case WM_KEYDOWN:
        break;             

    case WM_COMMAND:
        if(LOWORD(wParam) == IDOK)
        {
        //  MessageBox(hWnd, "确定", "", MB_OK);
        }
        else if(LOWORD(wParam) == IDCANCEL)
        {
            DestroyWindow(hWnd);   
        }
        return TRUE;
    case WM_CLOSE:
        DestroyWindow(hWnd);
        return TRUE;

    case WM_DESTROY:
        PostQuitMessage(0);
        return TRUE;
    }

    return FALSE;   // 如果函数不处理消息,则对话框应用程序应该返回零值。
}