Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

HBITMAP to CBitmap?

268 views
Skip to first unread message

Justin Houk

unread,
Jan 2, 2001, 11:20:37 PM1/2/01
to
Happy New Year everyone!

Thanks to the folks who replied to my previous post. It was very
helpful! Sadly, I'm stuck again and wishing I was working on an
open-source platform like Linux so I could dig into the source code of
the OS and figure out what I'm doing wrong. So sad.

This has got to be simpler than I'm making it, but I can't find a good
way to create a CBitmap member variable using an HBITMAP object. Here's
the code I'm using (I know it's icky):

if ((!(tempGDIBitmap = (HBITMAP)LoadImage(NULL, fileName, IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE))) ||
((tempMFCBitmapPtr = CBitmap::FromHandle(tempGDIBitmap))==NULL))
{
TRACE0("Failed to load bitmap for normal image.\n");
return FALSE; // need this one image
}
tempMFCBitmapPtr->GetBitmap(&tempBitmapStruct);
m_bitmap.CreateBitmapIndirect(&tempBitmapStruct);


Running this through the debugger, I see that tempBitmapStruct has the
proper bmWidth and bmHeight, but bmBits is NULL. Oh, and when I try to
display m_bitmap, it's garbage. I know the problem is in the loading
not the displaying because if I replace this with loading a bitmap from
a resource, everything works great.

Any ideas?

Thanks in advance!!!
--Justin

Justin Houk

unread,
Jan 3, 2001, 6:14:22 PM1/3/01
to
Well, I've spent a few more hours trying to get this to work, but to no
avail. Please tell me it's not impossible to load a CBitmap from a
file. This is crazy! :(

I realized I could make the code snipped clearer if I took out some
error checking. Here it is:

tempGDIBitmap = (HBITMAP)LoadImage(NULL, fileName, IMAGE_BITMAP,

0, 0, LR_LOADFROMFILE);
tempMFCBitmapPtr = CBitmap::FromHandle(tempGDIBitmap);
tempMFCBitmapPtr->GetBitmap(&tempBitmapStruct);
m_bitmap.CreateBitmapIndirect(&tempBitmapStruct);

I'm creating a child class of CBitmapButton, so m_bitmap has to be a
CBitmap; it can't be a CBitmap*.

Is it possible that I have to provide an HINSTANCE as the first
parameter of LoadImage(), even if I'm loading from a file? If so, does
anyone know how to get the HINSTANCE of an MFC app? All the example
code I've been able to find gets an HINSTANCE from a DLL.

If anyone could spend a minute to explain what I must be missing, it
would really help me out. I'm stuck.

Thanks!
--Justin

Justin Houk

unread,
Jan 4, 2001, 11:10:07 PM1/4/01
to
Many thanks to a friend who sent me a friendly email to let me know that
CBitmaps actually DON'T deal with DIBitmaps (that is, bitmaps from
files) very well.

Armed with that information, I ditched the whole "inheriting from
CBitmapButton" paradigm and instead created a parallel class, which used
HBITMAPs instead of CBitmaps. I had used HBITMAPS before in a non-MFC
app, so I knew I could get those to draw to the screen properly.

So all's well? Well, almost. I'm still running into a problem where my
buttons aren't getting refreshed as often as they should. For example,
if I take the dialog I've made, and drag it practially entirely off the
screen, then drag it back on again, all the Windows-drawn dialogs are
redrawn, but mine aren't. It's like the DrawItem() function of my new
class never got called. Has anyone dealt with this problem before
and/or have any hints how I can get DrawItem() called more frequently?

Thankee-sai!
--Justin

Pete Arends

unread,
Jan 5, 2001, 1:22:49 AM1/5/01
to

Justin Houk <jhouk@SPAM_SUXwso.williams.edu> wrote in message
news:3A53B2F0.F602CE62@SPAM_SUXwso.williams.edu...

HBITMAP hbmp = (HBITMAP)LoadImage(NULL, filename, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE);
CBitmap cbmp;
cbmp.Attach(hbmp);

use AfxGetInstanceHandle() to get HINSTANCE of your app;

Jon White

unread,
Jan 8, 2001, 6:03:33 AM1/8/01
to

"Justin Houk" <jhouk@SPAM_SUXwso.williams.edu> wrote in message
news:3A53B2F0.F602CE62@SPAM_SUXwso.williams.edu...

Hello Justin
Try AfxGetInstanceHandle()
Jon


0 new messages