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
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
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
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;
Hello Justin
Try AfxGetInstanceHandle()
Jon