2

I write a program in C / C++ / OpenGL / GLUT under Windows. I want to add an icon to my program. I use MinGW and command prompt to compile my only one .cpp file. I wrote myself some .bat files, which I can compile to debug-mode, release-mode, and things like that.

I want to add an icon to my program. So, I made a new compile bat, that looks like this:

windres resource.rc object.o
set PATH=%PATH%;c:\mingw\bin
g++ program.cpp object.o >>>some other settings<<<

I have a .ico file, and a resource file in "resource script" language. Resource file looks like this:

#ifndef _resource_rc
#define _resource_rc

MAINICON ICON "icons\\icon1.ico"

#endif

Now, the problem: I compile these things, and I got a release .exe, if I looks it in a file-handler program, I can see the icon near the .exe file, and if I run it, there is an icon in the corner. But, my program starts with a console-mode menu. If you travel in the menu, and choose a given menu-point, after that, you can start a graphic window, which wrote in OpenGL / GLUT. The problem is, that the console-window has the icon, but the OpenGL / GLUT window don't. It has a basic .exe icon (blue border, white rectangle inside), and not my specific icon. This is not good :-(

How can I solve, that the OpenGL / GLUT window has my icon, and not the default?

Thank for answers, and sorry because of my grammar :-)

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 2
    possible duplicate of [How to change freeglut main window icon in C++?](http://stackoverflow.com/questions/12748103/how-to-change-freeglut-main-window-icon-in-c) – jamesdlin Aug 05 '13 at 19:05
  • @jamesdlin no, that one is for DLL, where you have to call winapi, and this one is for an executable, which can be simply done by set the GLUT_ICON or 0x1000, 0x1001, the windows way, and both without calling winapi to change the icon by hand, which is done by the linker. – user26742873 Jul 28 '20 at 05:04

3 Answers3

2

The only way to do this in a cross-platform way, if it matters that much to you, is to make your application not a console application. Make it a Windows application that creates a console. so your main window will be the OpenGL window, and the console window will be secondary.

Creating a console window on Windows and hooking it up to standard in/out is not a trivial process, but it can be done.

Community
  • 1
  • 1
Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
1

For MSVC that's simple!

You don't need to use winapi. Just simply create a resource file

GLUT_ICON ICON logo.ico

where logo.ico is your icon file.

Then link it with your program

rc resoure.rc
link main.obj resource.RES

And that's it! GLUT will set everything for you: icon in the file explorer and icon in the runtime.

You can also do it the windows way. In resource.rc:

#define ICO_BIG   0x1000
#define ICO_SMALL 0x1001
ICON_BIG ICON "logo.ico"
ICON_SMALL ICON "logo.ico"

And everything will be the same. You don't need to set icon by hand.

user26742873
  • 919
  • 6
  • 21
0

You can explicitly set the icon for a window by sending it a WM_SETICON message with the desired HICON. (And you can get the HICONs from your resource by calling LoadImage.)

For example:

SIZE smallIconSize = { GetSystemMetrics(SM_CXSMICON),
                       GetSystemMetrics(SM_CYSMICON) };
HICON icon = static_cast<HICON>(LoadImage(hInstance, MAKEINTRESOURCE(MAINICON),
                                          IMAGE_ICON,
                                          smallIconSize.cx, smallIconSize.cy,
                                          0));
SendMessage(hWnd, WM_SETICON, ICON_SMALL, reinterpret_cast<LPARAM>(icon));
jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • OK, how do you get a HWND from a FreeGLUT window? – Nicol Bolas Aug 05 '13 at 19:04
  • @NicolBolas Ugh, good question. I suppose you can use `FindWindow`, as described in http://stackoverflow.com/questions/12748103/how-to-change-freeglut-main-window-icon-in-c – jamesdlin Aug 05 '13 at 19:07
  • It looks good, but this is a Win32 code. If somebody use OpenGL, he have to use something to communicate with it: GLUT, freeGLUT, Win32, ... And with that, you can make a window, use keyboard and mouse, and so on. I'm using GLUT for that, not Win32. Can I mix them? I mean, I have a lots of GLUT-code, but to change icon I would use Win32 code, can I do that? – user2654137 Aug 07 '13 at 06:30