CWaitCursor クラス

時間がかかる処理を実行している最中に、通常は砂時計として表示される待機カーソルを表示する 1 つの方法を提供します。

構文

class CWaitCursor

メンバー

パブリック コンストラクター

名前 説明
CWaitCursor::CWaitCursor オブジェクトを CWaitCursor 構築し、待機カーソルを表示します。

パブリック メソッド

名前 説明
CWaitCursor::Restore 変更後に待機カーソルを復元します。

解説

CWaitCursor には基底クラスはありません。

Windows プログラミングの優れたプラクティスでは、かなりの時間がかかる操作を実行するたびに待機カーソルを表示する必要があります。

待機カーソルを表示するには、長い操作を CWaitCursor 実行するコードの前に変数を定義するだけです。 オブジェクトのコンストラクターにより、待機カーソルが自動的に表示されます。

オブジェクトがスコープ外になると (オブジェクトが宣言されているブロックの末尾) CWaitCursor 、そのデストラクターはカーソルを前のカーソルに設定します。 つまり、オブジェクトは必要なクリーンを自動的に実行します。

Note

コンストラクターとデストラクターの動作により、 CWaitCursor オブジェクトは常にローカル変数として宣言されます。グローバル変数として宣言されることも、割り当てもされません new

メッセージ ボックスやダイアログ ボックスの表示など、カーソルが変更される可能性がある操作を実行する場合は、Restore メンバー関数を呼び出して待機カーソルを復元します。 待機カーソルが現在表示されている場合でも呼び出 Restore してもかまいません。

待機カーソルを表示するもう 1 つの方法は、CCmdTarget::BeginWaitCursorCCmdTarget::EndWaitCursor、およびおそらく CCmdTarget::RestoreWaitCursor の組み合わせを使用することです。 ただし、 CWaitCursor 時間の長い操作が完了したら、カーソルを前のカーソルに設定する必要がないため、使いやすくなります。

Note

MFC は、CWinApp::D oWaitCursor 仮想関数を使用してカーソルを設定および復元します。 この関数をオーバーライドして、カスタム動作を提供できます。

継承階層

CWaitCursor

必要条件

ヘッダー: 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;
}

CWaitCursor::CWaitCursor

待機カーソルを表示するには、長い操作を実行する CWaitCursor コードの前にオブジェクトを宣言するだけです。

CWaitCursor();

解説

コンストラクターによって、待機カーソルが自動的に表示されます。

オブジェクトがスコープ外になると (オブジェクトが宣言されているブロックの末尾) CWaitCursor 、そのデストラクターはカーソルを前のカーソルに設定します。 つまり、オブジェクトは必要なクリーンを自動的に実行します。

デストラクターがブロックの末尾 (関数の末尾の前にある可能性があります) で呼び出され、関数の一部でのみ待機カーソルがアクティブになるという事実を利用できます。 この手法を次の 2 番目の例に示します。

Note

コンストラクターとデストラクターの動作により、 CWaitCursor オブジェクトは常にローカル変数として宣言されます。グローバル変数として宣言されることも、割り当てもされません new

// The following example illustrates the most common case
// of displaying the wait cursor during some lengthy
// processing.
void LengthyFunction()
{
   // perhaps you display a dialog box before displaying a
   // wait cursor

   CWaitCursor wait; // display wait cursor

   // do some lengthy processing
   Sleep(1000);

} // destructor automatically removes the wait cursor

// This example shows using a CWaitCursor object inside a block
// so the wait cursor is displayed only while the program is
// performing a lengthy operation.
void ConditionalFunction()
{
   if (SomeCondition)
   {
      CWaitCursor wait; // display wait cursor in this block only

      // do some lengthy processing
      Sleep(1000);

   } // at this point, the destructor removes the wait cursor
   else
   {
      // no wait cursor--only quick processing
   }
}

CWaitCursor::Restore

待機カーソルを復元するには、メッセージ ボックスやダイアログ ボックスの表示などの操作を実行した後にこの関数を呼び出します。これにより、待機カーソルが別のカーソルに変更される可能性があります。

void Restore();

解説

待機カーソルが現在表示されている場合でも呼び出 Restore しても問題ありません。

オブジェクトが宣言されている関数以外の関数で待機カーソルを復元する必要があるCWaitCursor場合は、CCmdTarget::RestoreWaitCursor を呼び出すことができます。

// This example illustrates performing an operation
// which changes the wait cursor. You should call
// CWaitCursor::Restore to restore the wait
// cursor after an operation which changes the cursor.
void AnotherLengthyFunction()
{
   CWaitCursor wait; // display wait cursor

   // do some lengthy processing
   Sleep(1000);

   // The dialog box will normally change the cursor to
   // the standard arrow cursor.
   CFileDialog dlg(TRUE);
   dlg.DoModal();

   // It is necessary to call Restore here in order
   // to change the cursor back to the wait cursor.
   wait.Restore();

   // do some more lengthy processing
   Sleep(1000);

   // destructor automatically removes the wait cursor
}

// If the wait cursor is changed by a function called by
// the function which created the wait cursor, you
// can call CCmdTarget::RestoreWaitCursor to restore
// the wait cursor.
void CalledFunction()
{
   CFileDialog dlg(TRUE);
   dlg.DoModal();

   // Since CWinApp is derived from CCmdTarget, we can use a
   // pointer to our application object to make the call to
   // CCmdTarget::RestoreWaitCursor.
   AfxGetApp()->RestoreWaitCursor();

   // Yet more lengthy processing...
   Sleep(1000);
}

関連項目

階層図
CCmdTarget::BeginWaitCursor
CCmdTarget::EndWaitCursor
CCmdTarget::RestoreWaitCursor
CWinApp::D oWaitCursor
Visual C++ を使用して MFC のウィンドウのマウス ポインターを変更する