Search code examples
visual-c++mfcsdi

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


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?


Solution

  • 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.