Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an icon in Windows program

I'm trying to set a custom icon into my program but nothing happens.

  1. I created an icon using this tool.
  2. I created a resource file with contents:

    MYICON1 ICON  "glider.ico"
    
  3. I added the resource file into my Visual Studio 2013 project and set its type to "Resource Compiler". Type "Resource" doesn't work ("invalid or corrupt file: cannot read at 0x1A")

  4. I load the icon with the following code when creating a window:

    wc.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( MYICON1 ) ); // MYICON1 is defined in Resource.h as 101.
    
like image 210
SurvivalMachine Avatar asked May 25 '14 07:05

SurvivalMachine


People also ask

How do I add an icon to a program?

To do this, search for an app in the Start menu, then right-click its name and choose Open file location. In the resulting folder, right-click the program name and choose Send to > Desktop (create shortcut). Now you can modify the new shortcut on your desktop. Right-click it and choose Properties to open a new window.


1 Answers

Just to make sure that I have correctly understood your question…Everything compiles fine when you set the type of your resource file to "Resource Compiler", right? That is the correct setting, leave it there. The other stuff is for .NET applications.

So the problem is just that you aren't seeing the icon? What size icon did you create in the editor? The LoadIcon function is extremely old and can only load icons with the default size, generally 32x32. If you want to load icons with a different size, you will need to use the LoadImage function instead. Over 99% of the time, that's the function you should call to load icons. I haven't used LoadIcon in years.

HICON hIcon = static_cast<HICON>(::LoadImage(hInstance,
    MAKEINTRESOURCE(MYICON1),
    IMAGE_ICON,
    48, 48,    // or whatever size icon you want to load
    LR_DEFAULTCOLOR);

Like Retired Ninja suggests in his comment, be sure to test the return values of functions when the code doesn't work as expected. For example, test whether hIcon is NULL. If so, the LoadImage function failed, and you need to figure out why. Most likely, the parameters you passed were incorrect. Either there is no resource with that ID in your application module, or there is no icon with the corresponding size in the icon file.

Another tip for debugging purposes is to use one of the built-in system icons. If you can see that icon, you know everything is working correctly, and you can start swapping things out to load your custom icon. For example:

HICON hIcon = static_cast<HICON>(::LoadImage(NULL,
    MAKEINTRESOURCE(IDI_WARNING),
    IMAGE_ICON,
    0, 0,
    LR_DEFAULTCOLOR | LR_SHARED | LR_DEFAULTSIZE));

That should show the standard warning icon, like the one you see in a message box. If that works, but loading your own icon doesn't work, the problem is very likely to be with the .ico file that you created.

like image 66
Cody Gray Avatar answered Oct 12 '22 23:10

Cody Gray