2013-06-05 41 views
0

我遇到的問題是當我繪製線條時,屏幕每次重繪都會閃爍。我不能完全弄清楚如何不讓它閃爍。我知道當我按下左鍵移動鼠標時,我每秒重繪客戶區域數百次就會產生閃爍,但我如何才能解決這個問題?繪製線條時客戶區域閃爍

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, 
          WPARAM wParam, LPARAM lParam) 
    { 
    static HDC  hdc; 
    PAINTSTRUCT  ps; 
    RECT   rect; 
    RECT   size; 
    static POINT point1; 
    static POINT point2; 
    static HBRUSH origBrush; 
    static bool drawingLine = false; 

    switch (message) 
     { 

     case WM_CREATE: 
      origBrush = CreateSolidBrush(RGB(0, 0 , 0)); 

      break; 

      case WM_PAINT: 
       hdc = BeginPaint (hwnd, &ps); 

       GetClientRect (hwnd, &rect); 

       GetWindowRect(hwnd, &size); 

       Rectangle(hdc, 10, 10, 80, 80); 

       if(drawingLine == true) 
       { 
        MoveToEx(hdc, point1.x, point1.y, NULL); 
        LineTo(hdc, point2.x, point2.y); 
       } 

       EndPaint (hwnd, &ps); 
       return 0; 

       //Has all the commands that exist in your program. 
      case WM_COMMAND: 
       switch(LOWORD(wParam)) 
       { 
       case ID_COLOR_RED: 
        break; 

       case ID_COLOR_BLUE: 
        break; 

       case ID_COLOR_BLACK: 
        break; 

       case ID_THICKNESS_1: 
        break; 

       case ID_THICKNESS_2: 
        break; 

       case ID_THICKNESS_3: 
        break; 

       } 
       break; 

      case WM_LBUTTONDOWN: 
       drawingLine = true; 
       SelectObject(hdc, GetStockObject(BLACK_BRUSH)); 
       point1.x = LOWORD(lParam); 
       point1.y = HIWORD(lParam); 
       MoveToEx(hdc, point1.x, point1.y, NULL); 
       break; 

      case WM_MOUSEMOVE: 
       point2.x = LOWORD(lParam); 
       point2.y = HIWORD(lParam); 
       InvalidateRect(hwnd, NULL, 1); 
       break; 

      case WM_LBUTTONUP: 
       point2.x = LOWORD(lParam); 
       point2.y = HIWORD(lParam); 

       drawingLine = false; 
       break; 

       //Causes the program to exit. 
      case WM_DESTROY: 
       PostQuitMessage(0); 
       break; 
     } 
+0

查找雙緩衝。 – chris

+0

如果您沒有處理'WM_ERASEBKGND',那麼系統將在您每次使其失效時清除整個客戶區。 –

回答

2

主要要做的是應對WM_ERASEBKGND並返回true,以防止後臺的默認重畫。

然後,您需要在繪製新的線之前清除舊線。舉例來說,您可以保存位於該行下方的位,繪製它們,然後在繪製行之前還原這些像素(並且只有這些像素),因此當您需要擦除它時,您可以恢復這些像素以將其擦除。

明顯的選擇(通常比較簡單,理論上至少慢一點)是使用雙緩衝。大致繪製您現在的繪圖,但要顯示在離屏位圖上。然後,當它完全從BitBlt完成到屏幕。