【发布时间】:2011-07-14 09:11:01
【问题描述】:
根据 MSDN Library 中的GetMessage API,它可能会在出现错误时返回 -1。文档提供了应该避免的常见错误的代码sn-p:
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);
}
}
我的问题是,在每个示例代码中,包括从 Visual Studio 创建的默认应用程序,来自 Microsoft,主消息循环如下所示:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
注意上面GetMessage的第二个参数是NULL。如果上面的代码是有效的,是不是意味着这里的GetMessage永远不会返回-1,这样就不需要处理返回值-1了?
【问题讨论】: