Click here to Skip to main content
14,934,046 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I am using VC6 and the compiler is Win32 (for my university project), I have got several buttons on my dialog and i want to add different icons correspond to different buttons.

What I have tried:

I tried this code, as my button called
IDC_BTN_AUTOSET
and my icon is called
IDR_MAINFRAME
. This code gives no error, but it does not show me any thing even if i change the button propertiy to icon.

			HICON h_Ico = (HICON) LoadImage( AfxGetResourceHandle(),"IDR_MAINFRAME", IMAGE_ICON, 32, 32,LR_LOADFROMFILE );
    CButton *Button=(CButton*)GetDlgItem(IDC_BTN_AUTOSET);
   Button->SetIcon( h_Ico );
SetIcon(h_Ico, TRUE);	
SetIcon(h_Ico, FALSE);
Posted
Updated 22-Aug-18 14:31pm
v2
Comments
Manish K. Agarwal 21-Aug-18 2:48am
   
please refer https://www.codeproject.com/Tips/406870/Change-ICON-of-MFC-Application-and-Dialog
Manish K. Agarwal 21-Aug-18 2:51am
   
also
https://support.microsoft.com/en-ae/help/179582/how-to-set-the-title-bar-icon-in-a-dialog-box
Baderd94 21-Aug-18 8:59am
   
Sorry I improved the question, It was not clear as I said add an icon to a dialog instead of a button in aq dialog.

The LoadImage() function gives an error but you did not check if an error occured (if the returned handle is NULL). Read the documentation: LoadImageA function | Microsoft Docs[^].

If you want to load an image or icon from the resources embedded in your executable, you have to pass the ID using MAKEINTRESOURCE and don't use LR_LOADFROMFILE:
HICON h_Ico = (HICON) LoadImage( AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 32, 32, 0 );
if (h_Ico == NULL)
{
    // Handle error here
    // The error code can be retrieved by calling GetLastError()
}
   
Comments
Baderd94 21-Aug-18 8:59am
   
Sorry I improved the question, It was not clear as I said add an icon to a dialog instead of a button in aq dialog.
Jochen Arndt 21-Aug-18 9:07am
   
You have to ensure first that the icon is loaded successfully as shown in my example. Once that is satified you can use it where you want.
This code works fine for me

m_btn2.SetBitmap(LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP13)));

as
m_btn2
is my button's name and
IDB_BITMAP13
is my BITMAP's name
make sure you add
Cbutton m_btn2;
in your header file.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900