設(shè)為首頁收藏本站Access中國

Office中國論壇/Access中國論壇

 找回密碼
 注冊(cè)

QQ登錄

只需一步,快速開始

1234下一頁
返回列表 發(fā)新帖
查看: 10321|回復(fù): 33
打印 上一主題 下一主題

【示例】WORD下實(shí)現(xiàn)多窗口TASKPAN

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
1#
發(fā)表于 2014-3-22 16:08:57 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
  示例代碼包含一個(gè)應(yīng)用程序WordDocEditTimer,它維護(hù)著Word文檔的一個(gè)編輯次數(shù)列表。本節(jié)將詳細(xì)解釋這個(gè)應(yīng)用程序的代碼,因?yàn)樵搼?yīng)用程序演示了前面介紹的所有內(nèi)容,還包含一些有益的提示。
  這個(gè)應(yīng)用程序的一般操作是只要?jiǎng)?chuàng)建或加載了文檔,就啟動(dòng)一個(gè)鏈接到文檔名稱上的計(jì)時(shí)器。如果關(guān)閉文檔,該文檔的計(jì)時(shí)器就暫停。如果打開了以前計(jì)時(shí)的文檔,計(jì)時(shí)器就恢復(fù)。另外,如果使用Save As把文檔保存為另一個(gè)文件名,計(jì)時(shí)器就更新為使用新文件名。
這個(gè)應(yīng)用程序是一個(gè)Word應(yīng)用程序級(jí)的插件,使用一個(gè)定制任務(wù)面板和一個(gè)ribbon菜單。
ribbon菜單包含一個(gè)按鈕和一個(gè)復(fù)選框,按鈕用于開關(guān)任務(wù)面板,復(fù)選框用于暫停當(dāng)前活動(dòng)的文檔的計(jì)時(shí)器。包含這些控件的組添加到Home ribbon選項(xiàng)卡的最后。任務(wù)面板顯示一組活動(dòng)的計(jì)時(shí)器。

效果如下:



本帖子中包含更多資源

您需要 登錄 才可以下載或查看,沒有帳號(hào)?注冊(cè)

x
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享分享 分享淘帖 訂閱訂閱
推薦
 樓主| 發(fā)表于 2014-3-22 16:17:36 | 只看該作者
  因此,每個(gè)計(jì)時(shí)器都可以通過文檔引用或文檔名來定位。這是必要的,因?yàn)槲臋n引用可以跟蹤文檔名的變化(這里沒有可用于監(jiān)控文檔名變化的事件),文檔名允許跟蹤關(guān)閉、再次打開的文檔。
  ThisAddIn類還維護(hù)一個(gè)CustomTaskPane對(duì)象列表(如前所述,Word中的每個(gè)窗口都需要一個(gè)CustomTaskPane對(duì)象):
  1. private List < Tools.CustomTaskPane > timerDisplayPanes;
復(fù)制代碼
  插件啟動(dòng)時(shí),ThisAddIn_Startup()方法執(zhí)行了幾個(gè)任務(wù)。首先它初始化兩個(gè)集合:
  1. private void ThisAddIn_Startup(object sender, System.EventArgs e)
  2. {
  3. // Initialize timers and display panels
  4. documentEditTimes = new Dictionary < string, DocumentTimer > ();
  5. timerDisplayPanes = new
  6. List < Microsoft.Office.Tools.CustomTaskPane > ();
復(fù)制代碼





2#
 樓主| 發(fā)表于 2014-3-22 16:10:32 | 只看該作者
計(jì)時(shí)器通過DocumentTimer類來維護(hù):
  1. public class DocumentTimer
  2. {
  3. public Word.Document Document { get; set; }
  4. public DateTime LastActive { get; set; }
  5. public bool IsActive { get; set; }
  6. public TimeSpan EditTime { get; set; }
  7. }
復(fù)制代碼
  這段代碼保存了對(duì)Microsoft.Office.Interop.Word. Document對(duì)象的一個(gè)引用、總編輯時(shí)間、計(jì)時(shí)器是否激活,以及它上一次激活的時(shí)間。ThisAddIn類維護(hù)這些對(duì)象的一個(gè)集合,這些對(duì)象與文檔名關(guān)聯(lián)起來:
  1. public partial class ThisAddIn
  2. {
  3. private Dictionary < string, DocumentTimer > documentEditTimes;
復(fù)制代碼



4#
 樓主| 發(fā)表于 2014-3-22 16:18:03 | 只看該作者
  接著通過ApplicationEvents4_Event接口添加幾個(gè)事件處理程序:
  1. // Add event handlers
  2. Word.ApplicationEvents4_Event eventInterface = this.Application;
  3. eventInterface.DocumentOpen += new Microsoft.Office.Interop.Word
  4. .ApplicationEvents4_DocumentOpenEventHandler(
  5. eventInterface_DocumentOpen);
  6. eventInterface.NewDocument += new Microsoft.Office.Interop.Word
  7. .ApplicationEvents4_NewDocumentEventHandler(
  8. eventInterface_NewDocument);
  9. eventInterface.DocumentBeforeClose += new Microsoft.Office.Interop.Word
  10. .ApplicationEvents4_DocumentBeforeCloseEventHandler(
  11. eventInterface_DocumentBeforeClose);
  12. eventInterface.WindowActivate += new Microsoft.Office.Interop.Word
  13. .ApplicationEvents4_WindowActivateEventHandler(
  14. eventInterface_WindowActivate);
復(fù)制代碼



5#
 樓主| 發(fā)表于 2014-3-22 16:18:31 | 只看該作者
  這些事件處理程序用于監(jiān)控文檔的打開、創(chuàng)建和關(guān)閉,并確保ribbon上的Pause復(fù)選框保持最新狀態(tài)。后一個(gè)功能是使用WindowsActivate事件跟蹤窗口的激活狀態(tài)來實(shí)現(xiàn)的。
  在這個(gè)事件處理程序中,最后一個(gè)任務(wù)是開始監(jiān)控當(dāng)前文檔,把定制的任務(wù)面板添加到包含文檔的窗口中:
  1. // Start monitoring active document
  2. MonitorDocument(this.Application.ActiveDocument);
  3. AddTaskPaneToWindow(this.Application.ActiveDocument.ActiveWindow);
  4. }
復(fù)制代碼




6#
 樓主| 發(fā)表于 2014-3-22 16:19:16 | 只看該作者
  MonitorDocument()實(shí)用方法為文檔添加一個(gè)計(jì)時(shí)器:
  1. internal void MonitorDocument(Word.Document Doc)
  2. {
  3. // Monitor doc
  4. documentEditTimes.Add(Doc.Name, new DocumentTimer
  5. {
  6. Document = Doc,
  7. EditTime = new TimeSpan(0),
  8. IsActive = true,
  9. LastActive = DateTime.Now
  10. });
  11. }
復(fù)制代碼



7#
 樓主| 發(fā)表于 2014-3-22 16:19:56 | 只看該作者
  這個(gè)方法僅為文檔創(chuàng)建了一個(gè)新的DocumentTimer對(duì)象。DocumentTimer引用文檔,其編輯次數(shù)是0,且是在當(dāng)前時(shí)間激活的。接著把這個(gè)計(jì)時(shí)器添加到documentEditTimes集合中,并關(guān)聯(lián)到文檔名中。
  AddTaskPaneToWindow()方法把定制任務(wù)面板添加到窗口中。這個(gè)方法首先檢查已有的任務(wù)面板,確保窗口中還沒有任務(wù)面板。Word中的另一個(gè)古怪的特性是如果在加載應(yīng)用程序后,立即打開一個(gè)舊文檔,默認(rèn)的Document1文檔就會(huì)消失,且不觸發(fā)關(guān)閉事件。在訪問包含任務(wù)面板的文檔窗口時(shí),這可能導(dǎo)致異常,所以該方法還檢查表示是否出現(xiàn)該異常的ArgumentNullException:
  1. private void AddTaskPaneToWindow(Word.Window Wn)
  2. {
  3. // Check for task pane in window
  4. Tools.CustomTaskPane docPane = null;
  5. Tools.CustomTaskPane paneToRemove = null;
  6. foreach (Tools.CustomTaskPane pane in timerDisplayPanes)
  7. {
  8. try
  9. {
  10. if (pane.Window == Wn)
  11. {
  12. docPane = pane;
  13. break;
  14. }
  15. }
  16. catch (ArgumentNullException)
  17. {
  18. // pane.Window is null, so document1 has been unloaded.
  19. paneToRemove = pane;
  20. }
  21. }
復(fù)制代碼




8#
 樓主| 發(fā)表于 2014-3-22 16:20:42 | 只看該作者
  如果拋出了一個(gè)異常,就從集合中刪除錯(cuò)誤的任務(wù)面板:
  1. // Remove pane if necessary
  2. timerDisplayPanes.Remove(paneToRemove);
復(fù)制代碼

  如果窗口中沒有任務(wù)面板,這個(gè)方法就添加一個(gè):
  1. // Add task pane to doc
  2. if (docPane == null)
  3. {
  4. Tools.CustomTaskPane pane = this.CustomTaskPanes.Add(
  5. new TimerDisplayPane(documentEditTimes),
  6. "Document Edit Timer",
  7. Wn);
  8. timerDisplayPanes.Add(pane);
  9. pane.VisibleChanged +=
  10. new EventHandler(timerDisplayPane_VisibleChanged);
  11. }
  12. }
復(fù)制代碼



9#
 樓主| 發(fā)表于 2014-3-22 16:21:16 | 只看該作者
  添加的任務(wù)面板是TimerDisplayPane類的一個(gè)實(shí)例。稍后介紹這個(gè)類。它添加時(shí)使用的名稱是Document Edit Timer。另外,在調(diào)用CustomTaskPanes.Add()方法后,還為得到的CustomTaskPane的VisibleChanged事件添加了一個(gè)處理程序,這樣在第一次顯示任務(wù)面板時(shí),可以刷新顯示:
  1. private void timerDisplayPane_VisibleChanged(object sender, EventArgs e)
  2. {
  3. // Get task pane and toggle visibility
  4. Tools.CustomTaskPane taskPane = (Tools.CustomTaskPane)sender;
  5. if (taskPane.Visible)
  6. {
  7. TimerDisplayPane timerControl = (TimerDisplayPane)taskPane.Control;
  8. timerControl.RefreshDisplay();
  9. }
  10. }
復(fù)制代碼



10#
 樓主| 發(fā)表于 2014-3-22 16:21:42 | 只看該作者
  TimerDisplayPane類有一個(gè)RefreshDisplay()方法,它在上面的代碼中調(diào)用。這個(gè)方法刷新timerControl對(duì)象的顯示。
  接著的代碼確保監(jiān)控所有的文檔。首先創(chuàng)建新文檔時(shí),調(diào)用eventInterface_New- Document()事件處理程序,調(diào)用MonitorDocument()和前面介紹過的AddTaskPaneTo- Window()方法監(jiān)控文檔。
  1. private void eventInterface_NewDocument(Word.Document Doc)
  2. {
  3. // Monitor new doc
  4. MonitorDocument(Doc);
  5. AddTaskPaneToWindow(Doc.ActiveWindow);
復(fù)制代碼




您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則

QQ|站長(zhǎng)郵箱|小黑屋|手機(jī)版|Office中國/Access中國 ( 粵ICP備10043721號(hào)-1 )  

GMT+8, 2025-7-13 08:14 , Processed in 0.087894 second(s), 35 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復(fù) 返回頂部 返回列表