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()?
Who is Participating?
fl0ydConnect With a Mentor Commented:
I guess I found a solution: Use ::LoadImage(...) instead of ::LoadIcon(...). With ::LoadImage(...) you can specified the desired width and height of the icon to load. You can find further information here:
HICON hNewIcon = LoadIcon(...);
::SetClassLong(m_hYourWnd, GCL_HICON, (LONG)hNewIcon);
Since you are using MFC you could also use:

AfxGetMainWnd()->SetIcon( ::LoadIcon(...), FALSE );
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

mahtieubraultAuthor Commented:
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);
mahtieubraultAuthor Commented:
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!
mahtieubraultAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.