Computing.Net > Forums > Programming > displaying bitmaps on Buttons

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

displaying bitmaps on Buttons

Reply to Message Icon

Name: Steveo
Date: February 14, 2004 at 20:30:04 Pacific
OS: XP home
CPU/Ram: 1.9
Comment:

Exactly what the questtion says. How do i add a bitmap (or icon) to display on the front of a button. MFC and c++. Working code please :) i'm to stupid to figure out what people mean when they say stuff like add a bitmap or a CThis and Cthat.



Sponsored Link
Ads by Google

Response Number 1
Name: egkenny
Date: February 16, 2004 at 01:22:23 Pacific
Reply:

You can use normal CButtons or BitmapButtons. With a CButton you can add a simple bitmap to the button. With a CBitmapButton you can add up to four images for different states for the button

This example is for a normal CButton.

1. Start with image file: cat.bmp
2. Photoshop Elements was used to reduce pixel size from 348W x 284H to 100W x 82H
3. Created MFC Application project with dialog box
4. Insert bitmap into project using menu: Insert -> Resource -> Bitmap -> Import
5. Change bitmap resource ID to IDB_BITMAP_CAT
6. Add CButton to dialog
7. Change resource ID of button to IDC_BUTTON_CAT
8. Edit properties of this IDC_BUTTON_CAT to check the Bitmap Style
9. Open ClassWizard and add Member Variable to dialog class that refers to the CButton created
Control ID: IDC_BUTTON_CAT
Type: CButton
Member: m_button_cat
10. Add variable to dialog class header file to reference bitmap:
file: test_buttonDlg.h
----------------------
class CTest_buttonDlg : public CDialog
{
...
protected:
CBitmap m_Cat;
...
}
11. Add code to dialog class cpp file in OnInitDialog function:
file: test_buttonDlg.cpp
------------------------
BOOL CTest_buttonDlg::OnInitDialog()
{
...
// TODO: Add extra initialization here
// load bitmap for button
m_Cat.LoadBitmap(IDB_BITMAP_CAT);
HBITMAP hBitmap= (HBITMAP) m_Cat.GetSafeHandle();
m_button_cat.SetBitmap(hBitmap);
// resize buton for bitmap
BITMAP bm;
m_Cat.GetBitmap(&bm);
int width = bm.bmWidth;
int height = bm.bmHeight;
CRect r;
m_button_cat.GetWindowRect(&r); /* r comes out in screen coordinates */
ScreenToClient(&r); // MoveWindow needs coordinates in parent window
r.right = r.left + width;
r.bottom = r.top + height;
m_button_cat.MoveWindow(&r);
}

Reference:
Owner-Draw Buttons and Menus
http://www.cs.sjsu.edu/faculty/beeson/courses/cs130/LectureNotes/16-OwnerDraw/OwnerDraw.html


0

Response Number 2
Name: Steveo
Date: February 16, 2004 at 13:54:14 Pacific
Reply:

thanks



0

Response Number 3
Name: Lucyes
Date: February 24, 2004 at 00:15:03 Pacific
Reply:

Do you know how many CBitmapbuttons can I use?
The functions LoadBitmap and CreateCompatibleDC fail when I have a large number of buttons, but i donīt know why.
I need help,
thanks


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: displaying bitmaps on Buttons

display bitmap on screen www.computing.net/answers/programming/display-bitmap-on-screen/2680.html

JavaScript: Menu on button down www.computing.net/answers/programming/javascript-menu-on-button-down/5930.html

Simple (i guess) MFC (CButton) prob www.computing.net/answers/programming/simple-i-guess-mfc-cbutton-prob/5743.html