handless
handless

Reputation: 23

How can I close a child window without closing the parent?

I have a window which has 5 child windows. How can I close a child window without closing the parent?

Upvotes: 2

Views: 5764

Answers (3)

Yaroy Díaz
Yaroy Díaz

Reputation: 21

I had the same problem so I did this to solve it:

  1. Create a global handle variable the will be used as a reference to the main parent window.

    HWND mainHwnd;
    
  2. In the main function of the program where the parent window is first created I did this:

    mainHwnd = CreateWindowEx( /* your window setup here */ );
    
  3. Then, in the LRESULT method I did this:

    LRESULT CALLBACK AboutWindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
    
        switch(msg){
    
        case WM_COMMAND:{
            switch(wParam){
    
            case BTN_ABOUT_CLOSE:
                EnableWindow(mainHwnd, true); // => THIS IS THE SOLUTION
                DestroyWindow(hWnd);
                break;
    
            }
            break;
        }
    
        case WM_CREATE:{
            // WHATEVER HAPPENS HERE...
            break;
        }
    
        case WM_CLOSE:{
            EnableWindow(mainHwnd, true); // => AND HERE TOO, IN CASE USER CLOSES THE WINDOW USING THE X BUTTON
            DestroyWindow(hWnd);
            break;
        }
        default:{
            return DefWindowProcW(hWnd, msg, wParam, lParam);
        }
    }
    
    return 0;
    
    }
    

I hope it helps. Happy coding!

Upvotes: 0

annoying_squid
annoying_squid

Reputation: 531

This was happening to me. I had added a case WM_DESTROY to the window procedure I registered for my child windows. This was causing my entire application to quit. Once removed, everything worked good.

Upvotes: 0

StevieG
StevieG

Reputation: 8709

Presumably you have handles for the child windows? If so, then just use DestroyWindow.

EDIT:

You should define a WndProc method in your main 'window' to handle callbacks from your child windows. You use this to define what you want to do with each message. In your case, you want to call destroyWindow.

Something like this:

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

Upvotes: 5

Related Questions