Confusion about Windows GDI. Novice programmer

I am a Chinese student, and here is my first question I asked in an external forum. I wrote two programs, you can work fine, but the other failed.

Here is the normal one: <i>

case WM_PAINT: hdc = BeginPaint (hwnd, &ps) ; if(fIsTime) ShowTime(hdc, &st); else ShowDate(hdc, &st); EndPaint (hwnd, &ps) ; return 0 ; 

Here's the bad one: <i>

  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 ; 

The only difference between the two codes is the WM_Paint code WM_Paint , the latter cannot display anything. I am confused by where is the error in the last code?

+4
source share
1 answer

The biggest problem is that you have source and target domain controllers replaced with a BitBlt call. The first parameter should be the destination, not the source.

In addition, when you set the bitmap to DC, you must remember the old value that is returned to you and restore it when you are done.

Try the following:

  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 ; 
+5
source

All Articles