14.9 Simple Way to Change a Window Icon

The previous section described how to use AfxRegisterWndClass to change the icon that Windows uses to display a minimized window. If all you want to do is change the icon and leave the class style, cursor, and background brush unchanged, then you can define an icon resource with the proper ID in your application's resource file. The Microsoft Foundation window classes will automatically use that icon. This technique is simpler than using AfxRegisterWndClass because you do not have to write any code; defining the icon resource is sufficient.

By default, the Foundation window classes use an empty rectangle icon to represent a minimized window. The classes CFrameWnd and CMDIChildWnd look in the application's resource file for an icon with the ID number AFX_IDI_STD_FRAME. These classes will use that icon instead of the default if it exists.

·To change a window icon:

Design an icon with the Windows Icon Editor and include it in your application resource file with the following command taken from the sample program file HELLO.RC:

AFX_IDI_STD_FRAME ICON hello.ico

Within one application:

All window classes derived from CFrameWnd and CMDIChildWnd will use the AFX_IDI_STD_FRAME icon, or the default application icon if it is not defined.

All windows derived from CMDIFrameWnd use the icon with the ID number AFX_IDI_STD_MDIFRAME, or the default application icon if it is not defined.

If you have a program that uses more than one type of child window derived from CFrameWnd or CMDIChildWnd, such as an MDI program with more than one type of child window, the Microsoft Foundation Class Library's default mechanism for choosing custom minimized icons does not support different minimized icons for those child windows. To have a custom icon for different types of child windows in the same application, use AfxRegisterWndClass to create a Windows registration class for each child window class. For a description of how to use AfxRegisterWndClass, see the section “Using the AFXRegisterWnd Class Function” on page 324. Also, see the MDI sample program.