Eddy Lin Eddy Lin - 8 months ago 106
C++ Question

How to set icon for c++ application window and icon for task bar?

How to set these icons for application?

I am using visual studio.

enter image description here
enter image description here

The icon on application window is empty.

I have already added a project.rc file to project and have added icon resource into it

And it shows the correct icon on the task bar when I dock it to the task bar.

enter image description here
enter image description here

Answer Source

I've found 2 methods of doing this after a cursory search, assuming you are dealing the the WIN32 api in some form.

1.Set the hIcon member of the WNDCLASSEX structure used to register your main window to an appropriate value like the following code does. (Where EXAMPLE_ICON is your icon resource)

WNDCLASSEX wcex = {}; 
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(EXAMPLE_ICON));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = "ExampleIconWindowClass";
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

2.You can send a WM_SETICON message to your main window like the following (Again, where EXAMPLE_ICON is your icon resource)

HICON hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(EXAMPLE_ICON));
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM)hIcon);

Here is the end result of either of these methods: Finished Product

Sorry about the poor formatting, I'm still unfamiliar with this website.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download