How to Set Icon in a Windows Application using Visual Studio - c++

I want to add an icon to my application. I have added an icon resource. and its working file for the first icon. That means after I have compiled, the 16x16 icon of the exe file is same as the icon file used. but when the explorer window is zoomed to large icon size, then the icon of the exe file vanishes and another blank default icon is displayed.
How to set icon for large as well as small size display?

From your comments, it sounds like you have multiple icon files set for your project. That's not going to work the way you want, or at least it's the really hard way of doing it.
The simple way is to let the operating system handle it for you. Create a single icon file that contains multiple icon sizes. 16x16, 32x32, 48x48, and 256x256 are the minimum sizes needed for current versions of Windows, but you can certainly add more (like 24x24 or 128x128) to improve pixel fidelity at those sizes if desired.
You can use Visual Studio to create the icons. Opening an .ico file should automatically open its icon resource editor. But I personally find it difficult or cumbersome to use. I highly recommend using a third-party program like IcoFX to create your icons and push pixels around.

Related

MFC picture control changes size when DPI awareness disabled or running on Win7

I made an MFC app for my friend using VS2015 in Win10. It looks like this, which is exactly the same as in resource editor.
.
But when he ran the app on his computer in Win7, the Bitmap image in Picture Control enlarges and covers up some text boxes below, which looks like this.
.
After I searched and realized it may be related with DPI awareness. I disabled DPI-Awareness in property page of Manifest Tool and rebuilt. The same thing happened even when it runs in Win10.
Could someone help me explain the cause of this and find a solution to fix the size of the image control? Thanks.
The main problem is that a dialog from a resource is always measured in DLUs.
And DLUs are calculated from the size of the font, that is used for the dialog.
See this article how dialog base units are calculated.
Now you have a static picture control that is sized in DLUs. The bitmap is just scaled in pixels and is never resized, when you assign it to a static dialog control. And because the real size of the static control depends on the used font, you get different layouts for your dialog and your bitmap.
And because just the font changes when you choose no DPI awareness and because the font changes from windows version to windows version your dialog always look different.
Advice: Paint you picture your own and stretch it accordingly.
Also this stackoverflow question is nice documents and shows the effect of DLUs.
And here some code for auto sizeing picture controls.
An auto-sizing bitmap picture control
A simple image preview class using GDI+
CxImage
Normally, I prefer to keep control in my hand by using SetWindowPos() to set the size of image I want in different situations. You can use below two lines to control/set position and size of your image.
Assume ID of the Picture Control is IDC_STATIC2 then you can use like:
CStatic * pStatic = (CStatic *) GetDlgItem(IDC_STATIC2);
pStatic->SetWindowPos(NULL,20,20,50,50,0);

How to change button color?

I am developing a GUI application using Embarcadero VCL c++ IDE for windows OS. As part of this project, I have to change color of button with respect to an external state.
I understood that windows32 API will not allow to change the color of button.
Could you please suggest me, how to change button color?
Do you wish to change the background-colour of the button, or the text-colour of it?
Since windows has used visual themes for some time now, if you have commctrl loaded and include a manifest file, the button will be drawn using the default (current) theme.
Options I can see include (a) custom-drawing the background (b) changing the text-colour in the normal draw process (c) drawing the button without a theme (i.e drawing a 'flat' button).
You could simply draw a bitmap-button, changing the bitmap depending on the state of the button. You could also use a single bitmap, tinting it using the HSL or HSV colour-space, depending on the state.
As for the flat type of button, I think you can probably change it's background-colour in much the same way as you can change the colour of the text - by intervening during the standard draw process and changing the colour from 3D_FACE (or whatever it is, I forget) to whatever you'd like.
If you look at the calculator included with windows XP, you can see an example of changing the text colour.
CodeProject.com likely has a stack of articles that would help in this endeavour. :)

Icon with different resolution in MSVC2010

ALL,
I am making an application where I need to use an icon.
I have 2 icon files: myicon-16.ico and myicon-32.ico. First has resolution 16x16 and second - 32x32.
Now when I opened MSVC 2010 project (C++) I see that the default icon file has 2 icons combined, i.e. it has a resource for 16x16 and 32x32 in one file. What I mean is when I open the resource file in Visual Studio solution and click on the standard icon I see many different resolution for the icon. And I can select each and the bitmap (icon) will be displayed with an appropriate resolution. But when I open the rc file in the text editor I see only 2 lines: one with my own icon and one with the standard icon "IDI_SMALL".
What I need to do to make the same thing with my 2 files? What I want is when I open resource file in VS and click on my icon resource I want to see 2 icons with 16x16 and 32x32 resolutions. AFAIU, I can only place an additional icon resource in the rc file. Or I am wrong?
Please advise.
You can use VS2010 to add additional image types in your ico file. Just open an ico file with VS2010. Say, open myicon-16.ico. You will see just one icon there - 16x16. Now open menu Image->New Image Type (or use right click->New Image Type, or use keyboard shortcut Ins). You will be presented with a choice ranging from 16x16 1 bit to 128x128 24 bit. Just insert as many as you like.
For your case you need to insert an empty 32x32 into myicon-16.ico, then copy and paste from your 32-pixel file.
On the side note. Who draws the icons nowadays. Just google free icons, and you will find tons of icon libraries on the Internet with sizes from 16x16 to 256x256 png.
Yes, you need to combine your images into one ICO file that has all the data.
From Wikipedia:
ICO files contain one or more small images at multiple sizes and color
depths, such that they may be scaled appropriately.
There are many image editors that can do this, personally I've used GIMP to create multi-layered png files, and then save as .ico to get the final result.
You also should be able to right click in the list of image sizes in VS2010 and then click "New Image Type" to add different resolutions within the built in ICO editor. You can then copy and paste your image data from another editor into Visual Studio.

Expanding Qt interface if necessary

I'm working on a Qt project and I've noticed a persistent problem with some GUI forms. The form looks fine on KDE (bottom picture) and Windows , but when the app runs on anything GNOME3-based (like Unity or GNOME3 itself) some parts of the form are hidden from view. (Top picture, everything below the Sort Ascending radio button is cut off)
The problem seems to be with how Qt layouts handle large font sizes. If the user is using normal-sized system font (<= 10pt) everything works fine. If they are using larger fonts, the form is not large enough to accommodate everything. Other forms affected by this bug are merely crowded, but that isn't as serious as having vital controls out of bounds. The layout doesn't want to resize itself to take advantage of new space if I enlarge the dialog. Is there an easy way to make it do this or do I need to hard-code it? Originally the code prevented dialog resizing during runtime, but restoring that functionality didn't fix the bug. Even if the dialog can expand, the problem is the layout won't expand with it.
Up until now, I've made all affected forms oversized to compensate for this bug, but it looks strange to have the dialogs much bigger than they need to be on Windows and KDE systems where the font is the proper size. Is there a way to cause an affected dialog/layout to resize itself so everything fits properly at runtime? If so, how would the program detect it when parts of the GUI are out of bounds? I would prefer not to force a certain font size (some people may prefer large fonts due to vision problems).
Thanks in advance for help.
The fix for this is using a different approach when displaying forms. A more dynamic way as I'll describe. I've successfully used this approach on Windows with 96 and very high DPI modes (over 120).
1.
Query the OS and get the user's chosen font for a particular system item; say the font used for the window caption or system dialog boxes. Also you could allow the user to choose their font later if they desired. Use True Type fonts when doing this if possible.
2.
Using that font, construct a string object that you'll use for a label or edit control (I don't know what this is for QT, for Windows it is GetTextExtentPoint32) and pass it to a system function to determine the width and height of the string for your environment.
3.
Given the above value, place the control and dynamically resize the form with the padding all around the control as you like. For buttons you might always add a certain percentage of pixels above and below the button to taste.
4.
For graphical elements like Bitmaps and jpegs, again query the OS for the current DPI settings of the monitor and use larger, pre-made resources. Naturally, all text around theses elements will be dynamically placed on the fly.
Note that on Windows you'll need to mark your exe as high dpi aware using a manifest.

In resources of a executable file, how does one find the default icon?

i need to find the default icon of a windows executable (PE file = dll, exe, com..) programatically. I do know how to walk throught the resources and identify what is an icon, what a cursor etc, but as far as i know none of the icons is in any way marked as the default one. So, does somebody know, how to find the default icon? Moreover, i do not want to use any windows api call, i want to code the function myself. The problem is that i don't know which one of all the icons is the default one.
After a lot of searching, i found out that the default icon is not the one with the lowest id.
Windows use several sizes of one icon for various things. For more information, look here, but in short here is the important information:
When the system displays an icon, it must extract the appropriate icon image from the .exe or .dll file. The system uses the following steps to select the icon image:
Select the RT_GROUP_ICON resource.
If more than one such resource
exists, the system uses the first
resource listed in the resource
script.
Select the appropriate RT_ICON image
from the RT_GROUP_ICON resource. If
more than one image exists, the
system uses the following criteria
to choose an image:
The image closest in size to the
requested size is chosen.
If two or more images of that size
are present, the one that matches the
color depth of the display is chosen.
If no images exactly match the color
depth of the display, the image with
the greatest color depth that does
not exceed the color depth of the
display is chosen. If all exceed the
color depth, the one with the lowest
color depth is chosen.
Note: The system treats all color depths of 8 or more bpp as equal. Therefore, there is no advantage of including a 16x16 256-color image and a 16x16 16-color image in the same resource — the system will simply choose the first one it encounters. When the display is in 8-bpp mode, the system will choose a 16-color icon over a 256-color icon, and will display all icons using the system default palette.
Since the requested size is 16x16 (because thats the system small icon size, ie. the default icon size) i think we can say that the default icon is the icon from the first icon group which has the smallest size (no smaller icon than 16x16 can exist) with the highest color depth.
EDIT: a small correction. A icon of size smaller than 16x16 might apparently be in the resources, but that indicates that the file does not have a default icon and the system then does supply its own icon instead.
The first one you find is the default one.
The default icon is simply the icon with the lowest id, so, by definition, is the first icon discovered when enumerating resources.