首页 > 代码库 > wxWidgets第十二课 wxBufferedPaintDC OnPaint函数中的双缓存DC
wxWidgets第十二课 wxBufferedPaintDC OnPaint函数中的双缓存DC
说明
当使用DC进行图形渲染的过程中,会出现闪烁的问题,主要是图形是实时渲染的,如果先渲染在一块内存中,然后渲染完毕之后,才一次性贴图到屏幕上,就可以避免闪烁的问题。因此引入wxBufferedPaintDC。wxBufferedPaintDC继承自wxBufferedDC,wxPaintDC是其私有成员变量,因此该类只能够在wxPaintEvent事件中使用,也就是只能在OnPaint( wxPaintEvent& event )函数中使用。
为了使用该类,需要包含头文件#include "wx/dcbuffer.h",目前并没有被包含在wx/wx.h文件中,可以直接添加,避免每一次手写
例子
#include "wx/dcbuffer.h"
void CDownLinkPlaybackSliderCtrl::OnPaint( wxPaintEvent& event )
{
wxBufferedPaintDC dc(this);
dc.SetBrush(*wxWHITE_BRUSH);
dc.Clear();
wxSize size = this->GetSize();
int width = size.GetWidth();
int height = size.GetHeight();
int middleHeight = height/2;
dc.SetPen(wxPen(*wxBLACK, 2));
// dc.DrawLine(0, middleHeight-1, width, middleHeight-1);
// dc.DrawLine(0, middleHeight+1, width, middleHeight+1);
dc.SetBrush(*wxBLACK_BRUSH);
dc.DrawRectangle(2, middleHeight-2, width-4, 4);
int topDialHeight = middleHeight-2;
int buttonDialHeight = middleHeight+2;
int averageGap = (width-4)/25;
int count = 0;
for (int i=2; i<width-4; i=i+averageGap)
{
dc.DrawLine(i, topDialHeight, i, topDialHeight-2);
dc.DrawLine(i, buttonDialHeight, i, buttonDialHeight+2);
if (count%4==0)
{
char szText[4] ={0};
sprintf(szText, "%d", count);
wxRect rect(i-3, topDialHeight-2-3, 3, 3);
dc.DrawLabel(szText, rect, 1);
}
count++;
}
dc.SetBrush(*wxWHITE_BRUSH);
dc.DrawRectangle(m_sliderLen-3, middleHeight - 2, 4, 6);
}
wxWidgets第十二课 wxBufferedPaintDC OnPaint函数中的双缓存DC