Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

c++ - 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.
    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...