首页 > 解决方案 > 为什么我的窗口已关闭,但应用程序有时仍在后台运行

问题描述

主.cpp:

#include <Windows.h>

LRESULT CALLBACK WndProc(HWND hWnd,
    UINT msgID,
    WPARAM wParam,
    LPARAM lParam)
{
    return DefWindowProc(hWnd, msgID, wParam, lParam);
}


int CALLBACK WinMain(_In_ HINSTANCE hIns,
    _In_opt_ HINSTANCE hPreIns,
    _In_ LPSTR lpCmdLine,
    _In_ int nCmdShow)
{
    WNDCLASS wc = { 0 };
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hIns;
    wc.hIcon = NULL;
    wc.hCursor = NULL;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = "MAINWINDOW";

    RegisterClass(&wc);

    HWND hWnd = CreateWindow("MAINWINDOW",
        "mainwindow",
        WS_OVERLAPPEDWINDOW,
        100, 100, 500, 500,
        NULL, NULL, hIns, NULL);

    ShowWindow(hWnd, SW_SHOW);

    MSG nMsg = { 0 };
    while (GetMessage(&nMsg, NULL, 0, 0))
    {
        TranslateMessage(&nMsg);
        DispatchMessage(&nMsg);
    }
    return 0;
}

我认为如果我关闭窗口,应用程序也应该关闭。但是,有时它按我的想法工作,有时窗口关闭但应用程序仍在运行。为什么以及如何确保在我关闭窗口后肯定退出应用程序

标签: c++windows

解决方案


推荐阅读