Solved

How to place a .bmp over a button

Posted on 1998-02-17
9
410 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

757 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

22 Experts available now in Live!

Get 1:1 Help Now