Search code examples
c++winapiiconsglutfreeglut

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


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?


Solution

  • 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 ?