kingkong_zxy 2017-04-30 13:27 采纳率: 100%
浏览 1576
已采纳

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

#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条回答 默认 最新

  • 战在春秋 2017-04-30 22:38
    关注

    你的直觉是对的。

     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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。