1
votes

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.

1
I think a bit more info is needed. What have you tried so far?finlaybob
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 meanstomas.teicher
I dont really know a lot about MFC stuff, sorry. However a quick google search has got this. 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
Finlaybob is correct. Use CBitmapButton class (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 - msdn.microsoft.com/en-GB/library/zz9355ha(v=vs.90).aspxSChepurin
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 questiontomas.teicher

1 Answers

0
votes

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