如何在不关闭父窗口的情况下关闭子窗口?

发布于 2025-01-03 04:37:49 字数 42 浏览 2 评论 0原文

我有一个有 5 个子窗口的窗口。如何在不关闭父窗口的情况下关闭子窗口?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒一口袋星星 2025-01-10 04:37:49

想必您有子窗口的句柄?如果是这样,则只需使用 DestroyWindow< /a>.

编辑:

您应该在主“窗口”中定义一个 WndProc 方法来处理来自子窗口的回调。您可以使用它来定义要对每条消息执行的操作。在你的情况下,你想调用 destroyWindow。

像这样的事情:

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;
}

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;
}
白昼 2025-01-10 04:37:49

这件事就发生在我身上。我已将一个案例 WM_DESTROY 添加到为子窗口注册的窗口过程中。这导致我的整个应用程序退出。移除后,一切正常。

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.

雪花飘飘的天空 2025-01-10 04:37:49

我遇到了同样的问题,所以我这样做来解决它:

  1. 创建一个全局句柄变量,它将用作对主父窗口的引用。

    HWND mainHwnd;
    
  2. 在首次创建父窗口的程序的主函数中,我这样做了:

    mainHwnd = CreateWindowEx( /* 这里设置你的窗口 */ );
    
  3. 然后,在 LRESULT 方法中,我执行了以下操作:

    LRESULT CALLBACK AboutWindowProcedure(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
    
        开关(消息){
    
        案例 WM_COMMAND:{
            开关(wParam){
    
            案例 BTN_ABOUT_CLOSE:
                EnableWindow(mainHwnd, true); // =>这就是解决方案
                销毁窗口(hWnd);
                休息;
    
            }
            休息;
        }
    
        案例 WM_CREATE:{
            // 无论这里发生什么...
            休息;
        }
    
        案例 WM_CLOSE:{
            EnableWindow(mainHwnd, true); // =>这里也是,以防用户使用 X 按钮关闭窗口
            销毁窗口(hWnd);
            休息;
        }
        默认:{
            返回 DefWindowProcW(hWnd, msg, wParam, lParam);
        }
    }
    
    返回0;
    
    }
    

我希望它有帮助。快乐编码!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文