INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

HANDLE


PASSWORD
Remember Me
Forgot Password?

Come Join Us!

  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • Turn Off Ad Banners
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

E-mail*
Handle

Password
Verify P'word
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Member Feedback

"...Keep up the good work - excellent site - i'd been looking for something like this for ages !..."

Geography

Where in the world do Tek-Tips members come from?

Microsoft: Visual C++ FAQ

Window Designing

How do you change a bitmap displayed in a picture box?
Posted: 18 Jan 02 (Edited 9 Apr 03)

Complete Problem:
I had created three bitmaps as bitmap resources in my VC++ project.  Then, using VC++'s picture tool I created a picture box and filled it with the appropriate bitmap by making the picture type bitmap and then selecting the correct bitmap resource.  The problem was that I didn't want this bitmap to be static.  I wanted to be able to change the bitmap being displayed as the program executed.  It was a bit tricky, but here's what I came up with.  The code is pretty straightforward:
//*************************************************************************************************

//first step is to get the actual picture object
//it is stored as a CStatic object, or at least an object
//that inherits from CStatic
//nIDResourceItem is the UINT representing the picture
//or, more simply, just the ID of your picture
//this ID can also be in LPCSTR format
CStatic *bitmap=(CStatic*)(AfxGetMainWnd()->GetDlgItem(nIDResourceItem));
//
note, I use AfxGetMainWnd() because the bitmap I am changing is on my main window.  If this is not your case you should instead have a pointer to the window in which your bitmap resides (e.g. this).

//next you simply call the member function CStatic::SetBitmap
//on your picture(CStatic) object.  The hardest part
//was figureing out how to get  the HBITMAP
//handle pointing to the proper bitmap resource.
//nIDResourceBitmap is the UINT of the bitmap
//resource you wish to load.  LoadBitmap returns
//the HBITMAP you need to set the bitmap.  I don't think
//the MAKEINTRESOURCE is necessary, but the documentation
//had it so I used it.  I believe you can just use
//the UINT of the bitmap resource or a string representing
//the name of the UINT in your call to LoadBitmap
//AfxGetResourceHandle obviously returns a handle
//to the location the default resources are loaded
bitmap->SetBitmap(LoadBitmap(AfxGetResourceHandle(), MAKEINTRESOURCE(nIDResourceBitmap)));

//then you simply redraw the CStatic object so you
//can see the changes
bitmap->RedrawWindow();

//*************************************************************************************************
Like I said, not terribly complicated.  It was just a pain to put the peices together.  I hope I possibly helped someone else avoid the headaches I had trying to get this seemingly simple problem to work.  Happy Programming!!

Back to Microsoft: Visual C++ FAQ Index
Back to Microsoft: Visual C++ Forum
My FAQ Archive
Email This FAQ To A Friend

My Archive