C++ How to use CWnd * object to load images?

1k views Asked by At

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

There are 1 answers

2
Santosh Dhanawade On

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