Click here to Skip to main content
15,904,346 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Dear all,

While there are what appear to be several excellent improvements to the MFC button here at code project, I would like to check that I am not missing something obvious. I cant get image/icon MFC buttons to do what I want straight out of the box with Visual Studio's dialog editor.

What I want is to have a button, with a border is fine, but cooler without, that has an image which includes transparency. That is, where the current Windows theme's dialog background colour is assumed as the image's background colour.

The closest I have got is to load the source PNG file into GIMP2.6 and save it out as a Windows ICO format. It looks fine in GIMP. It looks fine in VS2010's editor when imported; i.e. its the same size as saved from GIMP.


But when I set a MFC Button on a dialog to have an "image type" of "icon" in the properties pane and select the ID of my lovely large PNG-now-ICO its suddenly very small.

I thought maybe there is an odd default of 32x32 or 48x48 etc.? But if I set the "autosize" property to true the button just collapses to be even smaller. I can resize the button but the icon will get no larger.

If there is no way of getting large icons onto buttons I would be very grateful for any recommendations of code project button enhancement submissions that are know to work with the dialog editor as custom controls.

I have spent hours searching but have found no answers - apologies if I have missed something simple.

I noticed from a quick play around that the C# dialog editor and button control works straight off from a PNG.

Best regards,

expedient.tec 6-Apr-11 1:55am    
Dear Hans,

Many thanks - I had come across this in my own searching for the answer but had missed that the solution included the visual studio "dialog editor".

I neglected to use the phrase "resource editor" when trying to narrow the search down within the page - which I could have done as I programmed in GEM in the 90's and that was the standard phrase then. The following steps below take from the link you kindly passed are indeed dialog editor friendly with a trivial addition step of a load image call. I will post a comment here tomorrow explaining how I got on. Lastly I would be still interested in the query about icons on buttons - are they always destined to be annoyingly small or can you get them to be larger some how straight out of VS2010 - with no additions?

Best regards,



Add a button to your dialog box with the resource editor, set the resource ID, and erase the text in the Caption box. You could set the style to Owner Draw, but you don’t need to because the code will automatically set this style.

Using the Class Wizard, add a variable to the ID you just created. In this example, I set the ID to IDC_PLAY, and the variable name to m_cPlay. Edit the dialog’s .h file, and change the control from CButton to CGdiButton. Don’t forget to include the file “GdipButton.h”.

In the resource editor, import the .png file from the res folder and set the resource type to PNG. Using PNG is just convention, it can be anything as long as the code matches what you name it. Right click on IDR_PNG1, select Properties, and rename it to something useful, IDR_PLAY in this example.
Step 3 – Add the LoadStdImage() function

Now, we just need to load the image to the button on initialization. In the OnInitDialg() function, add the following code near the bottom:

Hans Dietrich 6-Apr-11 2:10am    
Hi Neil,

(You responded to your own message rather than mine, so I did not get the notification.)

I have always found the CButton class to be annoyingly limited. In fact, I wrote my own XButtonXP class to get around some of these problems. Having said that, I can't say that you can't do what you want with CButton; I have just not seen it done. Maybe someone else can give you a better link.
expedient.tec 6-Apr-11 9:23am    
Hi Hans,

Many thanks for your reply - this is my first code project question and I missed the reply link at the top and only saw the add comment one at the bottom!

I tried the sample application - that compiled OK and ran - I only had to resize the dialog which had messed up the user interface (1/4 of it back & white badly drawn).

Then I followed the instructions and got this error:

...memdc.h(17): error C2011: 'CMemDC' : 'class' type redefinition
1> c:\program files (x86)\microsoft visual studio 10.0\vc\atlmfc\include\afxcontrolbarutil.h(64) : see declaration of 'CMemDC'

In following the instructions, to 'Add these files to your project' I used right click on source files > add existing item for "GdipButton.cpp" and right click on header files > add existing item for "GdipButton.h" "MemDC.h" & "CGdiPlusBitmap.h" in the VS2010 solution explorer pane.

In trying to fix that I came across this question but while replacing all uses of CMemDC to YA_CMemDC cleared error C2011 I am now left with lots of errors as if GdipButton.h is not included when compiling GdipButton.cpp

the pattern of includes I have used is supposed to match the pattern I saw in the sample applications, that is:

#include "MemDC.h" placed in the dialog class .cpp file that will have the buttons and
#include "GdipButton.h" placed in the dialog class .h file that will have the buttons

I am trying to shoehorn the GDI buttons into an existing project and am hoping to avoid doing it the other way around (shoehorning in my application into the GDI Button sample).

Thanks in advance for any help.

Hans Dietrich 6-Apr-11 9:48am    
The file memdc.h is from an article here on CP. Then MS came up with their own class, and called it by the same name!@&*$!!

Easiest fix: rename memdc.h to xmemdc.h, and rename the class to CXMemDC (naturally, the MS version is not compatible).
expedient.tec 7-Apr-11 5:40am    
Hi Hans,

I had renamed the class in the post above but was then getting many errors - it was as if GdipButton.cpp did not have site of GdipButton.h despite #includes I will check there order. I havent renamed the .h file - I can try that but I dont think thats the problem.

I started to do things the other way around but then the CPJNSMTPConnection code I have working in my application wasnt compiling in the GdipButton sample application. I must be doing something wrong bringing the additional code in. I would be very grateful if you could allow me to send you my modified GdipButton VS2010 solution files for you to try compiling in case you could quickly spot something?

Best regards,


1 solution

Share this answer

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