1.BeginWaitCursor()是CCmdTarget类的 函数
2.函数EndWaitCursor可以恢复此前的光标。
沙漏光标
沙漏光标为 Windows 操作系统本身默认支持之特性,微软已经把对沙漏光标的支持封装在 MFC 框架当中,开发人员可以不需要为此而在程序中引入 Cursor 资源。在 MFC 程序中,可以使用以下三种方式来操作沙漏光标:
(1)CWinApp::DoWaitCursor();
(2)CCmdTarget::BeginWaitCursor(),CCmdTarget::EndWaitCursor()和CCmdTarget::RestoreWaitCursor();
(3)CWaitCursor 封装类。
通过 Debug 跟踪可知,(2)和(3)的封装最终还是通过调用(1)来实现的。它们存在以下的对应关系:
打开沙漏光标
CWinApp::DoWaitCursor(1)
CCmdTarget::BeginWaitCursor()
CWaitCursor 构造函数
关闭沙漏光标
CWinApp::DoWaitCursor(-1)
CCmdTarget::EndWaitCursor()
CWaitCursor 析构函数
刷新沙漏光标
CWinApp::DoWaitCursor(0)
CCmdTarget::RestoreWaitCursor()
CWaitCursor::Restore()
下面列举实例场景以作说明:
在开始执行长操作之前打开沙漏光标,在长操作操作结束之后关闭沙漏光标。长操作在这里是指那些相对需要比较长的时间才能执行完毕的操作,比如说大文件的复制粘贴。
void CMyDialog::DoSomeLengthyOperation()
{
BeginWaitCursor(); // or AfxGetApp()->DoWaitCursor(1)
//... 执行长操作
EndWaitCursor(); // or AfxGetApp()->DoWaitCursor(-1)
}
如果在长操作的执行过程中,通过 DoModal() 方式显示了其他窗口的话,需要在 DoModal() 执行完毕之后进行沙漏光标的操作,以下代码修改自 MSDN :
void CMyDialog::DoSomeLengthyOperation( )
{
CWaitCursor wait; // display wait cursor
// do some lengthy processing
// The dialog box will normally change the cursor to
// the standard arrow cursor.
CSomeDialog dlg;
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
// destructor automatically removes the wait cursor
}