Solved

How to place a .bmp over a button

Posted on 1998-02-17
9
414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

732 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