Solved

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

Posted on 2007-11-19
15
2,379 Views
Last Modified: 2013-11-25
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
0
Comment
Question by:sternocera
  • 8
  • 7
15 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20314009
please send the code portion where you load your icons
0
 

Author Comment

by:sternocera
ID: 20314518
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
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20314576
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.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:sternocera
ID: 20315401
I've imported an icon(*.ico) into resource editor. I named it something like IDI_MY_ICON.

Thanks
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20315447
>>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.
0
 

Author Comment

by:sternocera
ID: 20315457
I have!

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

that's all there is to it.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20315555
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20315579
To load an icon from your application you will need:
::LoadIcon(::AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_MY_ICON))
0
 

Author Comment

by:sternocera
ID: 20315586
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
0
 

Author Comment

by:sternocera
ID: 20315598
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
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20315707
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)) );

0
 

Author Comment

by:sternocera
ID: 20318453
Jaime,

That doesn't work - the program builds, but my button doesn't display the icon,
Regards,
Sternocera
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20318526
Sorry, ignore my last comment, I had copied and pasted from your. Try with my previous comment:
myButton.SetIcon( ::LoadIcon(::AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_MY_ICON_FROM_RESOURC_EDITOR)))

By the way, you have to be care with this kind of icon usage.
You must load the icon just once, and before destroying the dialog you have to release it, to avoid a memory leak. So you have to store in a variable previously to using it.
0
 

Author Comment

by:sternocera
ID: 20318548
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
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20318559
You should store as a member in the dialog or window class you are using it.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Post a good COM tutorial 1 53
Thin secure Windows 10 5 93
Adding a namespace that doesn't exist for a SSIS Script Transformation 3 39
Resolve Dependency Issues 4 64
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now