设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
楼主: faunus
打印 上一主题 下一主题

【示例】WORD下实现多窗口TASKPAN

[复制链接]
11#
 楼主| 发表于 2014-3-22 16:22:04 | 只看该作者
  新文档在计时器运行时启动,此时这个方法还清除了ribbon菜单中的Pause复选框。这是通过一个实用方法SetPauseStatus()实现的,该方法在ribbon中定义:
  1. // Set checkbox
  2. Globals.Ribbons.TimerRibbon.SetPauseStatus(false);
  3. }
复制代码



12#
 楼主| 发表于 2014-3-22 16:22:57 | 只看该作者
  关闭文档之前,调用eventInterface_DocumentBeforeClose()事件处理程序。这个方法冻结了文档的计时器,更新了总编辑时间,清除了Document引用,删除了文档窗口中的任务面板(使用稍后介绍的RemoveTaskPaneFromWindow()方法),之后关闭窗口。
  1. private void eventInterface_DocumentBeforeClose(Word.Document Doc,
  2. ref bool Cancel)
  3. {
  4. // Freeze timer
  5. documentEditTimes[Doc.Name].EditTime += DateTime.Now
  6. - documentEditTimes[Doc.Name].LastActive;
  7. documentEditTimes[Doc.Name].IsActive = false;
  8. documentEditTimes[Doc.Name].Document = null;
  9. // Remove task pane
  10. RemoveTaskPaneFromWindow(Doc.ActiveWindow);
  11. }
复制代码



13#
 楼主| 发表于 2014-3-22 16:23:26 | 只看该作者
  打开文档时,调用eventInterface_DocumentOpen()方法。该方法完成了许多工作,因为在监控文档之前,这个方法必须查看计时器的名称,确定文档是否已有计时器:
  1. private void eventInterface_DocumentOpen(Word.Document Doc)
  2. {
  3. if (documentEditTimes.ContainsKey(Doc.Name))
  4. {
  5. // Monitor old doc
  6. documentEditTimes[Doc.Name].LastActive = DateTime.Now;
  7. documentEditTimes[Doc.Name].IsActive = true;
  8. documentEditTimes[Doc.Name].Document = Doc;
  9. AddTaskPaneToWindow(Doc.ActiveWindow);
  10. }
复制代码



14#
 楼主| 发表于 2014-3-22 16:23:47 | 只看该作者
  如果还没有监控文档,就为文档配置一个新监控器:
  1. else
  2. {
  3. // Monitor new doc
  4. MonitorDocument(Doc);
  5. AddTaskPaneToWindow(Doc.ActiveWindow);
  6. }
  7. }
复制代码
15#
 楼主| 发表于 2014-3-22 16:24:10 | 只看该作者
  RemoveTaskPaneFromWindow()方法用于从窗口中删除任务面板。其代码首先检查特定的窗口中是否有任务面板:
  1. private void RemoveTaskPaneFromWindow(Word.Window Wn)
  2. {
  3. // Check for task pane in window
  4. Tools.CustomTaskPane docPane = null;
  5. foreach (Tools.CustomTaskPane pane in timerDisplayPanes)
  6. {
  7. if (pane.Window == Wn)
  8. {
  9. docPane = pane;
  10. break;
  11. }
  12. }
复制代码
16#
 楼主| 发表于 2014-3-22 16:24:29 | 只看该作者
  如果找到了任务面板,就调用CustomTaskPanes.Remove()方法删除它。还要从任务面板引用的本地集合中删除它。
  1. // Remove document task pane
  2. if (docPane != null)
  3. {
  4. this.CustomTaskPanes.Remove(docPane);
  5. timerDisplayPanes.Remove(docPane);
  6. }
  7. }
复制代码
17#
 楼主| 发表于 2014-3-22 16:24:48 | 只看该作者
  这个类中的最后一个事件处理程序是eventInterface_WindowActivate(),在激活窗口时调用它。这个方法获得活动文档的计时器,选中ribbon菜单中的复选框,以更新文档的复选框:
  1. private void eventInterface_WindowActivate(Word.Document Doc,
  2. Word.Window Wn)
  3. {
  4. // Ensure pause checkbox in ribbon is accurate, start by getting timer
  5. DocumentTimer documentTimer =
  6. documentEditTimes[this.Application.ActiveDocument.Name];
  7. // Set checkbox
  8. Globals.Ribbons.TimerRibbon.SetPauseStatus(!documentTimer.IsActive);
  9. }
复制代码
18#
 楼主| 发表于 2014-3-22 16:25:05 | 只看该作者
  ThisAddIn的代码还包含两个实用方法。第一个方法ToggleTaskPaneDisplay()用于设置CustomTaskPanes.Visible属性,为当前活动的文档显示或隐藏任务面板。
  1. internal void ToggleTaskPaneDisplay()
  2. {
  3. // Ensure window has task window
  4. AddTaskPaneToWindow(this.Application.ActiveDocument.ActiveWindow);
  5. // toggle document task pane
  6. Tools.CustomTaskPane docPane = null;
  7. foreach (Tools.CustomTaskPane pane in timerDisplayPanes)
  8. {
  9. if (pane.Window == this.Application.ActiveDocument.ActiveWindow)
  10. {
  11. docPane = pane;
  12. break;
  13. }
  14. }
  15. docPane.Visible = !docPane.Visible;
  16. }
复制代码
19#
 楼主| 发表于 2014-3-22 16:25:37 | 只看该作者
  上述代码中的ToggleTaskPaneDisplay()方法由ribbon控件上的事件处理程序调用,如后面所述。
  最后,该类有另一个从ribbon菜单中调用的方法,它允许ribbon控件暂停或恢复文档的计时器:
  1. internal void PauseOrResumeTimer(bool pause)
  2. {
  3. // Get timer
  4. DocumentTimer documentTimer =
  5. documentEditTimes[this.Application.ActiveDocument.Name];
  6. if (pause & & documentTimer.IsActive)
  7. {
  8. // Freeze timer
  9. documentTimer.EditTime += DateTime.Now - documentTimer.LastActive;
  10. documentTimer.IsActive = false;
  11. }
  12. else if (!pause & & !documentTimer.IsActive)
  13. {
  14. // Resume timer
  15. documentTimer.IsActive = true;
  16. documentTimer.LastActive = DateTime.Now;
  17. }
  18. }
  19. }
复制代码
20#
 楼主| 发表于 2014-3-22 16:26:11 | 只看该作者
  这个类定义中的其他代码是Shutdown的空事件处理程序以及VSTO为关联Startup和Shutdown事件处理程序而生成的代码。
  接着布置项目中的ribbon,即TimerRibbon,如图所示。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-3 21:23 , Processed in 0.086634 second(s), 32 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表