Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

CWaitCursor Class

class CWaitCursor


CWaitCursor does not have a base class.

The CWaitCursor class provides a one-line way to show a wait cursor, which is usually displayed as an hourglass, while you're doing a lengthy operation. Good Windows programming practices require that you display a wait cursor whenever you're performing an operation that takes a noticeable amount of time.

To display a wait cursor, just define a CWaitCursor variable before the code that performs the lengthy operation. The object's constructor automatically causes the wait cursor to be displayed.

When the object goes out of scope (at the end of the block in which the CWaitCursor object is declared), its destructor sets the cursor to the previous cursor. In other words, the object performs the necessary clean-up automatically.

Note   Because of how their constructors and destructors work, CWaitCursor objects are always declared as local variables — they're never declared as global variables nor are they allocated with new.

If you perform an operation which might cause the cursor to be changed, such as displaying a message box or dialog box, call the Restore member function to restore the wait cursor. It is okay to call Restore even when a wait cursor is currently displayed.

Another way to display a wait cursor is to use the combination of CCmdTarget::BeginWaitCursor, CCmdTarget::EndWaitCursor, and perhaps CCmdTarget::RestoreWaitCursor. However, CWaitCursor is easier to use because you don't need to set the cursor to the previous cursor when you're done with the lengthy operation.

Note   MFC sets and restores the cursor using the CWinApp::DoWaitCursor virtual function. You can override this function to provide custom behavior.


Header: afxwin.h


BOOL CMyWnd::SomeLengthyProcess()
   CWaitCursor wait;
   //Do the lengthProcessing.

   MessageBox("Some result");      //This changes the cursor.
   wait.Restore();         //Restore the Wait cursor.
   //Continue Processing.

   //The destructor changes the cursor back to Regular cursor.
   return TRUE;         


See Also

Class Members | Hierarchy Chart | CCmdTarget::BeginWaitCursor | CCmdTarget::EndWaitCursor | CCmdTarget::RestoreWaitCursor | CWinApp::DoWaitCursor

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.