Problems with setting application icon - c++

(I'm using Visual Studio 2008, though I remember having similar problems with older versions as well.)
I've tried several different methods (many of them mentioned in this other question), but I am still having some strange issues:
When including an icon as a resource, it does show up as the executable file's icon immediately, but for the icon to show up on the taskbar, I have to restart the computer. Until then, it continues to show up as whatever the previous icon was. Cleaning the solution, restarting VS, doesn't have any effect. Not a really big issue, as it won't affect a released exe, but it would be nice to know where it's keeping the old icon cached and how to get rid of it.
No matter what I do, the icon displayed when alt-tabbing is the default app icon (square and white and generic). This includes embedding the icon in the executable, as well as setting ICON_BIG with WM_SETICON.
As for the second matter, my code looks something like:
hIcon = (HICON)(
LoadImage( NULL, szFilename, IMAGE_ICON, 32, 32, LR_LOADFROMFILE ) );
if( hIcon )
SendMessage( hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon );
However, after sending WM_SETICON, GetLastError() returns 6, "The handle is invalid.". hWnd is a valid window handle, and hIcon appears to be a valid icon handle. I've tried searching for reasons why WM_SETICON could cause that error, and at the very least, to figure out WHICH handle it thinks is invalid, but no luck yet. I've cleared the error code immediately before calling SendMessage(), so it has to be set somewhere in the processing of the message.
I tried an alternate method, loading the icon from the exe itself, where the ID of the resource is 101 (it's the first and only resource included):
hIcon = (HICON)(
LoadImage( GetModuleHandle( NULL ), MAKEINTRESOURCE( 101 ),
IMAGE_ICON, 48, 48, 0 ) );
if( hIcon )
SendMessage( hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon );
... but the same thing happens; after calling SendMessage(), GetLastError() gives the same error status.
I've tried different dimensions (such as 48x48, all of which are present in the icon file), but to no different effect. I know it's definitely finding and loading the images, because if I specify a size that doesn't exist or an invalid resource ID or the wrong filename (depending on how I am loading it), it fails out long before SendMessage().
Strangely, if I specify ICON_SMALL instead of ICON_BIG, the call succeeds with no error status, but from the docs, I need to use ICON_BIG to set the icon used while alt-tabbing. Also, if I use ICON_BIG but load the 16x16 icon, I get no error status, but nothing changes.
Any ideas about what could be causing WM_SETICON to fail? Anything terribly wrong with any of the code I've posted (aside from formatting/style/casting issues, as it's simplified to just the basics)?

I've revisited this to see if I can close out my question. I have been unable to get the app's icon to show up in the alt-tab list just through embedding it in the executable; it will show up in the taskbar, as the file's icon in Explorer, and elsewhere just fine.
I figured I'd try something simpler for setting the icon manually, and went with LoadIcon() instead, as the code below shows:
HICON hIcon = LoadIcon( GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1) );
if( hIcon )
{
SendMessage( GetHandle(), WM_SETICON, ICON_BIG, (LPARAM)hIcon );
DestroyIcon( hIcon );
}
// ... Same for ICON_SMALL
This seems to have done the trick. I really don't know why, but so far it's the only change that had any effect. It's really not an issue I should spend any more time on, so I'll just go with this.

OK, this worked a treat for me :
HICON hIconSmall =(HICON)LoadImage(handleToYourApplicationInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON,16, 16, 0);
HICON hIconLarge =(HICON)LoadImage(handleToYourApplicationInstance, MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON,256, 256, 0); // Big for task bar, small loaded otherwise.
SendMessage(yourWindowHandle, WM_SETICON, ICON_SMALL, (LPARAM)hIconSmall) ;
SendMessage(yourWindowHandle, WM_SETICON, ICON_BIG, (LPARAM)hIconLarge) ;

I've used a single .ico file with multiple resolutions 16x16, 32x32, 48x48, 96x96 without problems, as application icon. Then windows will pick up the right size.
The windows shell have an icon cache, there is a trick to restart it without rebooting or logout from the current session, or killing explorer.exe from task manager.

For anyone coming to this same difficulty if you are going to change ICON_BIG you must first send WM_SETICON with ICON_SMALL and then proceed to ICON_BIG.
For example:
SetLastError(0);
SendMessage(windowHandle, WM_SETICON, ICON_SMALL, (LPARAM)iconsmallHandle);
[do error handling]
SetLastError(0);
SendMessage(windowHandle, WM_SETICON, ICON_BIG, (LPARAM)iconbigHandle);
[do error handling]
You will need to use SetLastError after the first SendMessage to clear any returned error.
If you are just setting ICON_SMALL you can ignore ICON_BIG. For whatever reason in all of my tests you must set ICON_SMALL regardless if that icon needs to change or not, before attempting to change ICON_BIG, otherwise you will always get error code 0x6 (invalid handle).

If found the solution for me. I created an invisible CFrameWnd application window and then a few other main windows (which are dialog windows). The magic undocumented rule is: You have to change the big icon in the first created CFrameWnd. While each window keeps it's own ICON_BIG instance, it does not use them. So it seems not possible to show different taskbar icons for different windows inside one application.

Related

Get icon process from HWND, process name OR other process identifier

I have the process name and the handle (HWND) of its window. I want now to get the relative icon (if available). Searching through MSDN, I found ExtractIcon() to get the handle to the icon from a given exe name, and GetIconInfo() to get "information" of the icon from the HICON. I don't know if it's the right way to do this, and how to retrieve correct information to show (in a second moment) the icon without the handle to the icon.I have to send this information to another process (through socket) that has to show the icon.In the ICONINFO structure there are HBITMAP fields that contains the bitmap (black&white and with colour). Is it useful?
you can use the API GetClassLong to retrieve the icon associated with your program then use SendMessage API passing the hwnd of of the window you want to change it's icon.
in this Example I extracted the icon from my application then set it to Calculator. my windows calculator is open before sending to it the icon:
case WM_LBUTTONDOWN: // just for explanation so left clicking in your client area and see the result
{
HICON icon = (HICON)GetClassLong(hWnd, GCL_HICON);
HWND hCons = FindWindow(NULL, "Calculator"); // I already opened windows calculator. you can use any other window but be sure to get its valid Handle
if(!hCons)
MessageBox(0, "\"Calculator\" windows is not found!", 0, MB_OK|MB_ICONHAND);
SendMessage(hCons, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)icon); // setting the icon
}
break;

WM_SETICON not working when program launched via shortcut

I am using WM_SETICON to change the icon of an application. This works when the program is run in VisualStudio debugger, or via commandline, but uses the wrong taskbar icon when launched through a desktop shortcut on Windows 7. Right clicking to pin the program to the taskbar and then unpinning it causes the icon to display correctly.
HICON icon = (HICON) LoadImage(NULL, iconStr, IMAGE_ICON, 32, 32, LR_LOADFROMFILE| LR_SHARED);
SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM)icon);
SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)icon);
SendMessage(hwnd, WM_SETICON, ICON_SMALL2, (LPARAM)icon);
It seems as though something about launching through a shortcut is causing the program to use a stale cached icon and ignore WM_SETICON. Also the small icon next to the program titlebar is updating properly, it is only the icon on the taskbar that has issues.
I really do need to set the icon programatically because it will change based on commandline. Does anyone know of a way to make this work? Thanks.
I did find a workaround. Since the exe referenced by the shortcuts can't change it's icon, it can launch an exe with a different filename that will then be able to change taskbar icons with WM_SETICON.
shortcuts (1-n) each have their own icon and point to IgnoresWMSetIcon.exe.
On startup IgnoresWMSetIcon.exe launches CanChangeIcon.exe then closes. CanChangeIcon.exe is then able to function normally.
Still interested in an explanation of why this is the case, if someone knows.

How to change icon of: taskbar, in the top left corner, icon when Alt-Tab is pressed, of my .exe

Platform: Windows 7
IDE: VS2013
Language: C++
Windows class: WinAPI
I have a custom image: Icon.png.
I also have a windows class from the msdn sample code to create a basic Win32 window.
It has two things hIcon and hIconSm of my windows class structure which I am not sure what they correspond to, but they both are probably related to icons.
I need to: Change the icon of the taskbar to Icon.png, do same thing for the icons in top left corner of the exe, and change the icon which displays when alt+tab is pressed.
So that means I need to know which variables I change, what functions to use, and what its parameters stand for. Also if any clicking in VS or additional file creation is needed I also need instructions on how to do that.
Any help is greatly appreciated.
PS I tried the stuff posted by other people on here but it either didn't work or the instructions were unclear, hence my asking for specifics.
So, I found my own answer.
A) I could meddle with resource loading (which I did, and came out frustrated and unsuccessful at linking it to the LoadIcon function.
B) Second way: Use LoadImage and cast return value to HICON (hacky apparently, but it worked for all icons I was trying to change!!!!! So, problem solved hah.
Below is the pseudocode I used into my project.
windowclass.hIcon = (HICON) LoadImage( // returns a HANDLE so we have to cast to HICON
NULL, // hInstance must be NULL when loading from a file
"iconfile.ico", // the icon file name
IMAGE_ICON, // specifies that the file is an icon
0, // width of the image (we'll specify default later on)
0, // height of the image
LR_LOADFROMFILE| // we want to load a file (as opposed to a resource)
LR_DEFAULTSIZE| // default metrics based on the type (IMAGE_ICON, 32x32)
LR_SHARED // let the system release the handle when it's no longer used
);
So, if you want some other customization to above, I recommend going to MSDN definition of LoadImage and customize your parameters according to it.

Setting icon for application VS Express 2012 c++

I have been trying to get an icon added to my application as a resource, so that it is displayed with my application in VS 2012 Express using c++. So far I have picked up the following code to add to my rc file from other questions and forums.
IDI_APP ICON "resources/Icon.ico"
The icon is displayed on the desktop with the exe, and on the taskbar when the program is running. However in certain situations such as on the task manager the icon for the application dies not show up and instead the default program icon is displayed. I was wondering if anyone knows how to alter my code so that the icon is always associated with my program. I have heard that the problem may result from needing different sized icons however I have many sizes of icons within my ico file created with the program IcoFX. I was also wondering if I needed to programmatically set the icon for it to work in any place the application is associated with. I have tried rebuilding and renaming my program to update the icon in the shell. I am using a sfml window instead of the winapi and a HWND window.
Double check that you created a single .ico file with multiple resolutions, usually 16x16, 32x32, 48x48, 96x96.
Load your icon with something like
ICON hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
Notify with Windows messages of the icon
//Change both icons to the same icon handle.
SendMessage(hwnd, WM_SETICON, ICON_SMALL, hIcon);
SendMessage(hwnd, WM_SETICON, ICON_BIG, hIcon);
//This will ensure that the application icon gets changed too.
SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_SMALL, hIcon);
SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_BIG, hIcon);
Finally reset the shell icon cache as described here or by just restarting/logging out.
Additional resources and references:
https://stackoverflow.com/a/19656000/1938163
https://stackoverflow.com/a/2723270/1938163

Show System Menu From Another Window

The project I'm working on right now is essentially an open source version of Ultramon (the multiple taskbar program). Thus think of the application to be just like the windows taskbar. Buttons for each window, etc. This is coded in C/C++ using WinAPI
I've just started doing it, and have some of the functionality down. However, I'm getting stuck at getting the system menus (eg the menus that you get when you rightclick on a taskbar 'button') showing when and where I want them to.
I'm trying to use:
HMENU menu = GetSystemMenu(item, false);
SetForegroundWindow(hWnd);
TrackPopupMenu(menu, TPM_LEFTALIGN | TPM_BOTTOMALIGN, 0, 0, 0, hWnd, NULL);
PostMessage(hWnd, WM_NULL, 0, 0);
item is the HWND I want the menu for, hWnd is the HWND for the button/item I want the menu to show up on. (its showing at 0, 0 for now, the top left corner of my main monitor).
This code works perfectly every time for a system menu which is customized. Eg: its NOT the standard menu of just "Restore", "Maximize" etc etc, it has some added in menu items. Those menus will always display where I want.
However, the default basic system menus (eg the ones with only Maximize, restore, close etc), will ONLY display for the FIRST time I use them. After that they refuse to show up. Unless I restart the computer, run it again, at which point they work perfectly.
Note: SetForegroundWindow(hWnd); and PostMessage(hWnd, WM_NULL, 0, 0); are in there because I read somewhere that the system menu would only work the first time unless I did that trick of bringing it to the foreground, but it still doesn't work correctly.
So does anyone have any idea how to get this working correctly? Or why it works only the first time after I restart the computer?
Just tested something, it will also show the system menu again for a window if I close the window (eg exiting the program) and re-opening it. But once again, it will only show it once then it stops working.
It might be hacky, but have you tried setting the window focus and then issuing an Alt+Space through something like SendInput?