Re: Moving a graphic object without flicker
The answer to your question is "double buffering".
void MyView::OnPaint()
{
CPaintDC dc(this);
CDC MemDC;
MemDC.CreateCompatibleDC(&dc);
CBitmap MemBmp;
MemBmp.CreateCompatibleBitmap(&dc,...);
int SavedDC = MemDC.SaveDC();
//draw everything on the memory dc.
for (int i = 0; i < 1000, i++)
{
Objects[i].Draw(MemDC);
}
//the blit the entire thing on the screen
dc.BitBlt(0,0,Width,Height,&MemDC,0,0,SRCCOPY);
MemDC.RestoreDC(SavedDC);
}
Sometimes I go even a step further. I keep the memdc and the membmp as part
of the class, have a function called DrawItems() that I can call at anytime
to draw the images on the memory dc. And all OnPaint really does is bitblt
from the memory dc. This will save alot of time in the event that your
window gets covered and then uncovered while none of you objects have really
changed. This way you always have a snapshot of what's supposed to be drawn
on the screen, and all you do is bitblt on the screen.
AliR.
<stratpilot@gmail.com> wrote in message
news:1173743673.799575.161490@p10g2000cwp.googlegroups.com...
I am using MFC to draw some simple graphic objects that I move around
the display. I am using CDC::CPen, CDC::CBrush to draw and
CDC:FillSolidRect to erase. Basic idea is that I update the location,
draw, using CDC::ellipse for example, wait a bit erase using
FillSolidRect and repeat. This approach works but there is a
significant amount of flicker in the displayed objects. Is there a
smarter way to do this and still stay with MFC?
The code snippet below is called in a loop as follows:
{
ob.Draw(true);
ob.Draw(false);
Delay(10);
ob.Move(newLocation);
}
Thanks in advance,
Bob
void C2DMoveObject::Draw(bool Erase)
{
CClientDC* pDC = (CClientDC*)m_pDispWindow->GetDC();
if (Erase)
{
pDC->FillSolidRect(m_ShapeBoundRect + CRect(1,1,0,0), pDC-
GetBkColor());
}
else
{
CBrush* pOldBrush = pDC->SelectObject(&m_MainBrush);
CPen* pOldPen = pDC->SelectObject( &m_MainPen );
pDC->Ellipse(m_ShapeBoundRect);
pDC->SelectObject( pOldPen );
pDC->SelectObject(pOldBrush);
}
}