MFC:: BeginWaitCursor()和EndWaitCursor()函数

来源:互联网 发布:淘宝供应链管理 编辑:程序博客网 时间:2024/04/27 18:28

1.BeginWaitCursor()是CCmdTarget类的函数

  函数原型:void   BeginWaitCursor();  
  功能简介:本函数用于显示沙漏光标。调用本函数显示沙漏光标,告诉用户系统正在运行,繁忙。
  注意:在不是处理单个消息时,BeginWaitCursor()可能不像其它函数那样有效,例如:OnSetCursor()的处理也能改变光标形状。  

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
}
  


0 0
原创粉丝点击