//---------------窗口函数---------------
LRESULT CALLBACK WndProc(HWND hwnd,
 UINT message,
 WPARAM wParam,
 LPARAM lParam)
{
static int n = 0; switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;  case WM_PAINT:
// HDC dc;
// PAINTSTRUCT ps;
// dc = BeginPaint(hwnd, &ps);
// TextOut(dc, 0, 0, "hello", strlen("hello"));
// EndPaint(hwnd, &ps);
  TRACE("WM_PAINT : %d\n", n++);
  break; default:
return DefWindowProc(hwnd, message, wParam, lParam);
}

return 0;
}
对于WM_PAINT,为什么什么都不做的时候,发现不停的TRACE,CPU占用很高。
有BeginPaint和下面的操作的时候,就正常了。

解决方案 »

  1.   

    因为你的无效区域一直都存在,所以系统会不断的发送WM_PAINT到应用程序消息队列中
    BeginPaint

    EndPaint
    不能去掉
      

  2.   

    对于WM_PAINT,为什么什么都不做的时候,发现不停的TRACE,CPU占用很高。
    有BeginPaint和下面的操作的时候,就正常了。
    ============
    只有当存在无效区时才会触发 WM_PAINT消息,而调用BeginPaint才会消除掉无效区,因为你没有调用BeginPaint,所以无效区没有消除,于是系统一直发WM_PAINT消息。
      

  3.   

    MSDN上说的好像不是很清楚(反正我看的不是很懂了),又网上查了一下,说BeginPaint函数会自动把无效区域变成有效的区域。这样的话,问题就彻底明白了。
      

  4.   

    之前没有找到足够的信息,MSDN有这段话:BeginPaint sets the update region of a window to NULL. This clears the region, preventing it from generating subsequent WM_PAINT messages. If an application processes a WM_PAINT message but does not call BeginPaint or otherwise clear the update region, the application continues to receive WM_PAINT messages as long as the region is not empty. In all cases, an application must clear the update region before returning from the WM_PAINT message.