Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Assigning a bit-map to a button

Posted on 2004-10-13
8
Medium Priority
?
229 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 200 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 2

Accepted Solution

by:
samooo_vc earned 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

660 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