MemDC in OnPaint()-function

孤者浪人 提交于 2019-12-10 12:08:16

问题


My OnPaint() function calls several other drawing functions.

 void CGraph::OnPaint ()
 {
    CPaintDC dc(this);
    // CMemDC DC(&dc);

    dc.SetViewportOrg (0, 400);
    dc.SetMapMode(MM_ISOTROPIC);
    dc.SetWindowExt(1000, 800);
    dc.SetViewportExt(1000, -800);

    ProcessData ();
    DrawCoordinateSystem (&dc);
    DrawGrid (&dc);
    DrawGraph (&dc);
}

Example of DrawCoordianteSystem:

void CGraph::DrawCoordinateSystem (CDC* pDC)
{
   CPen PenBlack (PS_SOLID, 1, RGB(0, 0, 0));
   pDC->SelectObject (PenBlack);
   // Rectangle round the system
   pDC->Rectangle (0, -400, 1000, 400);
   // Horizontal axis
   pDC->MoveTo (0, 0);
   pDC->LineTo (1000, 0);
   pDC->SetTextColor (RGB(0,0,0));
   pDC->SetBkColor (RGB(240, 240, 240));
   pDC->TextOut (1001, 0, L"0");
   // Vertical axis
   pDC->MoveTo (0, -400);
   pDC->LineTo (0, 400);
}

I now want to avoid flickering with CMemDC. However, i can't get it to work right. Can somebody show me how to implement CMemDC right?

Thanks


回答1:


You need to load the memory DC with a bitmap to then BitBlt to the screen DC.

Try something like:

CDC dcMem;
CBitmap bitmap;

dcMem.CreateCompatibleDC( pDC );
bitmap.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height())
CBitmap* pOldBitmap = dcMem.SelectObject(&bitmap);

... DO ALL YOUR DRAWING TO dcMem ...

pDC->BitBlt(rect.left, rect.top, rect.Width(), rect.Height(), &dcMem, 0, 0, SRCCOPY);

dcMem.SelectObject(pOldBitmap)



回答2:


To avoid flickers you should draw everything on CMemDC and BitBlt it to the real DC.

Secondly add windows message handler for WM_ERASEBKGND message and change the code to

BOOL CGraph::OnEraseBkgnd(CDC* pDC) 
{
    return TRUE;
}


来源:https://stackoverflow.com/questions/11448118/memdc-in-onpaint-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!