Displaying Your Own Icons

Windows displays a class icon when the application is minimized and removes it when the application is maximized. All the application does is specify it as the class icon. This meets the needs of most applications, since most applications do not need to display additional information to the user when the application is minimized.

However, sometimes your application may need to display its icon itself, instead of letting Windows display a prespecified class icon. This is particularly useful when you want your application's icon to be dynamic, like the icon in the Clock application. (The Clock application continues to show the time even when it has been minimized.) Windows lets applications paint within the client area of an iconic window, so that they can paint their own icons.

If you want your application to display its own icon:

1.In the window class structure, set the class icon to NULL before registering the window class. Use the following statement:

wc.hIcon = NULL;

This step is required because it signals Windows to continue sending WM_PAINT messages, as necessary, to the window function even though the window has been minimized.

2.Add a WM_PAINT case to your window function that draws within the icon's client area if the window receives a WM_PAINT message when the window is iconic (minimized). Use the following statements:

PAINTSTRUCT ps;

HDC hDC;

.

.

.

case WM_PAINT:

hDC = BeginPaint(hWnd, &ps);

if(IsIconic(hWnd))

{

/* Output functions for iconic state */

}

else

{

/* Output functions for non-iconic state */

}

EndPaint(hWnd, &ps);

break;

Applications need to determine whether the window is iconic, since what they paint in the icon may be different from what they paint in the open window. The IsIconic function returns TRUE if the window is iconic.

The BeginPaint function returns a handle to the display context of the icon's client area. BeginPaint takes the window handle, hWnd, and a long pointer to the paint structure, ps. BeginPaint fills the paint structure with information about the area to be painted. As with any painting operation, after each call to BeginPaint, the EndPaint function is required. EndPaint releases any resources that BeginPaint retrieved and signals the end of the application's repainting of the client area.

You can retrieve the size of the icon's client area by using the rcPaint field of the paint structure. For example, to draw an ellipse that fills the icon, you can use the following statement:

Ellipse(hDC, ps.rcPaint.left, ps.rcPaint.top,

ps.rcPaint.right, ps.rcPaint.bottom);

You can use any GDI output functions to draw the icon, including the TextOut function. The only limitation is the size of the icon, which varies from display to display, so make sure that your painting does not depend on a specific icon size.