Warning
Because the return value can be nonzero, zero, or -1, avoid code like this:
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:
Hide Example
BOOL bRet;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Windows消息循环最佳实践
本文介绍了在Windows应用程序中正确使用消息循环的方法,避免了因GetMessage返回-1而导致的应用程序错误。通过示例代码展示了如何处理不同返回值,并提供了解决方案。
634





