Solved

Assigning a bit-map to a button

Posted on 2004-10-13
8
223 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 50
Strange date stored 22 120
Generic progress indicator 6 116
only14 challenge 19 75
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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