首页 > 代码库 > 双缓存显示问题以及图片

双缓存显示问题以及图片

void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
	if (!pdc)
		return;

	CClientDC dc(this);

	CRect rect; 
	GetClientRect(rect); 

	CDC MemDC;
	MemDC.CreateCompatibleDC(&dc); 

	CBitmap MemBitmap; 
	

	if(GetFileAttributes(m_strFileName) == INVALID_FILE_ATTRIBUTES)//显示文字
	{
		MemBitmap.CreateCompatibleBitmap(pdc,rect.right,rect.bottom); 

		CBitmap* pOldBitmap = (CBitmap*)MemDC.SelectObject(&MemBitmap);

		MemDC.FillSolidRect(&rect,RGB(0xFA, 0xFA, 0xFA)); 
		static CBrush brFrame(RGB(202, 202, 202));
		MemDC.FrameRect(rcBounds, &brFrame);

		MemDC.SetTextColor(RGB(128, 128, 128));

		MemDC.SetBkMode(TRANSPARENT);

		CString sTextOut(_T("未扫描到图片"));

		CRect rcTextOut(rect);
		const int iTextHeight = MemDC.DrawText(sTextOut, CRect(rcTextOut), DT_CALCRECT | DT_CENTER | DT_WORDBREAK | DT_EDITCONTROL);

		rcTextOut.DeflateRect(0, (rcTextOut.Height() - iTextHeight) / 2);

		MemDC.DrawText(sTextOut, rcTextOut, DT_CENTER | DT_SINGLELINE|DT_WORDBREAK | DT_EDITCONTROL);

		pdc->BitBlt(0, 0, rect.Width(), rect.Height(), &MemDC, 0, 0, SRCCOPY);

		MemDC.SelectObject(pOldBitmap);

		MemDC.DeleteDC();

		DeleteObject(MemBitmap); 

		return;
	}
	//显示图片

	

         

	/*HBITMAP hbmp;//LoadImage加载png图片

	hbmp = (HBITMAP)::LoadImage(AfxGetInstanceHandle(), 
		m_strFileName, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION|LR_LOADFROMFILE); 
	if( hbmp == NULL ) 
	{
		return;
	}*/

	CImage imgTemp;
	imgTemp.Load(m_strFileName);
	if ( MemBitmap.m_hObject ) MemBitmap.Detach();
	MemBitmap.Attach(imgTemp.Detach());

	//MemBitmap.Attach( hbmp );
	
	DIBSECTION ds;
	BITMAPINFOHEADER &bminfo = ds.dsBmih; 
	MemBitmap.GetObject( sizeof(ds), &ds );


	MemDC.SelectObject(&MemBitmap);    //连接位图

	pdc->SetStretchBltMode(HALFTONE);
	pdc->StretchBlt(0,0,rect.Width(),rect.Height(),&MemDC,0,0,bminfo.biWidth,bminfo.biHeight,SRCCOPY);

	MemDC.DeleteDC();

	DeleteObject(imgTemp);
}