Double Buffering MFC HELP!

Started by
2 comments, last by brasslips2 15 years, 7 months ago
Hello everyone I'm working on a project in mfc and I'm trying to remove the flickering. However, I'm having a ton of trouble and can't seem to figure out how to do it. I've looked at tons of tutorials and searched like every page of google about it and couldn't find anything that would help me or would work. I know i have to return true in the onEraseBckrnd function and overload the onDraw function from what I've read. However I can't seam to figure out how to set up this "view" class that everyone seems to talk about but doesn't have a clear example of what it is. So basically I can't really overload these functions because I don't know how to do it with the view class? Does anyone have any pointers/suggestions/examples that you can give me to help? Thanks!
Advertisement
Quote:
I know i have to return true in the onEraseBckrnd function and overload the onDraw function from what I've read.


That is correct, but you also have to store an off-screen bitmap to draw on and present it later. Flicker Free Drawing In MFC makes adaption easy by wrapping everything in a class.

Quote:
However I can't seam to figure out how to set up this "view" class that everyone seems to talk about but doesn't have a clear example of what it is


(IIRC) In Multiple-/Single-Document-Interface, there exists two classes: a CDocument class, and a CView class. The CDocument contains all the information about the file you are working on, while the CView class presents the CDocument to the user.

As far as you're concerned, the CView class is basically where the drawing gets done.
Ok, so after about 6 hours of trying to figure this out last night I finally ran into a tutorial that states when you create the project in visual studios that it makes the view class and a bunch of other stuff for you. When I started working in MFC i followed a tutorial that started from scratch but never went over this view class. So now i'm at the point where I followed this tutorial you gave me, thanks by the way. Anyways I put all of that stuff in but i'm still hitting a problem. I've created points on the screen that I can move around and they're redrawn everyframe. However when I follow what it says to do here http://www.codeproject.com/KB/GDI/flickerfree.aspx I still get trails of my objects when I pick or move them on my screen. I tried invalidate rect on the mousemove message but that didn't work. I also tried drawing a white rect over the client space. This worked to fix the trails but it made my screen flash again. Here's what i have so far...
BOOL CSplineView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CSplineView::OnDraw(CDC* pDC)
{
CMemDC pdc(pDC);
CSplineDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
}
void CSplineView::OnPaint()
{
CPaintDC dc(this);
CDC memDC;
memDC.CreateCompatibleDC(&dc);

CBrush NewBrush(RGB(0, 255, 0));
dc.SelectObject(&NewBrush);

std::vector<Point>::iterator iter;
for(iter = pointVec.begin(); iter < pointVec.end(); ++iter)
{
CPoint point = iter->GetCords();
dc.Ellipse(point.x - SPHERE_RADIUS, point.y - SPHERE_RADIUS,
point.x + SPHERE_RADIUS, point.y + SPHERE_RADIUS);
}
}
Woot I finally figured out a way to do this! I had to do all my drawing of my objects in the OnDraw function rather than the OnPaint. So now It looks like this...
void CSplineView::OnDraw(CDC* pDC)
{
CMemDC dc(pDC);
CSplineDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

CBrush NewBrush(RGB(0, 255, 0));
dc.SelectObject(&NewBrush);

std::vector<Point>::iterator iter;
for(iter = pointVec.begin(); iter < pointVec.end(); ++iter)
{
CPoint point = iter->GetCords();
dc.Ellipse(point.x - SPHERE_RADIUS, point.y - SPHERE_RADIUS,
point.x + SPHERE_RADIUS, point.y + SPHERE_RADIUS);
}
}

Also in my OnPaint I had to call my OnDraw function like so...

void CSplineView::OnPaint()
{
CPaintDC dc(this);
CDC memDC;
memDC.CreateCompatibleDC(&dc);

CSplineView::OnDraw(&dc);
}

After changing those 2 functions it works beautifully! Thanks for pointing me in the direction for that tutorial fastcall it helped a bunch!

This topic is closed to new replies.

Advertisement