Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Visual C++ and MFC Fundamentals Chapter 12: Dialog-Based Windows


WndCls.hInstance = AfxGetInstanceHandle();
}

In Lesson 3, we saw that an icon can be used to represent an application in My Computer
or Windows Explorer. To assign this small picture to your application, you can either use
an existing icon or design your own. To make your programming a little faster, Microsoft
Windows installs a few icons. The icon is assigned to the hIcon member variable using
the LoadIcon() function. For a Win32 application, the syntax of this function is:

HICON LoadIcon(HINSTANCE hInstance, LPCTSTR lpIconName);

The hInstance argument is a handle to the file in which the icon was created. This file is
usually stored in a library (DLL) of an executable program. If the icon was created as part
of your application, you can use the hInstance of your application. If your are using one
of the icons below, set this argument to NULL.

The lpIconName is the name of the icon to be loaded. This name is added to the resource
file when you create the icon resource. It is added automatically if you add the icon as
part of your resources; otherwise you can add it manually when creating your resource
script. Normally, if you had created and designed an icon and gave it an identifier, you
can pass it using the MAKEINTRESOURCE macro.

To make your programming a little faster, Microsoft Windows installs a few icons you
can use for your application. These icons have identification names that you can pass to
the LoadIcon() function as the lpIconName argument. The icons are:

ID Picture

IDI_APPLICATION (^)
IDI_INFORMATION (^)
IDI_ASTERISK (^)
IDI_QUESTION (^)
IDI_WARNING
IDI_EXCLAMATION (^)
IDI_HAND (^)
IDI_ERROR (^)
If you designed your own icon (you should make sure you design a 32x32 and a 16x16
versions, even for convenience), to use it, specify the hInstance argument of the
LoadIcon() function to the instance of your application. Then use the
MAKEINTRESOURCE macro to convert its identifier to a null-terminated string. This
can be done as follows:
WndCls.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_STAPLE));
If you are creating an MFC application, to use a standard icon, you can call the
CWinApp::LoadIcon() method. It is provided in two versions as follows:
HICON LoadIcon(LPCTSTR lpszResourceName) const;
HICON LoadIcon(UINT nIDResource) const;
The icon can be specified by its name, which would be a null-terminated string passed as
lpszResourceName. If you had designed your icon and gave it an ID, you can pass this
identifier to the LoadIcon() method.

Free download pdf