根据获取消息API http://msdn.microsoft.com/en-us/library/ms644936%28VS.85%29.aspx从 MSDN 库来看,出现错误时可能会返回 -1。该文档提供了应避免的常见错误的代码片段:
while (GetMessage( lpMsg, hWnd, 0, 0)) ...
文件说:
返回值-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);
}
}
我的问题是,在每个示例代码中,包括从 Microsoft 的 Visual Studio 创建的默认应用程序,主消息循环如下所示:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
请注意,上面 GetMessage 的第二个参数为 NULL。如果上面的代码有效,这是否意味着这里的 GetMessage 永远不会返回 -1,因此不需要处理返回值 -1 ?