Sign in to follow this  

Replacing titlebar icon

This topic is 4481 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)));

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
    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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endurion
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.

Downloading it now. In the meantime I tried making a new 16x16*16 colours icon. Didn't work either. [evil]

Share this post


Link to post
Share on other sites
Okay, my icons are labels as 1, and 2 (ordinal name) and they all have a green gearlike icon with 1033?

I tried all these numbers, always NULL

Share this post


Link to post
Share on other sites
Error 1813: The specified resource type cannot be found in the image file. [headshake] The icon works everywhere else. BTW, Thanks for all your help so far.

Share this post


Link to post
Share on other sites
Aha, it seems that the resource type for the icon is wrongly added. There are several predefined resource types (for icons it's RT_ICON) and it seems that your icon got the wrong type associated with it.

The entry in the .rc-file should look something like this:

IDR_MAINFRAME ICON "res\\AddressBook.ico"

Share this post


Link to post
Share on other sites
Quote:
Original post by Endurion

The entry in the .rc-file should look something like this:

IDR_MAINFRAME ICON "res\\AddressBook.ico"
That is what my entry looks like.

IDR_ICO_MAIN ICON "c:\code\gamename\gamename.ico"

Difference?

Share this post


Link to post
Share on other sites
Looks correct to me. Truly weird.

Have you tried using LoadImage instead of LoadIcon? AFAIK LoadImage is a bit more easy on the image types.

Would be interesting what resource types EnumResourceTypes comes up with as LoadIcon doesn't seem to find the RT_ICON type.

Share this post


Link to post
Share on other sites
If it wasn't an icon, why would it work in explorer for my .exe? Yes this is very odd. Could be that I'm using infraview to save my icon. Do you know a freeware program that I could use to load/save it?

-edit- LoadImage fails for the same reason.

Share this post


Link to post
Share on other sites
Sign in to follow this