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() ); //这个地方要这样释放}

解决方案 »

  1.   

    // ReleaseDC(pdc); 源方式这行干嘛要注释掉?
    资源不释放会出错。
      

  2.   

    前面有paint dc,干嘛还要Getdc? 直接当作参数传进来用就行了
      

  3.   

    void PageDraw(CDC& dc)
      

  4.   

    up 
    不用自己创建DConpaint函数,利用的是CPaintDC
    CDC *pdc=this->GetDC(); //error  不一定得到CPaintDC
      

  5.   

    RECT rc;
    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);
      

  6.   

    自己获得dc,是因为有多处调用。OnPaint() 只是其中一处。   CDC *pdc=this->GetDC();这样不能正确获取对话框的dc吗
      

  7.   

    我平常都是再加上GDI的双缓冲