how to add bitmap image to buttons in MFC?

倖福魔咒の 提交于 2019-11-27 06:20:34

问题


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..


回答1:


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




回答2:


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);



回答3:


You could subclass existing button using CBitmapButton::SubclassWindow, then use LoadBitmaps.




回答4:


you don't know how much this helped out. Thanks for posting. Also have to change a few other things to bitmap as well ...

CButton* pBtn= (CButton*)GetDlgItem(ID_MYDIALOG);
pBtn->ModifyStyle( 0, BS_BITMAP );

HBITMAP hIcn= (HBITMAP)LoadImage(
  AfxGetApp()->m_hInstance,
  MAKEINTRESOURCE(IDB_MYPIC),
  IMAGE_BITMAP,
  0,0, // use actual size
  LR_DEFAULTCOLOR
  );

pBtn->SetBitmap( hIcn );



回答5:


Use the button classes from the Feature Pack. They have support for showing both text and images on buttons, your regular button can't do that. Look at the 'samples' directory in your VS installation directory.




回答6:


I want to add some ideas to @Amruta Ghodke 's answer:

You can resize your button using the GetWindowRect and SetWindowPos functions. See an example below:

CRect rc;

pButton->GetWindowRect(rc);
pButton->SetWindowPos(NULL, rc.left, rc.top, myWidth, myHeight, SWP_NOSENDCHANGING | SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOMOVE);

If you want to display transparent images, use the software Pixelformer to convert your PNGs to Alpha-enabled BMPs. You will have to:

  1. Go to Image->Properties and set RGB color with alpha channel
  2. Export the file using format A8:R8:G8:B8 and disabled Premultiplied alpha and Top-down row order


来源:https://stackoverflow.com/questions/2047470/how-to-add-bitmap-image-to-buttons-in-mfc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!