Replacing titlebar icon

Started by
17 comments, last by Endurion 18 years, 6 months ago
I made an icon to go with my program the other day, and I placed it in a resource file and compiled it. It immediatley changed so that my .exe had the icon representing it. I didn't think it would work that easy (never done that before). But when I opened my program I still have the default on the titlebar. I know I can change it while the window is being created, however that's buried in my OpenGL window framework that I do not want to change so that I need to include an icon load into it. I want to just change my icon at runtime. I looked up some stuff on MSDN and found these two methods, but they are not working. They compile, but none have an effect when executed. Note that GetHWND() just returns the handle to my main window, and it DOES work. rsc.h, defines my 32x32 .ico file
#define IDR_ICO_MAIN 411
HICON hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_ICO_MAIN));
SendMessage(GetHWND(), WM_SETICON, ICON_BIG, (LPARAM) hIcon );
SendMessage(GetHWND(), WM_SETICON, ICON_SMALL, (LPARAM) hIcon );
SetClassLong(GetHWND(),GCL_HICON,(LONG) 
LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_ICO_MAIN)));
Advertisement
wait for it...

wait for it...



BUMP!
It should work exactly that way. Show your window proc, are you passing unhandled messages on to the DefWindowProc?

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

I am apssing them to the deault handler. Could it be something with my icon? Does it need to be 8bit?
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){    switch (message)    {                case WM_ACTIVATE:        {            if (LOWORD(wParam) == WA_INACTIVE) bActive = false;            else             {                                bActive = true;                                //must reacquire the input devices                if (bAcquired) InputAcquire();            }            break;        }                         case WM_CLOSE:        case WM_QUIT:        {            bDone = true;            break;        }        case WM_DESTROY:            PostQuitMessage (0);            break;        default:            return DefWindowProc (hwnd, message, wParam, lParam);    }    return 0;}
Did you check if the HICON gets a valid value after LoadIcon? That's the last thing that might fail.

The icon image depth should not matter at all.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

    HICON hIcon = LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDR_ICO_MAIN));    if (hIcon == NULL)    {        MessageBox(GetHWND(),"Icon failed to load!",APP_TITLE,MB_OK|MB_ICONERROR);    }


Yep. The box is popping up. Does it need to be 16x16? I'm not sure what can be wrong here. It's in my res file and it shows up in the .exe when I look for an icon in it with explorer and the .exe even uses it in explorer. :S I wonder if GetModuleHandle(NULL) is doing what I expect. Is that how I properly get my hInstance? It has always worked before when I'm loading controls and stuff.
Hmm, probably the icon id is off?

Try to get hold of Resource Hacker or open the .exe in your Visual Studio. It should show you the resources (types and ids) inside. Maybe the define for the icon id got mangled.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

I read the LoadIcon docs again and and swapped out my icon for default icons and it worked. I don't have visual studio. I'm using Codeblocks and VC++Toolkit. :S
I just tried reducing the icon from 24bpp to 16 colours, it still doesn't load. [sad] Comes up NULL.
Weird, try to open your exe with Resource Hacker (it's a nifty freeware tool allowing you to read and even modify the resources of exes and dlls). Look esp. at the resource sections and the ID. It should contain one Icon and one Icon Group. The id of the icon group should resemble the define you pass in LoadIcon.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement