windows的消息循环

发表于:2013-06-21来源:Csdn作者:terrycanny点击数: 标签:windows
GetMesssge函数的返回值可非零、零或-1,应避免如下代码出现: [cpp] view plaincopyprint? while (GetMessage( lpMsg, hWnd, 0, 0))

  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