Problems painting into memory device context with GDI

Here is a snippet of some GDI code, which uses a memory device context and copies it onto the view device context.  It appears to work fine but it's wrong since the old bitmap pointer is not saved:

CDC xdc;
CBitmap xbitmap;

xdc.CreateCompatibleDC(pDC);
xbitmap.CreateCompatibleBitmap(pDC, tex, wry);
xdc.SelectObject(xbitmap);
//...Draw onto xdc
//...BitBlt is used

Here is the code including the fixed resource leak.  It creates an error when SelectObject does not return a valid CBitmap pointer.

xdc.CreateCompatibleDC(pDC);
xbitmap.CreateCompatibleBitmap(pDC, tex, wry);
CBitmap* oldbitmap = xdc.SelectObject(xbitmap);
//...Draw onto xdc
//...BitBlt is used
//...
xdc.SelectObject(oldbitmap);

Of course, SelectObject takes a pointer to the object, so here is the fixed line.  Now it doesn't work at all; after the BitBlt operaton, the view is still empty.

CBitmap* oldbitmap = xdc.SelectObject(&xbitmap);

What am I doing wrong with this GDI code?
LVL 5
Alkali_GuyAsked:
Who is Participating?
 
Alkali_GuyConnect With a Mentor Author Commented:
I found the problem:
I accidentally restored the pOldBitmap (oldbitmap) into xdc before BitBlting it onto the screen.  Since the above code is correct, there was no indication here of incorrect order in my own code.
0
 
nabehsCommented:
i dont see anything wrong with the code, maybe the problem in BitBlt. can u post it?
0
 
Alkali_GuyAuthor Commented:
pDC->BitBlt( 0, 0, mex, cry, &xdc, 0, 0, SRCCOPY );
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Alkali_GuyAuthor Commented:
I have only seen this code work when I pass the xbitmap object to SelectObject, instead of passing its pointer the way I am supposed to.
0
 
Alkali_GuyAuthor Commented:
Other information:
Similar code later in the same function works fine, so what is currently happening is:

memdc   >-BitBlt->  xdc  >-BitBlt->  *pDC

I have also tryed CreateCompatibleBitmap passing &xdc instead of pDC.
0
 
nabehsCommented:
R U declaring xbitmap as CBitmap* xbitmap? It should be CBitmap xbitmap; I know that it is posted here correctly but maybe in your original code it is not.

0
 
Alkali_GuyAuthor Commented:
xbitmap is declared correctly in the code.
0
 
Alkali_GuyAuthor Commented:
To reiterate the code where the fault is:

xdc.CreateCompatibleDC(pDC);
xbitmap.CreateCompatibleBitmap(pDC, x, y);
CBitmap* pOldBitmap = xdc.SelectObject( &xbitmap );

//Draw stuff

pDC->BitBlt( 0, 0, x, y, &xdc, 0, 0, SRCCOPY );
xdc.SelectObject(oldbitmap);

Note that x and y are not necessarily the width and height of *pDC.  Do I have to convert coordinates before use?
0
 
Alkali_GuyAuthor Commented:
[The last line in above code should read:
xdc.SelectObject(pOldBitmap);]
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.