Solved

How to place a .bmp over a button

Posted on 1998-02-17
9
411 Views
Last Modified: 2011-04-14
Addition to my question from 02-12-98 and galkins answer:

He told me to call CButton::SetBitmap to place a .bmp over a button.

(I need only one bitmap for the button, so this should be the right function)
This is what I tried in my C++ code:

BOOL CDiaKamera::Create()
{
CButton* pButton;
BOOL ret;

ret = CDialog::Create(CDiaKamera::IDD);
pButton = (CButton*) GetDlgItem(IDC_Home);
pButton->SetBitmap((HBITMAP) GetDlgItem(IDB_HOME));
return ret;
}
- IDC_Home is my button ID
- IDB_HOME is my bitmap I want to place over this button

...nothing happens...no bitmap visible...just an empty button.

Do I have to "redraw" the dialog to update the buttons ?
If yes, how can I do this ?

What's wrong here ?

-Stavi-
0
Comment
Question by:mitrakis
  • 5
  • 3
9 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 30 total points
ID: 1182404
Answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182405
The problem is GetDlgItem() returns a handle to a control in the dialog.  You use it correctly the first time, but the second time
in the line

pButton->SetBitmap((HBITMAP) GetDlgItem(IDB_HOME));

you use it to get a bitmap?  That's not what it does.

If the bitmap is stored in your resources, you can use LoadBitMap() to get it.  There are other ways of getting bitmaps (LoadImage(), LoadIcon(), for example) but this is probably the one you want.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1182406
It's hard to complete with "Answer Coming" answers...
0
 
LVL 32

Expert Comment

by:jhance
ID: 1182407
jhance,

You have no right to complain, you "stole" one from me just a day  or two ago.  Besides, I recommend you answer the same way.  Don't waste time answering when someone else might be...  
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1182408
Thanks for your efforts !

Unfortunately, I don't know who's right of you... =;-)

I'll grade your answer...tomorrow...I have to test this out in my office.

Thanks again.

Best regards

-Stavi-
0
 
LVL 3

Author Comment

by:mitrakis
ID: 1182409
Our answers are the same.  both of us are suggesting that you use LoadBitmap() to get the bitmap.  jhance was suggesting that you use a static MFC bitmap object.  I don't think that is really necessary. Without knowing more about your code I cannot judge whethor or not that is a good idea, but the basic idea of both of our suggestions is the same.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182410
I wouldn't normally use a static in the OnInitDialog but rather would just a it as a data member to the CDialog class.  I put it here for clarity.  The end result is the same.  BTW, the object you pass to the SetBitmap() member function MUST BE VALID at any time when the button might get repainted.  If you try my example without the static, you will see that it doesn't work.  Even though the HBITMAP was valid when SetBitmap() was called, it isn't valid later when the button actually get drawn.  I'm sure that diving down into the internals of MFC would yield an explanation but my assumption is that SetBitmap doesn't store the bitmap locally but rather assumes you are providing an HBITMAP that is valid for the life of the button.
0
 
LVL 32

Expert Comment

by:jhance
ID: 1182411
Is it possible that what you are talking about is that if you declare a local CBitmap (not static) the bitmap is disposed of when the CBitmap leaves scope and thus the button is not drawn correctly?  Using a static CBitmap will fix this, but has potentials for problems (with a few exceptions, static causes more problems than it solves).  If you drop the CBitMap completely and just LoadBitmap and pass the handle to SetBitmap().  I don't know much MFC, but I beleive you don't have to worry about freeing the bitmap as it will be freed when the button is destroyed, thus a CBitmap is not necessary and actually inappropriate as it will cause the bitmap to be deleted twice.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1182412
It works !
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now