Jin

In me the tiger sniffs the rose.

21 Nov 2010

GDI+ double buffer 绘图

pool


问题

我在使用GDI+绘制控件的时候,如果频繁的刷新会很容易出现闪烁的问题,这是由于每一次画布的提交,都会造成窗口的刷新。

解决思路

于是我想能不能把多次提交暂存在一张虚拟的画布中,最后统一提交给窗口。

1. 首先在自己的频繁刷新的函数中创建一张后台画布:

CClientDC dc(this);
Graphics graphics(dc.GetSafeHdc());
CRect rect;
GetClientRect(&rect);
Bitmap bmp(rect.Width(),rect.Height());
m_pGraph=Graphics::FromImage(&bmp);

2. 然后将所有需要绘制到屏幕中的图片,文字等等会绘制到这张虚拟的画布中:

DrawImageToBuffer(_T("bg.jpg"),0,0,600,400);
DrawTextToBuffer(_T("bg1.jpg"),300,0,_T("测试文字"),(int)wcslen(_T("测试文字")),12);
float currTime  = (float)timeGetTime();
float timeDelta = (currTime - lastTime)*0.001f;
totalTime+=timeDelta;
fcount+=1.0f;
TCHAR textTemp[30];
wsprintf(textTemp,_T("fps:%d/s\n"),(int)(fcount/totalTime));
DrawTextToBuffer(_T("bg2.jpeg"),300,30,textTemp,(int)wcslen(textTemp),30);

3. 最后提交我们的画布到前台窗口中去:

graphics.DrawImage(&bmp,rect.left,rect.top,rect.right,rect.bottom);
m_pGraph=NULL;

4. OK,结束。按照这种方式,现在就不会再闪烁了。

注意:DrawImageToBufferDrawTextToBuffer 是我对GDI+封装的函数,完整代码如下:

// GDI+DoubleBufferDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "GDI+DoubleBuffer.h"
#include "GDI+DoubleBufferDlg.h"
#include <ctime>
#include<windows.h>
#include <MMSystem.h>
#pragma comment(lib,"Winmm.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
static float lastTime=0;
static float totalTime=0;
static float fcount=0;
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CGDIDoubleBufferDlg 对话框




CGDIDoubleBufferDlg::CGDIDoubleBufferDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CGDIDoubleBufferDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGDIDoubleBufferDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CGDIDoubleBufferDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CGDIDoubleBufferDlg 消息处理程序

BOOL CGDIDoubleBufferDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	lastTime = (float)timeGetTime();
	SetTimer(1,1,NULL);
	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

void CGDIDoubleBufferDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CGDIDoubleBufferDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
//
HCURSOR CGDIDoubleBufferDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
void CGDIDoubleBufferDlg::DrawTextToBuffer(TCHAR* pFName,float x,float y,TCHAR* pText ,int nLength,int nSize,int type)
{


	FontFamily fontFamily(_T("黑体"));
	Font font(&fontFamily, nSize, FontStyleRegular, UnitPoint);
	PointF pointF(x, y);
	Image image(pFName);
	TextureBrush tBrush(&image);
	StringFormat stringFormat;

		stringFormat.SetAlignment(StringAlignmentCenter); 
	
	m_pGraph->DrawString(pText, nLength, &font, pointF,&stringFormat,&tBrush);


}
void CGDIDoubleBufferDlg::DrawImageToBuffer(TCHAR* pFName,float x,float y,float w,int h)
{
	RectF rt;
	rt.X=x;
	rt.Y=y;
	rt.Width=w;
	rt.Height=h;
	Image image(pFName,FALSE);
	m_pGraph->DrawImage(&image, rt);
}
void CGDIDoubleBufferDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	if (nIDEvent==1)
	{
		
		CClientDC dc(this);
		Graphics graphics(dc.GetSafeHdc());
		CRect rect;
		GetClientRect(&rect);
		Bitmap bmp(rect.Width(),rect.Height());
		m_pGraph=Graphics::FromImage(&bmp);

		DrawImageToBuffer(_T("bg.jpg"),0,0,600,400);

		DrawTextToBuffer(_T("bg1.jpg"),300,0,_T("测试文字"),(int)wcslen(_T("测试文字")),12);

	

		float currTime  = (float)timeGetTime();
		float timeDelta = (currTime - lastTime)*0.001f;
		totalTime+=timeDelta;
		fcount+=1.0f;
		TCHAR textTemp[30];
		
		wsprintf(textTemp,_T("fps:%d/s\n"),(int)(fcount/totalTime));
		DrawTextToBuffer(_T("bg2.jpeg"),300,30,textTemp,(int)wcslen(textTemp),30);

		graphics.DrawImage(&bmp,rect.left,rect.top,rect.right,rect.bottom);
		m_pGraph=NULL;
		

		lastTime = currTime;

	}
	CDialog::OnTimer(nIDEvent);
}