MFC雙緩衝代碼,拿去就能用,非常簡單

BOOL OnEraseBkgnd( CDC * pDC )
{
	//獲取窗口大小
	CRect rtWindow;
	GetWindowRect(&rtWindow);

	//////////////////////////////////////////////////////////////////////////
	CDC MemDC; //首先定義一個內存顯示設備對象
	CBitmap MemBitmap;//定義一個位圖對象
	MemDC.CreateCompatibleDC(NULL); //創建兼容設備dc
	MemBitmap.CreateCompatibleBitmap(pDC,rtWindow.Width(),rtWindow.Height());
	CBitmap *pOldBit=MemDC.SelectObject(&MemBitmap);
	MemDC.FillSolidRect(0,0,rtWindow.Width(),rtWindow.Height(),COLOR_BACK);//填充初始顏色
	//===start===//	
        //獲取客戶區域
	CRect rtClient;
	GetClientRect(&rtClient);
	//畫背景
	CBrush brushBK;
	brushBK.CreateSolidBrush(COLOR_BACK); 
	MemDC.FillRect(rtClient,&brushBK);
	//畫內容
	DrawItem(&MemDC);
	//===end===//
	pDC->BitBlt(0,0,rtWindow.Width(),rtWindow.Height(),&MemDC,0,0,SRCCOPY); //繪圖到真實顯示
	//////////////////////////////////////////////////////////////////////////

	//清理  
        MemDC.SelectObject(pOldBit);
	MemBitmap.DeleteObject();  
	MemDC.DeleteDC();  

	return TRUE;
}


在 //===start===//  和  //===end===// 之間用MemDC畫你要畫的東西就行了





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章