• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 405
  • Last Modified:

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()?
0
mahtieubrault
Asked:
mahtieubrault
  • 3
  • 2
  • 2
1 Solution
 
BeyondWuCommented:
HICON hNewIcon = LoadIcon(...);
::SetClassLong(m_hYourWnd, GCL_HICON, (LONG)hNewIcon);
0
 
fl0ydCommented:
Since you are using MFC you could also use:

AfxGetMainWnd()->SetIcon( ::LoadIcon(...), FALSE );
0
 
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
BeyondWuCommented:
Make another call,
::SetClassLong(m_hYourWnd, GCL_HICONSM, (LONG)hNewIcon);
0
 
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!
0
 
fl0ydCommented:
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:
http://search.microsoft.com/gomsuri.asp?n=4&c=rp_Results&siteid=us/dev&target=http://msdn.microsoft.com/library/en-us/winui/winui/windowsuserinterface/resources/introductiontoresources/resourcereference/resourcefunctions/loadimage.asp
0
 
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.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now