为什么下列代码执行的时候一直闪烁,但是使用注释中的代码是没有这个现象LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     HDC         hdc ;
     PAINTSTRUCT ps ;
     RECT        rect ;     switch (message)
     {
     case WM_PAINT://          hdc = BeginPaint (hwnd, &ps) ;
//          GetClientRect (hwnd, &rect) ;
//          DrawText (hdc, TEXT ("你好,欢迎你!"), -1, &rect,
//                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
//          EndPaint (hwnd, &ps) ;          hdc=GetDC(hwnd);
  GetClientRect (hwnd, &rect) ;
          DrawText (hdc, TEXT ("你好,欢迎你!"), -1, &rect,
                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
  ReleaseDC(hwnd,hdc);
  return 0 ;     case WM_DESTROY:          PostQuitMessage (0) ;          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

解决方案 »

  1.   

    在Charles Petzold的《windows 程序设计》中提到:在处理WM_PAINT消息时,使用BeginPaint和EndPaint调用。同时Windows程序还可以在处理非WM_PAINT消息时获取设备描述表句柄:
    hdc=GetDC(hwnd);
    [other program lines]
    ReleaseDC(hwnd,hdc);
      

  2.   

    在Charles Petzold的《windows 程序设计》中提到:在处理WM_PAINT消息时,使用BeginPaint和EndPaint调用。同时Windows程序还可以在处理非WM_PAINT消息时获取设备描述表句柄:
    hdc=GetDC(hwnd);
    [other program lines]
    ReleaseDC(hwnd,hdc);
      

  3.   

    难道 hdc=GetDC(hwnd);
    [other program lines]
    ReleaseDC(hwnd,hdc);不可以在 WM_PAINT 中使用吗
      

  4.   

    以下节选自 msdn July 2001The BeginPaint function automatically sets the clipping region of the device context to exclude any area outside the update region. The update region is set by the InvalidateRect or InvalidateRgn function and by the system after sizing, moving, creating, scrolling, or any other operation that affects the client area. If the update region is ed for erasing, BeginPaint sends a WM_ERASEBKGND message to the window. An application should not call BeginPaint except in response to a WM_PAINT message. Each call to BeginPaint must have a corresponding call to the EndPaint function. If the caret is in the area to be painted, BeginPaint automatically hides the caret to prevent it from being erased. If the window's class has a background brush, BeginPaint uses that brush to erase the background of the update region before returning. 看过以上信息,你就应该明白,BeginPaint为我们做了很多,而GetDC就没有,BeginPaint自动为我们选择了clipping region,而不是将整个客户区重画,还有它会隐藏光标,防止其被重画,以及自动调用背景画刷,这些是GetDC没有做的 
      

  5.   

    BeginPaint自动为我们选择了clipping region,而不是将整个客户区重画,这就很大程度上减弱了闪烁