共用方式為


CWaitCursor 類別

可讓您以使用一行程式碼的方式,在執行長時間作業期間顯示等待游標,這通常顯示為沙漏。

語法

class CWaitCursor

成員

公用建構函式

名稱 描述
CWaitCursor::CWaitCursor CWaitCursor建構 物件並顯示等候游標。

公用方法

名稱 描述
CWaitCursor::Restore 在變更等候資料指標之後還原。

備註

CWaitCursor 沒有基類。

良好的 Windows 程式設計做法會要求您在執行需要明顯時間的作業時,顯示等候游標。

若要顯示等候資料指標,請在執行冗長作業的程式碼之前定義 CWaitCursor 變數。 物件的建構函式會自動顯示等候資料指標。

當物件超出範圍時(在宣告物件的區塊 CWaitCursor 結尾),其解構函式會將游標設定為上一個資料指標。 換句話說,物件會自動執行必要的清除。

注意

由於其建構函式和解構函式的運作方式,物件一律會宣告為區域變數, CWaitCursor 它們永遠不會宣告為全域變數,也不會使用 new 來配置。

如果您執行可能導致資料指標變更的作業,例如顯示訊息方塊或對話方塊,請呼叫 Restore 成員函式來還原等候資料指標。 即使目前顯示等候資料指標,還是可以呼叫 Restore

顯示等候游標的另一種方式是使用 CCmdTarget::BeginWaitCursor CCmdTarget::EndWaitCursor 的組合,以及 CCmdTarget::RestoreWaitCursor 的組合。 不過,較容易使用, CWaitCursor 因為當您完成冗長的作業時,不需要將游標設定為上一個資料指標。

注意

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 結尾),其解構函式會將游標設定為上一個資料指標。 換句話說,物件會自動執行必要的清除。

您可以利用在區塊結尾呼叫解構函式的事實(可能位於函式結尾之前),讓等候資料指標只在函式的一部分作用中。 下列第二個範例顯示這項技術。

注意

由於其建構函式和解構函式的運作方式, 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 中視窗的滑鼠指標