Link to home
Start Free TrialLog in
Avatar of sternocera
sternocera

asked on

MFC, C++: Changing a CButton's icon to a custom icon

Hello,

I have an MFC CButton in an MFC dialog that I'd like to give an icon to, in order to have a proper alpha channel for anti-aliasing, which bitmaps don't seem to offer.

I've set the buttons style to icon in dialog editor (there doesn't seem to be a specific facility for selecting an icon, so I guess it's done in code).

I found this reference on MSDN: http://msdn2.microsoft.com/en-us/library/bd24s672(VS.80).aspx .

This cover's CButton's SetIcon() function.

The following example is given:

myButton.SetIcon( ::LoadIcon(NULL, IDI_QUESTION) );

This works for me - My CButton displays the system question mark icon. However, it does not work with my own icons that are imported into the project. I get the following compiler error:

error C2664: 'LoadIconA' : cannot convert parameter 2 from 'int' to 'LPCSTR'

A generic no conversion found error. Theres no point in doing an explicit typecast either.

What have I done wrong? What have I failed to appreciate?

Regards,
Sternocera
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

please send the code portion where you load your icons
Avatar of sternocera
sternocera

ASKER

Jaime,

I don't load my icons -  IDI_QUESTION refers to an icon created with the resource editor.

I don't know how to load icons.
Thanks,
Sternocera
You said:
>>However, it does not work with my own icons that are imported into the project. I get the following compiler error: error C2664: 'LoadIconA' : cannot convert parameter 2 from 'int' to 'LPCSTR'
>>A generic no conversion found error. Theres no point in doing an explicit typecast either.

So I assumed you have tried to load the icon somewhere.
I've imported an icon(*.ico) into resource editor. I named it something like IDI_MY_ICON.

Thanks
>>I've imported an icon(*.ico) into resource editor. I named it something like IDI_MY_ICON.
this operation doesn't cause the error you mentioned: error C2664: 'LoadIconA' : cannot convert parameter 2 from 'int' to 'LPCSTR'

So, again, please post the line which causes that error message.
I have!

myButton.SetIcon( ::LoadIcon(NULL, IDI_QUESTION) );  // myButton is a CButton

that's all there is to it.
OK, there are some confusing stuff:
IDI_QUESTION is a system icon. To load it you need:
::LoadIcon(NULL, MAKEINTRESOURCE(IDI_QUESTION))

since LoadIcon expects a string pointer in second argument, MAKEINTRESOURCE convert the integer to the proper type.
To load an icon from your application you will need:
::LoadIcon(::AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_MY_ICON))
As I said in my original post, I can get it to work with the system icon IDI_QUESTION. This works:
myButton.SetIcon( ::LoadIcon(NULL, IDI_QUESTION) );

However, this does not work:

myButton.SetIcon( ::LoadIcon(NULL, IDI_MY_ICON_FROM_RESOURC_EDITOR) );  // This is the only place this icon appears in the code

It causes the following error message:

error C2664: 'LoadIconA' : cannot convert parameter 2 from 'int' to 'LPCSTR'

I apologise if I  was unclear about that,
Regards,
Sternocera
Jaime,

I don't have access to my windows machine at the moment. I'll let you know how I get on with it tomorrow,

Thanks,
Sternocera
Ok, finally we have the problematic line:
myButton.SetIcon( ::LoadIcon(NULL, IDI_MY_ICON_FROM_RESOURC_EDITOR) );

it should be:
myButton.SetIcon( ::LoadIcon(NULL, MAKEINTRESOURCE(IDI_MY_ICON_FROM_RESOURC_EDITOR)) );

Jaime,

That doesn't work - the program builds, but my button doesn't display the icon,
Regards,
Sternocera
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Jaime,

Great, that works!

You talk about memory leaks. If this has caused something to be created on the heap, how can the memory be released?

How can I store in in a variable on the stack so it will be destroyed when it goes out of scope?

Thanks,
Sternocera
You should store as a member in the dialog or window class you are using it.