Export (0) Print
Expand All

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.

class CWaitCursor

CWaitCursor does not have a base class.

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.

NoteNote

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.

NoteNote

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 SomeLengthyProcess()
{
   CWaitCursor wait;
   //Do the lengthy processing.
   Sleep(1000);

   AfxMessageBox(_T("Some result")); //This changes the cursor.
   wait.Restore(); //Restore the Wait cursor.
   //Continue Processing.
   Sleep(1000);

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

}


Community Additions

ADD
Show:
© 2014 Microsoft