Solved

Assigning a bit-map to a button

Posted on 2004-10-13
8
225 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
  • 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
Independent Software Vendors: 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

Independent Software Vendors: 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
Strange date stored 22 167
Change owner from userA to server Local Administrators 7 71
sum67 challenge 35 114
matchUp  challenge 9 123
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 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