We help IT Professionals succeed at work.

Displaying a bitmap in a picture at runtime

I have a "Picture" control in my dialog under Visual C++ 6.0, that I would like to change to display another bitmap dynamically at runtime.

How is this done?

Comment
Watch Question

Author

Commented:
The control is called IDS_ABOUTBOX, and the bitmap
is called IDB_CURRENTLOGO.
CERTIFIED EXPERT

Commented:
Hi Claus,

simply attach a variable 'Control' of type CStatic in ClassWizard to the control.
Then, in your code load the bitmap (i.e. with CBitmap::LoadBitmap) from the
resources and set it to the control via CStatic::SetBitmap...

hope that helps,

ZOPPO
Put the pictre control on Dialog
Select properties, and chane its id IDC_STATIC to IDC_STATIC_BITMAP
Using class wizard create its object (Press Ctrl + W , and add as object of CStatic control ie m_oBitmap)

Now like below u can change the image dynamically

HBITMAP hBMP = (HBITMAP)LoadImage(AfxGetInstanceHandle(),
                                    "C:\\Documents and Settings\\user\\My Documents\\My Pictures\\Picture.BMP",
                                    IMAGE_BITMAP,
                                    0,
                                    0,
                                    LR_LOADFROMFILE);

     m_oBitmap.SetBitmap(hBMP);

GOOD LUCK

Author

Commented:
To Zoppo:

SetBitmap() does not accept an object of type CBitmap*, so what you're suggesting is not immediately possible.


To roshmon:

I am not interested in loading the bitmap from a file, since our application will be deployed in many different production environments, where the location of the file is uncertain.  Also, since I am working in Visual C++ 6.0, where the bitmap is accessible in memory runtime, it would be better to use that.


So ur program laoding bitmap from resource..?

Then I can give u another solution...

Waiting 4 ur reply

Roshmon

Author

Commented:
Yes it is loading it from a resource.  Its identifier is IDB_CURRENTLOGO.

-- Claus
Then u can use

HBITMAP hBMP = LoadBitmap(AfxGetResourceHandle(), MAKEINTRESOURCE(IDB_CURRENTLOGO));

GOOD LUCK
CERTIFIED EXPERT
Commented:
>SetBitmap() does not accept an object of type CBitmap*, so what
> you're suggesting is not immediately possible.
You can use CBitmap::operator HBITMAP to retrieve the handle of the bitmap, i.e.

CBitmap bitmap;
bitmap.LoadBitmap(...);
CStatic* pStatic = (CStatic*)GetDlgItem( IDC_THE_CONTROL );
pStatic->SetBitmap( bitmap );
// or, if you have a pointer
CBitmap* pBitmap = &bitmap;
pStatic->SetBitmap( *pBitmap );

in both cases the CBitmap::operator HBITMAP is impliciteley called.

hope that helps,

ZOPPO

Author

Commented:
Even though you both solved the problem, Zoppo's solution seems to be the correct way to do it in MFC.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.