Win32双缓冲绘图

MFC双缓冲绘图

// 创建相匹配的DC
HDC hMemDC = ::CreateCompatibleDC(dc.m_hDC);
// 根据DC设置CDC*
CDC* pDC = CDC::FromHandle(hMemDC);
// 使用pDC绘制图形
DrawMenuText(dc);
// 获取客户区大小
CRect ClientRect;
GetClientRect(&ClientRect);
// 绘制缓冲区中的图片
::BitBlt(dc.m_hDC,0,0,ClientRect.right,ClientRect.bottom,hMemDC,0,0,SRCCOPY);
// 删除DC
::DeleteDC(hMemDC);

WinAPI双缓冲绘图

hdc = BeginPaint(hWnd, &ps);
HDC hMemDC = CreateCompatibleDC(hdc);
HBITMAP  hBmpMem = CreateCompatibleBitmap(hdc, 300, 200);
HBITMAP hPreBmp = (HBITMAP)SelectObject(hMemDC, hBmpMem);
// 使用开始绘图hMemDC
BitBlt(hdc, 50, 50, 300, 200, hMemDC, 0, 0, SRCCOPY);
SelectObject(hMemDC, hPreBmp);
DeleteObject(hBmpMem);
DeleteDC(hMemDC);
EndPaint(hWnd, &ps);

MFC双缓冲绘图完整示例

CPaintDC dc(this);
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
ASSERT(dcMem.GetSafeHdc());
CBitmap bmp;
CRect rc;
GetClientRect(&rc);
bmp.CreateCompatibleBitmap(&dc, rc.Width(), rc.Height());
ASSERT(bmp.GetSafeHandle());
CBitmap* pOldBitmap = (CBitmap*)dcMem.SelectObject(&bmp);
dcMem.FillSolidRect(&rc, GetSysColor(COLOR_3DFACE));
dcMem.SetTextColor(RGB(255, 0, 128));
dcMem.SetBkMode(TRANSPARENT);
CString strText(_T("Hello, World!"));
dcMem.DrawText(strText, &g_hWndRect, DT_CENTER | DT_SINGLELINE);
dc.BitBlt(0, 0, rc.Width(), rc.Height(), &dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject(pOldBitmap);
dcMem.DeleteDC();

留下评论