WM_ENDSESSION 消息有问题

发布于 2024-11-02 13:15:11 字数 637 浏览 10 评论 0原文

我的 WM_ENDSESSION 消息有问题。也就是说,当 WM_ENDSESSION 消息发送时,我想退出应用程序的主循环(WindowProc)...所以,我写了类似的内容:

LRESULT CALLBACK windowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    //...       
    case WM_QUERYENDSESSION: return TRUE;       
    case WM_ENDSESSION: 
         if(wParam) PostQuitMessage(0);         
    break;    
    default:
        return DefWindowProc(hwnd,msg,wParam,lParam);
    }

    return 0;
}

...,但它不起作用- 应用程序没有退出主循环...

我在 msdn 上读到了有关 WM_QUERYENDSESSION 和 WM_ENDSESSION 的信息,但我找不到任何有用的信息...

知道吗,错误在哪里?

I have a problem with WM_ENDSESSION message. Namely I would like to exit from the main loop of the application (WindowProc) when the WM_ENDSESSION message is sending... So, I wrote something like that:

LRESULT CALLBACK windowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    //...       
    case WM_QUERYENDSESSION: return TRUE;       
    case WM_ENDSESSION: 
         if(wParam) PostQuitMessage(0);         
    break;    
    default:
        return DefWindowProc(hwnd,msg,wParam,lParam);
    }

    return 0;
}

..., but it doesn't work - application doesn't exit the main loop...

I read about WM_QUERYENDSESSION and WM_ENDSESSION on msdn, but I couldn't find any helpful information...

Any idea, where is the mistake?

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

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

发布评论

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

评论(3

旧人九事 2024-11-09 13:15:11

我不认为调用 PostQuitMessage 来响应 WM_QUERYENDSESSION 是错误的。

WM_ENDSESSION 是世界末日。此时已经太晚了,无法将工作推迟到稍后的时间(调用 PostQuitMessage)。现在就做,否则你永远没有机会做。另外,考虑一下你在做什么。正如 Raymond Chen 曾经说过的那样,“[响应 WM_ENDSESSION 来清理应用程序]就像在拆除建筑物之前花时间蒸汽清洁地毯一样。浪费精力。”

WM_QUERYENDSESSION 授予您的窗口与用户交互的最后机会。您已代表用户决定您的应用程序将终止,并且您希望优雅地退出,因此这是您安排它的最后机会。

已更新

我不知道这是否适用于响应 WM_QUERYENDSESSION 的 PostQuitMessage。 MSDN 文档指出,“在系统处理 WM_QUERYENDSESSION 消息的结果之后,WM_ENDSESSION 消息发送到应用程序。”

已发送意味着消息泵无法破解消息。当然,即使是文档作者也经常混淆已发送已发布

I don't think it's wrong to call PostQuitMessage in response to WM_QUERYENDSESSION.

WM_ENDSESSION is the end of the world. It's too late at that point to postpone work until a later time (calling PostQuitMessage). Do it now, or you'll never get a chance to do it. Also, consider what you are doing. As Raymond Chen once put it, "[cleaning up your app in response to WM_ENDSESSION is] like taking the time to steam-clean the rugs before you demolish the building. Wasted effort."

WM_QUERYENDSESSION grants your window a last-chance to interact with the user. You have decided on behalf of the user that your app will die and you want to gracefully exit, so this is your last opportunity to schedule it.

Updated

I don't know that will even work to PostQuitMessage in response to WM_QUERYENDSESSION. The MSDN docs state, "The WM_ENDSESSION message is sent to an application after the system processes the results of the WM_QUERYENDSESSION message."

Sent implies that the message pump doesn't get a crack at messages. Of course, even the doc authors often confuse sent and posted.

吻泪 2024-11-09 13:15:11

您不需要任何特殊处理。只需调用 DefWindowProc 而不是处理这些消息。

You don't need any special handling. Just call DefWindowProc instead of handling these messages.

朕就是辣么酷 2024-11-09 13:15:11

如果您在 main() 函数中,我会放置

switch(msg)
{
    //...  
    case WM_ENDSESSION:  
        if(wParam) PostQuitMessage(0);  
        return 0;  
    //...  
}

return 0; 应该退出程序

I would put

switch(msg)
{
    //...  
    case WM_ENDSESSION:  
        if(wParam) PostQuitMessage(0);  
        return 0;  
    //...  
}

putting return 0; should exit the program, if you are in the main() function

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