Solved

Assigning a bit-map to a button

Posted on 2004-10-13
8
221 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
function returning CString in a regular win32 DLL 8 147
iSeries DB2 Query 2 84
Window placement 17 64
wordappend challenge 8 85
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now