Example: Displaying the Hourglass on a Lengthy Operation

Whenever your application begins a lengthy operation, such as reading or writing a large block of data to a disk file, you should change the shape of the cursor to the hourglass. This lets users know that a lengthy operation is in progress and that they should wait before attempting to continue their work. Once the operation is complete, your application should restore the cursor to its previous shape.

To change the cursor to an hourglass, use the following statements:

1 HCURSOR hSaveCursor;

HCURSOR hHourGlass;

.

.

.

hHourGlass = LoadCursor(NULL, IDC_WAIT);

.

.

.

2 SetCapture(hWnd);

3 hSaveCursor = SetCursor(hHourGlass);

/* Lengthy operation */

4 SetCursor(hSaveCursor);

5 ReleaseCapture();

.

.

.

In this example:

1 The application defines the variables that will be used to store the cursor handles. Both variables are type HCURSOR.
2 The application first captures the mouse input, using the SetCapture function. This keeps the user from attempting to use the mouse to carry out work in another application while the lengthy operation is in progress. When the mouse input is captured, Windows directs all mouse input messages to the specified window, regardless of whether the mouse is in that window. The application can then process the messages as appropriate.
3 The application then changes the cursor shape using the SetCursor function. SetCursor returns a handle to the previous cursor shape, so that the shape can be restored later. The application saves this handle in the variable hSaveCursor.
4 After the lengthy operation is complete, the application restores the previous cursor shape.
5 The ReleaseCapture function releases the mouse input.