MFC双缓冲技术(Gdi+)

1.创建一个一个最简窗口(见前文)

2.在stdafx.h中添加对Gdi+的支持

#include<gdiplus.h>
#pragma comment(lib,"Gdiplus.lib")
using namespace Gdiplus;

在App类中添加三个私有成员

ULONG_PTR m_tokenGdiplus;
Gdiplus;:GdiplusStartupInput input;
Gdiplus::GdiplusStartupOutput output;
在其InitInstance函数实现中添加
Status s=GdiplusStartup(&m_tokenGdiplus,&input,&output);
3.在CMainFrame类中添加私有成员
Image* img;
RectF rc{0,10,200,300};
4。在CMainFrame的构造函数中添加

img=Image::FromFile(_T("res/图片名"));//图片放在res文件内


4.在CMainFrame类中添加OnPaint()函数(类向导),修改OnPaint()函数

void CMainFrame::OnPaint()
{
	HDC hdc = ::GetDC(GetSafeHwnd());
	CDC* pDC = CClientDC::FromHandle(hdc);//获取窗口DC
	CDC m_dcMemory;//缓冲DC
	CBitmap bmp;//缓冲位图
	bmp.CreateCompatibleBitmap(pDC, rc.Width,rc.Height);//创建位图
	m_dcMemory.CreateCompatibleDC(pDC);//缓冲DC初始化
	CBitmap* pOldBitmap = m_dcMemory.SelectObject(&bmp);//将位图和缓存DC关联
									   //gdi+绘图
	Graphics gh(m_dcMemory.GetSafeHdc());

	//绘图代码
	{

		gh.DrawImage(img, rc);
	}
	::BitBlt(hdc, 0, 0, rc.Width, rc.Height, m_dcMemory.GetSafeHdc(), 0, 0, SRCCOPY);//将缓存DC的内容复制到屏幕
	pDC->DeleteDC();
}

调试



猜你喜欢

转载自blog.csdn.net/qq_33012981/article/details/80374045
今日推荐