GetMesssge函數的返回值可非零、零或-1,應避免如下代碼出現:
[cpp] view plaincopyprint?
while (GetMessage( lpMsg, hWnd, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
while (GetMessage( lpMsg, hWnd, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
-1返回值的可能性表示這樣的代碼會導致致命的應用程序錯誤。
可修改為:
[cpp] view plaincopyprint?
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
或
[cpp] view plaincopyprint?
BOOL bRet;
while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{
if (bRet == -1)
{
// handle the error and possibly exit
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
原文轉自:http://blog.csdn.net/terrycanny/article/details/8622492