相关疑难解决方法(0)

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

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

while (GetMessage( lpMsg, hWnd, 0, 0)) ...
Run Code Online (Sandbox Code Playgroud)

文件说:

-1返回值的可能性意味着此类代码可能导致致命的应用程序错误.相反,使用这样的代码:

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

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

while (GetMessage(&msg, NULL, 0, 0))
{
    TranslateMessage(&msg);
    DispatchMessage(&msg);
}
Run Code Online (Sandbox Code Playgroud)

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

c++ windows winapi

6
推荐指数
1
解决办法
1427
查看次数

标签 统计

c++ ×1

winapi ×1

windows ×1