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. Create a new MFC Dialog application and follow the instructions below:
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 View menu-> Class view 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);
}
Monday, October 19, 2009
Display Bitmaps on Buttons using MFC
Subscribe to:
Post Comments (Atom)
Hi,
ReplyDeleteYour post is good and detailed.
I'm a newbie to c++ MFC.
What if I add another, bitmap button. How would i do it.
Thanks, i hope you can share.