how to add an icon in the title bar of an SDI App developed using MFC?

3.6k views Asked by At

I have to make changes to an existing Application developed using MFC. this application uses the SDI template with Document\View architecture. I need to place an icon on the window that is being shown at the execution of the Application. At present it does not have any icon to show (some MFC applications by default shows MFC logo). can somebody help me out? I have googled alot but didn't get any success. Also I want to know why do we use IDR_MAINFRAME (same name) for all the resources in the resource.h file. and i have observed its value is fixed at 128.is there any specific reason for this? kindly suggest some good links or books on MFC if u have come across previously?

3

There are 3 answers

1
snowdude On BEST ANSWER

I'd recommend you create a new MFC App just to see what the defaults are. You should see that in your .RC file there is a line that looks something like this:

IDR_MAINFRAME   ICON       "res\\app.ico"

If it's not there you can add it. Looking in the MFC file winfrm.cpp you can see MFC trying to load the icon in CFrameWnd::GetIconWndClass()

HICON hIcon = ::LoadIconW(hInst, ATL_MAKEINTRESOURCEW(nIDResource));

Since resources are identified by their type AND id you can use the same id for multiple resources of different types. This can be very useful when the MFC frame code needs to load a toolbar, menu, and icon etc. without having the developer needing to specify a different id for each item.

The best MFC book I've seen on MFC is 'Professional MFC' by Mike Blaszczak. It doesn't have some of the new 'MFC Feature Pack' additions but it covers the old stuff very well. I also recommend that you download a source searching tool like Agent Ransack for searching through MFC source code.

0
l33t On
  1. Yes, the value must be 128. It is reserved for MFC SDI/MDI.
  2. IDR_ means "For multiple resource types (primarily used for menus, accelerators, and ribbons).". Usually, these resources are MFC specific.
  3. The icon is visible by default. Make sure you do not call SetIcon for your main window!

More about icons

As of Vista and later, icons may contain PNG images. I believe these icons might cause issues in XP or earlier. Try creating the icon using Visual Studio and see if the icon comes back. I'm assuming you are running Visual Studio 2008 or later. It might also be a good idea to update the Windows SDK in order to get this new PNG feature (rc.exe was updated to handle these new icons).

1
diwatu On

This is a known problem on Visual studio late versions. I tried everything I could, it still doesn't work. Finally I found a solution from somewhere else. You have to force the application to use the icon by code:

BOOL CMyApp::InitInstance()
{
    ...........
    CWinAppEx::InitInstance();
    ...........

    HICON hIcon = LoadIcon(IDR_MAINFRAME);
    HICON hPrev = pMainFrame->SetIcon(hIcon,FALSE);
    if (hPrev != NULL && hPrev != hIcon) 
        DestroyIcon(hPrev);

    return TRUE;
}