Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you change a bitmap displayed in a picture box?

Window Designing

How do you change a bitmap displayed in a picture box?

by  michaelkrauklis  Posted    (Edited  )
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:
//*************************************************************************************************
Code:
//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).
Code:
//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();
[code]
//*************************************************************************************************
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!!
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top