CMemDC (double buffering) not working now I'm using .NET

Hi there,

I have recently moved my MFC project into Visual Studio .NET.  I'm still using MFC and the bulk of the project is unmanaged code, but I'm also now using ADO .NET, so I'm #using <mscorlib.dll> and have "Use Managed Extensions" set to "Yes" in the project properties.

I have been using CMemDC which encapsulates double-buffering to cut down on flicker; you can see it here:

It worked fine for me in the past.  However, it is not working any more.  It doesn't crash or anything, it just doesn't put anything onto the screen when it gets to the BitBlt call.  I'm using CMemDC in a dozen different places in my project and it's the same everywhere.  I haven't made any changes to the GUI and certainly not to the CMemDC class since I moved over to VS .NET so I'm at a loss to explain why it's not working any more.  Does anyone know?

Thank you.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
I assume you have rebuilt all the code with .net
Have you put a breakpoint in the code and single stepped - check things like position/dimension of bitmap.
Can you post the problematic part? I will need to have a closer look. I dont think it is a issue.
wilsonianAuthor Commented:
Thanks for your comments guys

Andy- Yeah I've single stepped through and looked at that stuff- the values are exactly the same as in the old version.  Even if I paint the whole MemDC some other colour, nothing shows up.

nabehs- What do you want? The CMemDC class itself (which I changed *slightly* from what's in the link I posted above, back when I first started using it) and one of the places where I'm using it?
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

try adding a line in the constructor of CMemDC in memdc.h

            if (m_bMemDC) {
                  // Create a Memory DC

                  m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
                  m_oldBitmap = SelectObject(&m_bitmap);



                                                // Add the following line
                                                BitBlt(0, 0, m_rect.Width(), m_rect.Height(), pDC, m_rect.left,, SRCCOPY);

            } else {

ALSO IN THE DESTRUCTOR OF CMemDC of memdc.h modify the BitBlt line to the following

m_pDC->BitBlt(m_rect.left,, m_rect.Width(), m_rect.Height, this, 0, 0, SRCCOPY);


IF THIS DOES NOT WORK THEN m_bMemDC in the destructor was not true.

hope this works.
wilsonianAuthor Commented:
Sorry, I won't have the code in front of me until Tuesday but I can tell you that the flow of executioin was definitely reaching the BitBlt in the destructor, which I guess is why you wanted me to check that m_bMemDC was true?
wilsonianAuthor Commented:
Do you want me to post that code tomorrow?
sure. that will help
wilsonianAuthor Commented:
Ok, here's the version of CMemDC I'm using.  All that I've changed is I'm using CRects representing both the client window area and the area to be drawn.  I can't remember the specific reason why I did this, cos it was about 18 months ago, but it has been working fine for all that time.

#ifndef _MEMDC_H_
#define _MEMDC_H_

#include "Drawing.h"

// CMemDC - memory DC
// Author: Keith Rule
// Email:
// Copyright 1996-1997, Keith Rule
// You may freely use or modify this code provided this
// Copyright is included in all derived versions.
// This class implements a memory Device Context

class CMemDC : public CDC {
      CBitmap*      m_bitmap;
      CBitmap*      m_oldBitmap;
      CDC*            m_pDC;
      CRect            m_rcBounds;
      CRect            m_inner;
      CMemDC(CDC* pDC, const CRect& rcBounds, CRect& inner) : CDC()
            m_bitmap = new CBitmap;
            m_bitmap->CreateCompatibleBitmap(pDC, rcBounds.Width(), rcBounds.Height());
            m_oldBitmap = SelectObject(m_bitmap);
            m_pDC = pDC;
            m_rcBounds = rcBounds;
            m_inner = inner;
            CBrush* tempBrush = new CBrush(Drawing::GREY);
            CBrush* oldBrush = SelectObject(tempBrush);
            DWORD      color = ::GetSysColor( COLOR_BTNFACE );
            FillRect(m_rcBounds, tempBrush);
            delete tempBrush;

            m_pDC->BitBlt(m_inner.left,, m_inner.Width(), m_inner.Height(),
                              this, m_inner.left,, SRCCOPY);
            if (m_bitmap != NULL) delete m_bitmap;
      CMemDC* operator->() {
            return this;

wilsonianAuthor Commented:
Oh and I tried it with the completely original copy of the file and it made no difference, ie there continued to be no drawing.

Here's an example of where I'm using CMemDC in the code:

void ConfigPage::writeSyms(CDC* dc) {
    CRect inner(660*getRatioX(), 95*getRatioY(), 1115*getRatioX(), 125*getRatioY());
    CRect      rcBounds;
    CMemDC memDC(dc, rcBounds, inner);

    for (int i = 0; i < highlightingPhonemes.GetSize(); i++) {

Er... I don't think it matters what it actually does, but the point is that the code as written there does nothing, but if I can't the line inside the for loop to
it draws it as I expect (but with flicker).  

The area enclosed by inner is definitely the area I am drawing to.

Also, I have OnEraseBkgnd returning false; this is definitely working.  And the function writeSyms is also called in OnPaint() with the CPaintDC as its argument.

Is there other stuff you want to know?
I pasted the code in a vc++ project and tested it and it is working fine. The only change I had to make is the following line in memdc.h:

         CBrush* tempBrush = new CBrush(Drawing::GREY);
         CBrush* tempBrush = new CBrush(RGB(192, 192, 192));

In OnPaint( ) I wrote the following:

CPaintDC dc(this); // device context for painting
CRect rect;
CMemDC pDC(&dc, rect, rect);
srand( 1000 );
for(int i = 0; i < 10000; i++)
    pDC->MoveTo(rand() % 1000, rand() % 1000);
    pDC->LineTo(rand() % 1000, rand() % 1000);

and I forced the window on refreshing. it is working fine.

wilsonianAuthor Commented:
I have quite co-incidentally solved the problem- I noticed that display of images (ones where I wasn't using double buffering) was slower than it used to, and I tracked the problem down to the function that converts my jpeg files into HBITMAPs.  The function is this one here:

So I replaced it with the use of the class here:

which seeemed to cut down the time it took. And lo and behold, the images in my CMemDCs started appearing on the screen again!

I'm still willling to give out the points if someone can tell me *why* this is the case, because I have no idea.
wilsonianAuthor Commented:
Ok, so on further consideration it seems I have not solved the problem after all.  I am in fact more confused now than when I started.  

When I use the CPicture class, things appear just fine inside the CMemDC, whether they are jpegs or bitmaps.  _Outside_ the CMemDC (ie when I just BitBlt things straight onto the screen) they always appear opaquely, ie they have a white box around them.

When I just use bitmaps and load them in with (HBITMAP)::LoadImage() etc, they appear fine outside the the CMemDC.  BUT they will not appear inside the CMemDC(nothing shows up). Moreover, if I blt a mixture of jpegs (using CPicture) and bitmaps (not using CPicture) onto a CMemDC, nothing appears, not even the jpegs which would appear fine on their own.

I really don't know what's going on.  Does anyone know?
wilsonianAuthor Commented:
Right, I've had the problem pointed out to me. The problem was that in the function I was using to draw the bitmaps, I was selecting the bitmap into a CDC like so:

CDC dcMem;
CBitmap* oldBmp = dcMem.SelectObject(&myBitmap);

and then putting the old one back in:


Whoops.  Those ain't the same CDC.

Sorry about the complicated question when the problem was so simple.
wilsonianAuthor Commented:
I'll ask to close the question if that's ok with you nabehs
sure, no problem.
Closed, 500 points refunded.
Community Support Moderator

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.