1

I am totally newbie in C++ and I need to edit C++ softare. I need to replace GUI buttons graphics with image icons. I am not sure, but maybe Button is created in this part of code:

BOOL CButtonDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetDlgItemText(IDC_BUTTON_LOAD,_T("Load"));

return TRUE;
}

When I am looking into code I see buttons are enabled with such a code:

m_dButtondlg.GetDlgItem(IDC_BUTTON_LOAD)->EnableWindow(true);

Where IDC_BUTTON_LOAD is integer constatnt. I cannot find any other usage of the constant in code, so I am not sure how the button was created. I just know that

 m_dButtondlg.GetDlgItem(IDC_BUTTON_LOAD) 

returns pointer to CWnd

How can I attach images to button, using CWnd object?

EDIT: I have found out, that button identified with IDC_BUTTON_LOAD is instance of derived class of CDIalog, not CButton.

tomas.teicher
  • 913
  • 3
  • 13
  • 26
  • I think a bit more info is needed. What have you tried so far? – finlaybob Aug 14 '13 at 08:11
  • I have tried to load image with CImage object and tried to use StretchBlt method. But it requires hdc object. I am totally newbie in C++ so I am just learning what objects like cwnd or hdc means – tomas.teicher Aug 14 '13 at 08:21
  • I dont really know a lot about MFC stuff, sorry. However a quick google search has got [this](http://msdn.microsoft.com/en-GB/library/yf1wax6c(v=vs.90).aspx). I think you should alter your tags to include MFC, that way people more experienced with this type of thing may be able to help :) – finlaybob Aug 14 '13 at 08:23
  • Finlaybob is correct. Use *CBitmapButton* class (http://msdn.microsoft.com/en-GB/library/a3y45xs0(v=vs.90).aspx) and on the corresponding page always look for MSDN samples on the matter, like this one - http://msdn.microsoft.com/en-GB/library/zz9355ha(v=vs.90).aspx – SChepurin Aug 14 '13 at 08:36
  • The problem is, I am just editting finished porject, and the project is using CDialog to create buttons. I cannot just replace class CDialog with CBitmapButton. Sorry for not mentioneing that facts, I added EDIT to question – tomas.teicher Aug 14 '13 at 08:43
  • If I understand it correctly, I cannot change button to image if button is instance of CButton or CDialog? – tomas.teicher Aug 14 '13 at 09:18

1 Answers1

0

Best way is that you convert your CWnd pointer to CButton like shown in below,

CButton * DlgButton = (CButton*)GetDlgItem(IDC_BUTTON_LOAD);

And you can easily load image on CButton object.

EDIT

Code to load bitmap on CButton,

   CButton * DlgButton = (CButton*)GetDlgItem(IDC_BUTTON_LOAD);

   DlgButton->ModifyStyle( 0, BS_ICON );

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

    DlgButton->SetIcon( hIcn );
Santosh Dhanawade
  • 1,756
  • 14
  • 29
  • thanks for the hint, but as I read on documentation page http://msdn.microsoft.com/sk-sk/library/yf1wax6c(v=vs.90).aspx, I need to created CBitmapButton. cannot load image to Cbutton am I right? I cannot convert running object to another class (to CBitmapButton) once it is created. I am a newbie in C++ – tomas.teicher Aug 14 '13 at 11:27
  • New code is correct. It bypasses MFC and tells Windows directly to use the specified icon. – MSalters Aug 14 '13 at 11:54