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 requirements of most applications, since most applications typically do not display additional information to the user when the application is minimized.
However, sometimes you may want your application 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 Microsoft Windows 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 a minimized window, so that they can paint their own icons.
If you want your application to display its own icon, follow these steps:
1.In the window class structure wc, 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 procedure even though the window has been minimized.
2.Add a WM_PAINT case to your window procedure that draws within the icon's client area if the window is to receive a WM_PAINT message when the window is minimized. Use the following statements:
PAINTSTRUCT
ps; HDC hDC; . . . case WM_PAINT: hDC = BeginPaint(hWnd, &ps); if (IsIconic(hWnd)) { /* Output functions for minimized state */ } else { /* Output functions for nonminimized state */ } EndPaint(hWnd, &ps); break;
An application must determine whether the window is minimized, since what it paints in the icon may be different from what it paints in the open window. The IsIconic function returns a nonzero value if the window is minimized.
The BeginPaint function returns a handle of 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, each call to BeginPaint requires a corresponding call to the EndPaint function. 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 calling the GetClientRect function. For example, to draw an ellipse that fills the icon, you can use the following statement:
GetClientRect
(hWnd, &rc); Ellipse(hDC, rc.left, rc.top, rc.right, rc.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 screen to screen, so make sure that your painting does not depend on a specific icon size.