Link to home
Start Free TrialLog in
Avatar of mahtieubrault
mahtieubrault

asked on

Changing the application icon on the fly

I'm writing a simple SDI MFC application. The application is loading in the InitApplication (loading IDR_MAINFRAME by default) when registering the window class. I would like to change that icon later in the execution because I don't have it at that time. Later, in the InitInstance(), I load a resource DLL and then I would be ready to change the icon of my application.

How can I change the icon of my application after the InitApplication()?
Avatar of BeyondWu
BeyondWu
Flag of United States of America image

HICON hNewIcon = LoadIcon(...);
::SetClassLong(m_hYourWnd, GCL_HICON, (LONG)hNewIcon);
Avatar of fl0yd
fl0yd

Since you are using MFC you could also use:

AfxGetMainWnd()->SetIcon( ::LoadIcon(...), FALSE );
Avatar of mahtieubrault

ASKER

Thanks guys but there is one last thing. The icon I use has a lot of formats (16x16,32x32,48x48,64x64). If I put the icon as the regular MyApp.ico in the res directory, the icon is displayed fine (the 16x16 version in the taskbar). But if I use one of your calls, it seems like it uses a bigger icon and shrinks it. That looks ugly.

I tried
AfxGetMainWnd()->SetIcon( ::LoadIcon(...), FALSE )
AfxGetMainWnd()->SetIcon( ::LoadIcon(...), TRUE )
but it doesn't change anything.

btw, it workd fine for a SDI app, the icon glitch is only with my dialog-based application.
Make another call,
::SetClassLong(m_hYourWnd, GCL_HICONSM, (LONG)hNewIcon);
Doing the two calls with SetIcon (big and small) or the two calls with SetClassLong (big and small) don't change anything. The application icon in the taskbar is still blurry like it was srunk from another format that 16x16. In my SDI application it works fine!
ASKER CERTIFIED SOLUTION
Avatar of fl0yd
fl0yd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks it works good. I have to do one LoadImage (16x16) for the icon in the taskbar and one LoadImage (32x32) for when I do ALT-Tab.