gpt4 book ai didi

c++ - GetMessage() 会在主消息循环中返回 -1 吗?

转载 作者:可可西里 更新时间:2023-11-01 13:18:36 25 4
gpt4 key购买 nike

根据GetMessage API来自 MSDN Library,它可能会在出现错误时返回 -1。该文档提供了应避免的常见错误的代码片段:

while (GetMessage( lpMsg, hWnd, 0, 0)) ...

文档说:

The possibility of a -1 return value means that such code can lead to fatal application errors. Instead, use code like this:

BOOL bRet;
while( (bRet = GetMessage( &msg, hWnd, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

我的问题是,在每个示例代码中,包括从 Visual Studio 创建的默认应用程序,来自 Microsoft,主消息循环如下所示:

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

注意上面GetMessage的第二个参数是NULL。如果上面的代码有效,这是否意味着这里的 GetMessage 永远不会返回 -1,因此不需要处理 -1 的返回值?

最佳答案

您应该遵循 GetMessage() 的 MSDN 文档中指定的规则。这样做很轻松,而且您的代码中不会散布大量消息循环。

Visual Studio 团队与 Windows 团队是分开的,他们犯了和其他人一样的错误!

事实上,我无法想象 GetMessage() 会返回一个错误,但这是错误处理的本质——这并不意味着您不应该正确处理错误。

关于c++ - GetMessage() 会在主消息循环中返回 -1 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5288285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com