GDI+双缓存画图是这样用吗,有什么问题吗?出现现象:发现有时会画不出来。重启软件也是,不知何解?概率3%左右。
void CDlgMain::OnPaint() //CDlgMain为对话框
{
CPaintDC dc(this); // device context for painting...
PageDraw();
...
}void CDlgMain::PageDraw()
{
CRect rect;
GetClientRect(&rect);
int ndrawwidth=rect.Width()-m_nDrawstartpos-m_nbottombtwidth;//画图区域宽度 m_ngridShowcount=ndrawwidth/m_ngridpels;
m_nscaleShowcount=m_ngridShowcount*4;
if( m_ngridShowcount == 0 )return;
m_nPagecount=m_ngridcount/m_ngridShowcount;
if( m_ngridcount%m_ngridShowcount != 0 )
{
m_nPagecount=m_nPagecount+1;
} if (m_nPagecount == 0)
{
m_nPagecount = 1;
}
int nstartpos=rect.left + m_nDrawstartpos;
int nendpos=nstartpos+ndrawwidth;
int ntoppos=rect.top+(rect.Height() - m_nButtonDown)+5;
int nbottompos= ntoppos+20; m_nendgrid =m_nstartgrid+m_ngridShowcount-1;//结束格子
m_rtgrid.top=ntoppos-3;
m_rtgrid.left=nstartpos;
m_rtgrid.bottom=m_rtgrid.top+nbottompos-ntoppos+6;
Bitmap bitmap( rect.Width(),rect.Height() );
Graphics myGraphics(&bitmap);
myGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
//--------------------------背景色-------------------------------------
Rect rtback(m_rtgrid.left-3,m_rtgrid.top-2,m_rtgrid.Width()+4,m_rtgrid.Height()+6);
LinearGradientBrush Brush(
Point(0, rtback.Y-1),
Point(0, rtback.GetBottom()),
g_ObjUiColorData.m_ClrDlgTopMainBack4,
g_ObjUiColorData.m_ClrDlgTopMainBack5);
myGraphics.FillRectangle(&Brush,rtback) ;
//--------------------------------------------------------
CDC *pdc=this->GetDC();
Graphics graphicsDisplay(pdc->GetSafeHdc());
Status bsu=graphicsDisplay.DrawImage(&bitmap,rect.left,rect.top,rect.Width(),rect.Height());
// ReleaseDC(pdc); 源方式
graphicsDisplay.ReleaseHDC( pdc->GetSafeHdc() ); //这个地方要这样释放}
void CDlgMain::OnPaint() //CDlgMain为对话框
{
CPaintDC dc(this); // device context for painting...
PageDraw();
...
}void CDlgMain::PageDraw()
{
CRect rect;
GetClientRect(&rect);
int ndrawwidth=rect.Width()-m_nDrawstartpos-m_nbottombtwidth;//画图区域宽度 m_ngridShowcount=ndrawwidth/m_ngridpels;
m_nscaleShowcount=m_ngridShowcount*4;
if( m_ngridShowcount == 0 )return;
m_nPagecount=m_ngridcount/m_ngridShowcount;
if( m_ngridcount%m_ngridShowcount != 0 )
{
m_nPagecount=m_nPagecount+1;
} if (m_nPagecount == 0)
{
m_nPagecount = 1;
}
int nstartpos=rect.left + m_nDrawstartpos;
int nendpos=nstartpos+ndrawwidth;
int ntoppos=rect.top+(rect.Height() - m_nButtonDown)+5;
int nbottompos= ntoppos+20; m_nendgrid =m_nstartgrid+m_ngridShowcount-1;//结束格子
m_rtgrid.top=ntoppos-3;
m_rtgrid.left=nstartpos;
m_rtgrid.bottom=m_rtgrid.top+nbottompos-ntoppos+6;
Bitmap bitmap( rect.Width(),rect.Height() );
Graphics myGraphics(&bitmap);
myGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
//--------------------------背景色-------------------------------------
Rect rtback(m_rtgrid.left-3,m_rtgrid.top-2,m_rtgrid.Width()+4,m_rtgrid.Height()+6);
LinearGradientBrush Brush(
Point(0, rtback.Y-1),
Point(0, rtback.GetBottom()),
g_ObjUiColorData.m_ClrDlgTopMainBack4,
g_ObjUiColorData.m_ClrDlgTopMainBack5);
myGraphics.FillRectangle(&Brush,rtback) ;
//--------------------------------------------------------
CDC *pdc=this->GetDC();
Graphics graphicsDisplay(pdc->GetSafeHdc());
Status bsu=graphicsDisplay.DrawImage(&bitmap,rect.left,rect.top,rect.Width(),rect.Height());
// ReleaseDC(pdc); 源方式
graphicsDisplay.ReleaseHDC( pdc->GetSafeHdc() ); //这个地方要这样释放}
解决方案 »
- 关于getline()的用法
- 寻求 CSocket 在线程中关闭并重新连接问题的完美解决方案!
- 多个定时器之间的协调问题,请高手指教!
- 小问题
- 图象旋转的问题?
- 菜鸟的疑惑:len = recv(ServerSock,buf,US_MAXSIZE,0);recv(ServerSock,(char*)&bitSize,sizeof(bitSize)+1,0);有什么区别?
- 请问从GB2312 TO UCS2怎么转换的
- 请大家帮我看一段代码?把CStringArray放入ListCtrl。
- 位图颜色格式 是指的什么??
- 看来和版不错啊!
- ADO timeout Win server2003
- socket传输视频数据
资源不释放会出错。
不用自己创建DConpaint函数,利用的是CPaintDC
CDC *pdc=this->GetDC(); //error 不一定得到CPaintDC
GetClientRect(g_hwnd,&rc);
Bitmap bmp(int(rc.right),int(rc.bottom));、
Graphics bmpGraphics(&bmp);
bmpGraphics.SetSmoothingMode(SmoothingModeAntiAlias);
/*Drawing on bitmap*/
SolidBrush bkBrush(Color(0,0,0));
bmpGraphics.FillRectangle(&bkBrush,0,0,rc.right,rc.bottom);
/*Drawing on DC*/
Graphics graphics(hdc);
/*Important! Create a CacheBitmap object for quick drawing*/
CachedBitmap cachedBmp(&bmp,&graphics);
graphics.DrawCachedBitmap(&cachedBmp,0,0);