How do I set window titlebar icon and alt-tab icon in SDL2 / C++ / Visual Studio 2012 express program?

不羁岁月 提交于 2019-12-10 17:36:55

问题


I have added a .rc file to my project with just the following:

IDI_MAIN_ICON          ICON          "..\\..\\icon_artwork\\windows_icons\\project.ico"  

The .ico file contains icons at 16x16, 32x32, 48x48 and 256x256 resolutions.

I see the icon in the taskbar and explorer (so there is no doubt the .ico is successfully embedded in the .exe), but I see the default Windows 7 application icon in the application's own title bar, and in alt-tab.

Is there a way to get the OS to see the embedded icon by itself in all cases, or do I need to write some application code for this? I notice that there is an SDL2 function

void SDL_SetWindowIcon(SDL_Window*  window, SDL_Surface* icon)  

but that takes an SDL surface, not the embedded icon resource from the executable.

Any ideas gratefully received,
Tony


回答1:


You should be able to get a SDL_Surface from a .png file using

SDL_Surface *IMG_Load(const char *file)

then pass it to your SDL_SetWindowIcon(SDL_Window* window, SDL_Surface* icon) method. I don't think the IMG_Load(...) method takes .ico files though, but a 32x32 .png version of your icon would be a good compromise... Not ideal but the best workaround I can think of ;)




回答2:


As it stands, no. SDL for some reason eats the icon for the actual application window. However you can use WINDOWS specific code to fix this. Keep in mind that this is not portable.

#if defined(_WIN32) && defined(GCL_HICON)
void setWindowsIcon(SDL_Window *sdlWindow) {
    HINSTANCE handle = ::GetModuleHandle(nullptr);
    HICON icon = ::LoadIcon(handle, "IDI_MAIN_ICON");
    if(icon != nullptr){
        SDL_SysWMinfo wminfo;
        SDL_VERSION(&wminfo.version);
        if(SDL_GetWindowWMInfo(sdlWindow,&wminfo) == 1){
            HWND hwnd = wminfo.info.win.window;
            ::SetClassLong(hwnd, GCL_HICON, reinterpret_cast<LONG>(icon));
        }
    }
}
#endif


来源:https://stackoverflow.com/questions/19311609/how-do-i-set-window-titlebar-icon-and-alt-tab-icon-in-sdl2-c-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!