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

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.

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);

SHGetImageList returns an icon that is too small for the size

I'm trying to retrieve file icons on Windows. I follow this guide http://pogopixels.com/blog/getting-the-48x48-or-256x256-icon-of-a-file-on-windows/
I use SHIL_JUMBO to get the maximum size possible. However, not all icons returned are big enough, such as the QuickTime icon in the attached image. The size of the whole pixmap returned is still 256x256 but it does not fill the entire space. My program will then scale it down, making it too tiny to see.
I'm wondering if I can retrieve some extra info such as the size of the original icon, so I know that it's too small to scale down.

What size of ImageList icons do I need to make & load for my app (considering higher DPI)?

I have a CListCtrl control (or a ListView in Win32) that is created with LVS_REPORT style.
I am intending to display icons in its items as such:
But the question is what size of icons do I need to make and load?
Let me explain. From the old Win32 samples, I can see that everyone creates image lists with 15x15 pixel icons. But the issue with those is that it looks horribly pixelated on any modern PC with higher DPI settings. Thus I was looking for a dynamic way to determine the appropriate size of image lists for the CListCtrl.
And also the first part of the question, what icon size should I make originally?
EDIT
PS: Since DPI scaling came up, how do you find it out? I'm currently using the following approach:
//No error handling for brevity
HDC hDC = ::GetDC(hAppsMainWindowHandle);
int nCx = ::GetDeviceCaps(hDC, LOGPIXELSX);
int nCy = ::GetDeviceCaps(hDC, LOGPIXELSY);
::ReleaseDC(hAppsMainWindowHandle, hDC);
//I technically get horizontal & vertical scaling --
//can those be different?
double scalingCx = (double)nCx / 96.0; //1.0 = 100%
double scalingCy = (double)nCy / 96.0;
Is font scaling something different?
A list view uses a "small" or "large" image list depending on its mode. In report mode, it uses the "small" image list. You can use GetSystemMetrics() to get the dimensions of "small" images using the SM_CXSMICON and SM_CYSMICON metrics (use SM_CXICON and SM_CYICON for "large" images).
Note that the returned values will be virtual/scaled if your app is not DPI-aware, so to get accurate values, make sure it is DPI-aware via SetProcessDPIAware(), SetProcessDpiAwareness(), or a DPI manifest.
Update: I just ran across this function, might be useful for you when writing a DPI-aware app:
LoadIconWithScaleDown()
Make larger images and let the API scale them down to smaller sizes.
The report style list view wants small icons, that is icons with SM_CXSMICON by SM_CYSMICON metrics. Assuming your app is high DPI aware, then the actual value of these metrics depends on the user's chosen font scaling or DPI setting. So up front you cannot know what size icons should be used. You have to query the system metrics at runtime, and use appropriately sized icons.
Now, what size icons you include in your executable depend on what DPI settings you wish to support. Back in XP days you could reasonably expect to encounter 100% and 125% font scaling. These days, high density display panels are common and you really need to support larger ratios. At least 150% and 200%, and quite probably 175%.
The closest I can find to guidelines is in this MSDN article: http://msdn.microsoft.com/en-US/library/windows/desktop/dn742485.aspx
This article was written around the Vista time frame and already shows its age. I think you have to use these articles as a guide and adapt to the hardware of the day.
You will also find that people run their machines at font scaling values in between the round numbers listed above, and in the article I link to. One of my colleagues runs at 120%. Interestingly this has highlighted various bugs in our code so it's always useful to have someone dog-fooding your program.
When you build your image lists at runtime, size them according to system metrics. And try to avoid scaling icons. For instance, if system metrics suggest an 18px icons, and you only have 16px and 20px icons, then make a new 18px icons. Fill the image with transparent pixels, and blit the 16px icon into the middle of this 18px image. Make the icon out of that. This approach will avoid aliasing problems.

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.

C++ Win API Specify Custom toolbar Bitmap?

Picture and c++ files can be found here: http://phantomworksstudios.com/cpp/ss/
Ok so I have been having a lot of problems with my toolbars. such as them not displaying correctly, can't set the button size via height and width no matter what I do, the bitmaps are off set by 1 pixel to the right which are the back,forward,stop, etc icons. and the other icons such as the clip board and new file which is the last tool bar under the rest are cut off bad. I think its still trying to strip the tool bar list as 16x16 but the bitmap I have icons as 14x14.
The icons are within a bmp file which is 32 bit depth
one is 16x16 while the last one is 14x14
I have no idea what is going on and no matter what I can't seem to get it working like it should: