HyperByter
HyperByter

Reputation: 9

Windows title bar icon

I have a problem with setting the titlebar icon for my application.

MainWindow.cpp:

#include "../../res/Icons.h"

void MainWindow::Create(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
    const wchar_t CLASS_NAME[]  = L"MainWindow";
    WNDCLASSEX wc = {};
    wc.cbSize = sizeof(wc);
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hIcon = LoadIcon(NULL,IDI_MYICON);
    wc.hIconSm = LoadIcon(NULL,IDI_MYICON);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(RGB(255,255,255));
    RegisterClassEx(&wc);

    HWND hwnd = CreateWindowEx(0, CLASS_NAME, wstring(Language::wText[1].begin(),Language::wText[1].end()).c_str(), WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL);
    ShowWindow(hwnd, nCmdShow);

    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

Icons.h

#define IDI_MYICON 1000

Icons.rc

IDI_MYICON ICON "App.ico"

It compiles fine, and the icon is shown in the Taskbar, as well as in the executable file, but not in the titlebar. The icon is a standard ico with dimensions of 32x32. I have even tried using LoadImage(), but same result.

image

Upvotes: -1

Views: 2552

Answers (3)

Joe B.
Joe B.

Reputation: 1

This worked for me:

wndClass.hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON5), IMAGE_ICON, 16, 16, 0);//PROGRAM ICON

Upvotes: -1

Drake Wu
Drake Wu

Reputation: 7170

The resource header file needs to be used in conjunction with the .rc file (i.e. #include "icon. h" in icon. rc), otherwise the specific icon file (path is specified in the .rc file) will not be found. In addition, if "icon.h" does not end with an empty line, you will get an unexpected end of file found error.

Upvotes: -1

Anders
Anders

Reputation: 101636

You are passing NULL to LoadIcon, you should pass the HINSTANCE of your application to load from your own resources.

Upvotes: 0

Related Questions