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

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
}
  


  • 5
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值