C++ 关闭窗口而不是应用程序

标签 c++ winapi

我正在尝试创建窗口类的两个实例。 当第一个窗口关闭时,它应该关闭应用程序,但是当第二个窗口关闭时,它应该只关闭该窗口。

但是,当任一窗口关闭时,应用程序都会退出,我不确定为什么。我尝试比较 hWnd 以检查哪个窗口正在关闭。

// include the basic windows header file
#include <windows.h>
#include <windowsx.h>

//Forgive me now
#define MAX_WINDOWS 1024

HWND hWindows[MAX_WINDOWS];

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd,
    UINT message,
    WPARAM wParam,
    LPARAM lParam);

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"WindowClass1";

    RegisterClassEx(&wc);

    hWindows[0] = CreateWindowEx(NULL,
        L"WindowClass1",    // name of the window class
        L"Our First Windowed Program",   // title of the window
        WS_OVERLAPPEDWINDOW,    // window style
        300,    // x-position of the window
        300,    // y-position of the window
        500,    // width of the window
        400,    // height of the window
        NULL,    // we have no parent window, NULL
        NULL,    // we aren't using menus, NULL
        hInstance,    // application handle
        NULL);    // used with multiple windows, NULL

    hWindows[1] = CreateWindowEx(NULL,
        L"WindowClass1",    // name of the window class
        L"Our First Windowed Program",   // title of the window
        WS_OVERLAPPEDWINDOW,    // window style
        300,    // x-position of the window
        300,    // y-position of the window
        500,    // width of the window
        400,    // height of the window
        hWindows[0],    // primary window
        NULL,    // we aren't using menus, NULL
        hInstance,    // application handle
        NULL);    // used with multiple windows, NULL

    ShowWindow(hWindows[0], nCmdShow);
    ShowWindow(hWindows[1], nCmdShow);

    MSG msg;

    while (GetMessage(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam;
}

// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    // sort through and find what code to run for the message given
    switch (message)
    {

        case WM_CLOSE:
        {
            if (hWnd = hWindows[0]) {
                // close the application entirely
                PostQuitMessage(0);
            }
            else {
                DestroyWindow(hWnd);
            }
            return 0;
        } break;

    }

    // Handle any messages the switch statement didn't
    return DefWindowProc(hWnd, message, wParam, lParam);
}

最佳答案

if (hWnd = hWindows[0])

这就是作业。由于 hWindows[0] 不为零,因此该表达式的计算结果始终为 true。

你的意思是:

if (hWnd == hWindows[0])

您应该调用 PostQuitMessage 来响应 WM_DESTROY。由于默认窗口过程调用 DestroyWindow 来响应 WM_CLOSE,因此您可以这样编写:

switch (message)
{
case WM_DESTROY:
    {
        if (hWnd == hWindows[0]) {
            // close the application entirely
            PostQuitMessage(0);
        }
        return 0;
    }
    break;
}

// Handle any messages the switch statement didn't
return DefWindowProc(hWnd, message, wParam, lParam);

关于C++ 关闭窗口而不是应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35776580/

相关文章:

c++ - typedef 类型和相同类型的指针的正确方法是什么?

c++ - QT中用构造函数初始化对象,没有匹配的函数可以调用

c++ - NSURLConnection 和 NSURLRequest 的纯 C++ 等价物

c++ - IUnknown.Release 标准实现竞争条件?

windows - 如何获取任意时区的 TIME_ZONE_INFORMATION?

c++ - 连接 LPTSTR 与 const char* (Win32 C++)

c++ - Loki 的多重方法是否进入了 C++11?

c++ - Windows C/Sleep() 函数在时钟漂移期间将如何运行?

python - win32 : simulate a click without simulating mouse movement?

winapi - 如何在 Windows 8 中以编程方式检查/切换飞行模式?