windows消息循环

GetMesssge函数的返回值可非零、零或-1,应避免如下代码出现:

while (GetMessage( lpMsg, hWnd, 0, 0))
{
    TranslateMessage(&Msg); 
    DispatchMessage(&Msg); 
}

-1返回值的可能性表示这样的代码会导致致命的应用程序错误。

可修改为:

while(GetMessage(&Msg, NULL, 0, 0) > 0) 
{ 
    TranslateMessage(&Msg); 
    DispatchMessage(&Msg); 
} 

BOOL bRet;

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
}

你可能感兴趣的:(消息循环)