请问为什么对话框结束后主窗口也会自动关闭

#include
#include
#include
#include"resource.h"
#include "winres.h"

static CHOOSECOLOR cc;
static COLORREF CR[16];
static COLORREF backResult;
static COLORREF wordColor;
HBRUSH hBrush;

//函数声明
BOOL InitWindow(HINSTANCE hInstance, int nCmdShow);
BOOL CALLBACK ColorProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, //当前实例句柄
HINSTANCE hPrevInstance, //前一个实例句柄
PSTR szCmdLine, //命令行字符
int iCmdShow) //窗口显示方式
{
MSG msg;
char *p;

//创建主窗口
if (!InitWindow(hInstance, iCmdShow))
    return FALSE;

p = 0;
//进入消息循环:从该应用程序的消息队列中检取消息,
//送到消息处理过程,当检取到WM_QUIT消息时,退出消息循环。
while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}

return msg.wParam;

}

static BOOL InitWindow(HINSTANCE hInstance, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("EasyWin"); //应用程序名称
HWND hwnd; //窗口句柄
WNDCLASS wcMainWnd; //窗口类结构
ATOM a;

//填充窗口类结构
wcMainWnd.style = CS_VREDRAW | CS_HREDRAW;
wcMainWnd.lpfnWndProc = WinProc;
wcMainWnd.cbClsExtra = 0;
wcMainWnd.cbWndExtra = 0;
wcMainWnd.hInstance = hInstance;
wcMainWnd.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcMainWnd.hCursor = LoadCursor(NULL, IDC_ARROW);
wcMainWnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wcMainWnd.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wcMainWnd.lpszClassName = szAppName;

//注册窗口类
a = RegisterClass(&wcMainWnd);

if (!a)
{
    MessageBox(NULL, TEXT("注册窗口类失败!"), szAppName,
        MB_ICONERROR);
    return 0;
}

//创建主窗口
hwnd = CreateWindow(szAppName,                      //窗口类名称
    TEXT("Windows 应用程序基本框架"),  //窗口标题
    WS_OVERLAPPEDWINDOW,                //窗口风格
    100,                                //窗口位置的x坐标
    100,                                //窗口位置的y坐标
    400,                                //窗口的宽度
    300,                                //窗口的高度
    NULL,                               //父窗口句柄
    NULL,                               //菜单句柄
    hInstance,                          //应用程序实例句柄
    NULL);                              //窗口创建数据指针

if (!hwnd) return FALSE;

//显示并更新窗口
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
return TRUE;

}

LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;

switch (message)
{
case WM_CREATE:
    cc.Flags = CC_RGBINIT | CC_FULLOPEN;
    cc.hInstance = NULL;
    cc.hwndOwner = hWnd;
    cc.lCustData = 0;
    cc.lpCustColors = CR;
    cc.lpfnHook = NULL;
    cc.lpTemplateName = NULL;
    cc.lStructSize = sizeof(CHOOSECOLOR);
    cc.rgbResult = RGB(0, 0, 0);
    return 0;
case WM_PAINT:              //客户区重绘消息
                            //取得设备环境句柄
    hdc = BeginPaint(hWnd, &ps);
    //取得窗口客户区矩形
    GetClientRect(hWnd, &rect);
    //设置文字颜色
    SetTextColor(hdc, wordColor);
    //输出文字
    DrawText(hdc, TEXT("sdfkgjfsk"),
        -1, &rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

    //释放资源
    if (wParam)
    {
        hBrush = CreateSolidBrush(backResult);
        SetClassLong(hWnd, GCL_HBRBACKGROUND, (LONG)backResult);
        InvalidateRect(hWnd, NULL, TRUE);
    }
    EndPaint(hWnd, &ps);
    return 0;



case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case ID_40001:
        ChooseColor(&cc);
        wordColor = cc.rgbResult;
        InvalidateRect(hWnd, NULL, TRUE);
        return 0;

    case ID_40002:
        DialogBox(NULL,
            MAKEINTRESOURCE(IDD_DIALOG1), hWnd, ColorProc);

        InvalidateRect(hWnd, NULL, TRUE);
        /*SendMessage(hWnd, WM_COMMAND, wParam, lParam);*/
    }

case WM_DESTROY:
    PostQuitMessage(0);
    return 0;

}

return DefWindowProc(hWnd, message, wParam, lParam);

}
BOOL CALLBACK ColorProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
BYTE bRed;
BYTE bGreen;
BYTE bBlue;
static char copyResult0[10], copyResult1[10], copyResult2[10];
char Result[50];
switch (message)
{

case WM_INITDIALOG:
    return FALSE;

case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case IDC_BUTTON1:
        ChooseColor(&cc);//调用通用对话框
        backResult = cc.rgbResult;

        bRed = GetRValue(backResult);
        bGreen = GetGValue(backResult);
        bBlue = GetBValue(backResult);



        sprintf(Result, "%d", bRed);
        memcpy(&copyResult0[0], Result, sizeof(Result));
        memset(Result, 0, sizeof(Result));

        sprintf(Result, "%d", bGreen);
        memcpy(&copyResult1[0], Result, sizeof(Result));
        memset(Result, 0, sizeof(Result));

        sprintf(Result, "%d", bBlue);
        memcpy(&copyResult2[0], Result, sizeof(Result));
        memset(Result, 0, sizeof(Result));

        strcat(copyResult0, ",");
        strcat(copyResult0, copyResult1);
        strcat(copyResult0, ",");
        strcat(copyResult0, copyResult2);
        SetWindowText(GetDlgItem(hWnd, IDC_EDIT1), copyResult0);
        memset(copyResult0, 0, sizeof(copyResult0));
        memset(copyResult1, 0, sizeof(copyResult1));
        memset(copyResult2, 0, sizeof(copyResult2));
        InvalidateRect(hWnd, NULL, TRUE);
        return TRUE;

    case IDOK:
        SendMessage(GetParent(hWnd), WM_PAINT, wParam, lParam);
        EndDialog(hWnd, 1);
        return TRUE;

    case IDCANCEL:
        EndDialog(hWnd, 0);
        return TRUE;
    }
    break;
}
return FALSE;

}

2个回答

你的直觉是对的。

 case WM_DESTROY:
    PostQuitMessage(0); // 不要调用PostQuitMessage,否则会退出整个程序。
    return 0;

整个逻辑是这样的:

首先PostQuitMessage(0);会在消息队列中放入WM_QUIT

参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms644945(v=vs.85).aspx

**接下来WM_QUIT消息会通知结束应用程序 **

参考:https://msdn.microsoft.com/en-us/library/windows/desktop/ms632641(v=vs.85).aspx

fight_in_dl
战在春秋 看你的代码 ok和cancel按钮都已经有响应代码,应该不用调用postmessage,注释掉此行就可以了。具体还是得你自己上机调试一下。
3 年多之前 回复
kingkong_zxy
kingkong_zxy 如果不用postmessage要用什么呢?
3 年多之前 回复

我觉得是在WM_DESTROY消息那出了问题,但不知道该怎么处理

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问