Child Window closes application when close button is clicked

46 views Asked by At

I have a button and want to open a new window when the button is pressed.

HWND hwnd;

void CreateChildWindow() {

    HWND hNewWindow = CreateWindow(
        _T("OtherSimpleWindowClass"),
        _T("Neues Fenster"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        0,
        0,
        (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE),
        0);
        
    ShowWindow(hNewWindow, SW_SHOWNORMAL);

}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_CLOSE:
            PostQuitMessage(0);
            return 0;
        case WM_COMMAND:
            if (LOWORD(wParam) == 1) {
                CreateChildWindow();
            }
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = _T("SimpleWindowClass");

    RegisterClass(&wc);

    hwnd = CreateWindowEx(
        0,
        _T("SimpleWindowClass"),
        _T("Button"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        0,
        0,
        hInstance,
        0);

    ShowWindow(hwnd, nCmdShow);

    HWND hButton = CreateWindow(
        _T("BUTTON"),
        _T("Klick mich"),
        WS_CHILD | WS_VISIBLE,
        50, 50, 100, 30,
        hwnd,
        (HMENU)1, // Der Identifier des Buttons ist 1
        hInstance,
        NULL
    );

    MSG msg = {};
    while(GetMessage(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

The child window pops up as expected but when i try to close the child window the base window also closes and the application exits. How can i close only the newly created window but not the base window?

Edit I have added the full example.

1

There are 1 answers

0
mbrain On

Thanks to @Raymond Chen i came up with this solution. I passed the hInstance of the main window to the CreateChildWindow function.

#include <windows.h>
#include <tchar.h>

HWND hGwnd;

void CreateChildWindow(HINSTANCE hInstance) {

    WNDCLASS wcChild = {};
    wcChild.lpfnWndProc = DefWindowProc; // Default Window Procedure
    wcChild.hInstance = hInstance;
    wcChild.lpszClassName = _T("OtherSimpleWindowClass");

    RegisterClass(&wcChild);

    HWND hNewWindow = CreateWindow(
        _T("OtherSimpleWindowClass"),
        _T("Neues Fenster"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 400, 300,
        0,
        0,
        hInstance,
        0);
        
    ShowWindow(hNewWindow, SW_SHOWNORMAL);

}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        case WM_CLOSE:
            if (hGwnd == hwnd) { 
                PostQuitMessage(0);
            } else {
                DestroyWindow(hwnd);
            }
            return 0;
        case WM_COMMAND:
            if (LOWORD(wParam) == 1) {
                CreateChildWindow((HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE)); // hInstance übergeben
            }
            break;
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc = {};
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = _T("SimpleWindowClass");

    RegisterClass(&wc);

    hGwnd = CreateWindowEx(
        0,
        _T("SimpleWindowClass"),
        _T("Button"),
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        0,
        0,
        hInstance,
        0);

    ShowWindow(hGwnd, nCmdShow);

    HWND hButton = CreateWindow(
        _T("BUTTON"),
        _T("Klick mich"),
        WS_CHILD | WS_VISIBLE,
        50, 50, 100, 30,
        hGwnd,
        (HMENU)1, // Der Identifier des Buttons ist 1
        hInstance,
        NULL
    );

    MSG msg = {};
    while(GetMessage(&msg, 0, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}