Solved

How to place a .bmp over a button

Posted on 1998-02-17
9
413 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting IP address 8 89
mixing C++ & C# in Vis Studio 2013 7 202
How to programmatically differentiate between C and Java 10 204
Header of docx file 17 118
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

828 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