2013-01-21 25 views
4

我是中國學生,這是我在國外論壇上提出的第一個問題。 我寫了兩個程序,一個可以正常運行,但另一個失敗。關於windows GDI的困惑。新手程序員

這裏是正常的:

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

     if(fIsTime) 
      ShowTime(hdc, &st); 
     else 
      ShowDate(hdc, &st); 

     EndPaint (hwnd, &ps) ; 
     return 0 ; 

這裏是一個失敗:

case WM_PAINT: 
     hdc = BeginPaint (hwnd, &ps) ; 
     hdcMem = ::CreateCompatibleDC(hdc); 
     hBitmap = ::CreateCompatibleBitmap(hdc, cxClient, cyClient); 
     ::SelectObject(hdcMem, hBitmap); 

     if(fIsTime) 
      ShowTime(hdcMem, &st); 
     else 
      ShowDate(hdcMem, &st); 
     ::BitBlt(hdcMem, 0, 0, cxClient, cyClient, hdc, 0, 0, SRCCOPY); 

     ::DeleteObject(hBitmap); 
     ::DeleteDC(hdcMem); 
     EndPaint (hwnd, &ps) ; 
     return 0 ; 

兩個代碼之間的唯一區別是WM_Paint代碼塊,後者不能顯示任何內容。我對後面的代碼中錯誤的位置感到困惑?

回答

5

您最大的問題是您有撥打BitBlt電話的來源和目標DC換機。第一個參數應該是目的地,而不是來源。

此外,當您將位圖設置爲DC時,您必須記住返回給您的舊值並在完成後將其恢復。

嘗試以下操作:

hdc = BeginPaint (hwnd, &ps) ; 
    hdcMem = ::CreateCompatibleDC(hdc); 
    hBitmap = ::CreateCompatibleBitmap(hdc, cxClient, cyClient); 
    hbmpOld = ::SelectObject(hdcMem, hBitmap); 

    if(fIsTime) 
     ShowTime(hdcMem, &st); 
    else 
     ShowDate(hdcMem, &st); 
    ::BitBlt(hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY); 

    ::SelectObject(hdcMem, hbmpOld); 
    ::DeleteObject(hBitmap); 
    ::DeleteDC(hdcMem); 
    EndPaint (hwnd, &ps) ; 
    return 0 ; 
+0

哦,我扭轉目的地和來源..非常感謝你。 – nvfumayx