Solved

Assigning a bit-map to a button

Posted on 2004-10-13
8
226 Views
Last Modified: 2013-11-20
I'm programming with Visual C++ 6.0 MFC and have programmed in an ON/OFF button that changes it's label between "ON" and OFF"  when toggled. No problem.  But I'd really like to put an image on the button that changes, like a bright red dot when ON and a black dot when OFF. This MFC stuff is all new to me and though I've got two books on it, I haven't found a way yet to put a bit map - even an icon would be OK - on a dialog button.  Any help would be appreciated.

Thanks,
Steve
0
Comment
Question by:steva
[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
  • 4
  • 3
8 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 50 total points
ID: 12299313
Create a button on your dialog, set the style to bitmap.

Use the class wizard to add a CONTROL variable linked to the ID of the button and of type CButton

In the OnInitDialog add the following.

// Set the bitmap of the button to be the system check mark bitmap.
myButton.SetBitmap( ::LoadBitmap(NULL, MAKEINTRESOURCE(OBM_CHECK)) );


(You can use LoadBitmap with bitmpas you supply in the resource if you want).

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12299361
There is an older class called CBitmapButton.  With this you can supply different images for the different states (up, down, disabled and focused).  The help files tell you how to use that.
0
 

Author Comment

by:steva
ID: 12303101
Andy,

First, the CBitmapButton doesn't seem to provide what I want since the image changes automatically when you select, deselect, etc. the button.  I want the button to go to red and stay red when I click and release it once, and then go to black when I click and release it again.  

Regarding the CButton example you gave, I'm having trouble getting this to work.  The CButton::SetBitmap routine takes a handle to a bitmap (HBITMAP) but all I have are the ID's I assigned to the bitmaps I added to the resource script with the image editor. How do I create a handle from this?  I can see that the ::LoadBitmap(NULL, MAKEINTRESOURCE(OBM_CHECK) code will do this for a system image (though my system complains that it cant find OBM_CHECK).  If I try to use LoadBitmap to get a handle for my bitmap it wants an HINSTANCE for the application (NULL in the example you gave to get a system image) and I don't know how to get this either.

Just a little more help and I think I'll be there.

Steve

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 2

Accepted Solution

by:
samooo_vc earned 200 total points
ID: 12303113
a very good tutorial for this at .....
http://www.functionx.com/vcnet/mfccontrols/bmpbtn.htm

0
 

Author Comment

by:steva
ID: 12304345
Samoo,

Thanks for the link to the tutorial.  I was able to dig what I needed out of it.  You get your own instance handle with a call to AfxGetInstanceHandle(), which then lets me assign any bitpmap I want to the button with:

m_button.SetBitmap(::LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_RED_DOT)));

Steve
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12305475
'First, the CBitmapButton doesn't seem to provide what I want since the image changes automatically when you select, deselect, etc. the button.  I want the button to go to red and stay red when I click and release it once, and then go to black when I click and release it again.  '

Use the CButton and have a boolean variable to switch images.

eg.
CMyClass::OnBtnPress()
{
static bool bState = false;
if(bState)
  SetRedButton;
else
  SetBlackButton;
bState = !bState;
}
0
 

Author Comment

by:steva
ID: 12309955
Andy,

Yes, the boolean looks like a good idea. Actually, it's scope needs to be the entire dlg file since OnInitDialog() needs to set the state initially according to how it finds it, and then OnBtnPress will toggle it as you've indicated. But this was no problem.  I just put defined it at the top of the file, as I would in C.

Steve
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 12316420
member var (private or protected) of the class is better (more OOP)
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

Title # Comments Views Activity
Expand macro to ask for filename column 8 40
Whole sheet autoscrub still needed 19 54
haveThree challenge 22 127
x-bar in Google Sheets 2 129
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

734 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