Search code examples
c++winapiwindowchildwindow

c++ not able to open child window after closing it


I'm having some problems with my child window. I use a button from the AppendMenu to open it, but after I close the child window, I can't open it anymore.

code:

WNDCLASSEX chwincl;

chwincl.hInstance = hThisInstance;
chwincl.lpszClassName = "Child";
chwincl.lpfnWndProc = ChildProcedure;
chwincl.style = CS_DBLCLKS;
chwincl.cbSize = sizeof(WNDCLASSEX);
chwincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
chwincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
chwincl.hCursor = LoadCursor(NULL, IDC_ARROW);
chwincl.lpszMenuName = NULL;
chwincl.cbClsExtra = 0;
chwincl.cbWndExtra = 0;
chwincl.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);

if (!RegisterClassEx(&chwincl))
    return 2;

chwnd = CreateWindowEx(0,
    "Child",
    "Add...",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    300,
    150,
    HWND_DESKTOP,
    NULL,
    hThisInstance,
    NULL);

I open the child window with this:

if (LOWORD(wParam) == ID_Click) {
        ShowWindow(chwnd, SW_SHOWDEFAULT);
        UpdateWindow(chwnd);
    }

And I close it with this:

DestroyWindow(chwnd);

Why can I only open my child window once?

Thanks


Solution

  • DestroyWindow destroys the the window completely. After that call, it no longer exists. So you can't then show it again with ShowWindow - you have to actually create it again from scratch.

    Instead of calling DestroyWindow to hide it, use ShowWindow(chwnd, SW_HIDE);