Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add bitmap image to buttons in MFC?

Tags:

mfc

cbitmap

I am Trying to add an image to an existing button..I have done that to an extent, the problem is I can add an ownerdrawn Image but am not able to add the extact image that I want.. for the example see the below code

CButton* pBtn= (CButton*)GetDlgItem(ID_WIZBACK);

   pBtn->ModifyStyle( 0, BS_ICON );

   HICON hIcn= (HICON)LoadImage(
        AfxGetApp()->m_hInstance,
  MAKEINTRESOURCE(IDI_ICON3),
        IMAGE_ICON,
        0,0, // use actual size
        LR_DEFAULTCOLOR
    );

    pBtn->SetIcon( hIcn );

with the above code am converting the bitmap to an icon to add to my button...how can I add the exact Bitmap image directly to an existing button.Please help me frnds..

like image 212
kiddo Avatar asked Jan 12 '10 07:01

kiddo


People also ask

How do I add a bitmap to a button in MFC?

Step 1 − Add a Bitmap from Add Resource dialog box. Step 2 − Select Bitmap and click New. Step 3 − Design your bitmap and change its ID to IDB_BITMAP_START as shown above. Step 4 − Add a button to your dialog box and also add a control Variable m_buttonStart for that button.

How do I display image in picture control in MFC?

Step 1 − Right-click on the dialog in the designer window and select Insert ActiveX Control. Step 2 − Select the Microsoft Picture Click Control and click OK. Step 3 − Resize the Picture control and in the Properties window, click the Picture field. Step 4 − Browse the folder that contains Pictures.

What is a bitmap button?

A bitmap button is a push button, check box, or radio button that looks like a push button, but with a picture on its face instead of the usual text. Bitmap buttons are frequently grouped together to form a toolbar.


2 Answers

Steps for assigning bitmap to button in mfc :

  1. Create object of bitmap
  2. Load bitmap by using LoadBitmap()
  3. Get Handle of button using id and GetDlgItem() method
  4. Modify style so that we can assign bitmap to it
  5. use SetBitmap() on button's handle to assign bitmap

Code :

CBitmap bmp;

bmp.LoadBitmap( IDB_BITMAP4 );

CButton* pButton = (CButton* )GetDlgItem(IDC_BUTTON1);

pButton->ModifyStyle(0,BS_BITMAP);

pButton->SetBitmap(bmp);
like image 152
Amruta Ghodke Avatar answered Oct 19 '22 19:10

Amruta Ghodke


I actually fixed the problem..what I did is I replaced the HICON with HBITMAP and its working perfect...basically both would work fine but in my case when I loaded the icon into the button the background of the icon was not changing...I tried Bitmap then it work great. Now am working on positioning the Image and to add text...think I could go through

like image 40
kiddo Avatar answered Oct 19 '22 18:10

kiddo