How to change freeglut main window icon in C++?

倾然丶 夕夏残阳落幕 提交于 2019-12-13 18:21:55

问题


I have a DLL written in C++ that uses FreeGlut to visualize some data. I want to change the icon of the main (free)glut window.

I've read that it is impossible, but in the docs I see:

GLUT_ICON - specifies the icon that goes in the upper left-hand corner of the freeglut windows.

How can I change icon for (free)glut window, if possible?


回答1:


OK, I did it:

  1. Create a Resource for the project and add an 32x32 icon (edit it or import). This icon will get the ID equal to IDI_ICON1.
  2. Include the "resource.h" file.
  3. Create the glut window like this:

    glutCreateWindow("VIZ"); 
    HWND hwnd = FindWindow(NULL, _T("VIZ") ); //probably you can get the window handler in different way.. 
    

Now get the icon - it is in your DLL file, with IDI_ICON1 id, so we use:

    HANDLE icon = LoadImage(GetModuleHandle(_T("NAME_OF_YOUR_DLL")),  MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32, 32,  LR_COLOR );
    //You can also prepare second, smaller (16x16) icon - it looks better in title bar. 
    //...

Now send this message to the Window.

    SendMessage(hwnd, (UINT)WM_SETICON, ICON_BIG, (LPARAM)icon);

That's it! Probably some cleaning would be nice.

This solution does not require to deploy .ico file. If you prefer you can ommit resource file and load icon with:

    icon = LoadImage(GetModuleHandle(), _T("icon.ico"), IMAGE_ICON, 32, 32, LR_LOADFROMFILE | LR_COLOR);

You can also use LoadIcon function, but then you can't select icon size.

Manuals: LoadImage SendMessage LoadIcon

Edit:

I think it's not the best solution, so you're welcome to write yours. Maybe using GLUT_ICON ?



来源:https://stackoverflow.com/questions/12748103/how-to-change-freeglut-main-window-icon-in-c

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