قطعه کد زیر با win32 هست، برای MFC نیز توابع معادل با همین نام ها وجود دارد.
- در برنامه WM_ERASEBKGND را 1 قرار دادیم تا برنامه خودش پس زمینه رسم نکند.
- با CreateCompatibleDC و CreateCompatibleBitmap حافظه مورد نظر برای عکس را می گیریم.
- سپس با Rectangle یک مستطیل در این حافظه رسم می کنیم.
- در پایان نیز با BitBlt کل عکس را از حافظه به تصویر کپی می کنیم و نمایش می دهیم تا flicker نداشته باشیم.
- اگر انیمیشن هست، تایمر بسازید و هر دفعه WM_PAINT را فراخوانی کنید و مکان مستطیل ها را تغییر دهید.
case WM_ERASEBKGND:
return (LRESULT)1;
// ----------------------------------------------------------------
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
//------------------- Method one (Without flickers)-----------------------------
HDC hdcMem;
HBITMAP hbmMem, hbmOld;
HBRUSH hbrBkGnd;
// Get the size of the client rectangle.
GetClientRect(hWnd, &rc);
// Create a compatible DC.
hdcMem = CreateCompatibleDC(hdc);
// Create a bitmap big enough for our client rectangle.
hbmMem = CreateCompatibleBitmap(hdc, rc.right-rc.left,
rc.bottom-rc.top);
// Select the bitmap into the off-screen DC.
hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMem);
// Erase the background.
hbrBkGnd = CreateSolidBrush(GetSysColor(COLOR_WINDOW));
FillRect(hdcMem, &rc, hbrBkGnd);
DeleteObject(hbrBkGnd);
// Render the image into the offscreen DC.
SetBkMode(hdcMem, TRANSPARENT);
SetTextColor(hdcMem, GetSysColor(COLOR_WINDOWTEXT));
DrawText(hdcMem, TEXT("HELLO"), -1, &rc, DT_CENTER);
// Draw Rectangle into the offscreen DC.
hbr1 = CreateSolidBrush (RGB(0,0 ,155));
hbr2 = (HBRUSH) SelectObject (hdcMem, hbr1);
Rectangle (hdcMem, 100, 100, 300, 300);
// Blt the changes to the screen DC.
BitBlt(hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top,
hdcMem, 0, 0, SRCCOPY);
SelectObject (hdc, hbr2);
// Done with off-screen bitmap and DC.
SelectObject(hdcMem, hbmOld);
DeleteObject ( hbr1 );
DeleteObject(hbmMem);
DeleteDC(hdcMem);
//-------------------------------------------------------
// TODO: Add any drawing code here...
EndPaint(hWnd, &ps);
break;