vsto-demo/TestWordAddIn1/ThisAddIn.cs
2023-12-12 16:53:12 +08:00

216 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using CustomTaskPane = Microsoft.Office.Tools.CustomTaskPane;
using System.Windows.Forms;
namespace TestWordAddIn1
{
public partial class ThisAddIn
{
public OfficeType officeType = OfficeType.Word;
public string applicationStartupPath;
public bool proofreadPanelOpen;
private ProofreadPanel proofreadPanel;
private Dictionary<Document, CustomTaskPane> myTaskPanes;
private CustomTaskPane myCustomTaskPane;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
this.applicationStartupPath = System.Windows.Forms.Application.ProductName;
if (applicationStartupPath.Contains("WPS"))
{
this.officeType = OfficeType.WPS;
return;
}
var eventInfo = new ComAwareEventInfo(typeof(ApplicationEvents4_Event), "DocumentOpen");
var handler = new ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
// 监听文档相关操作
//eventInfo.AddEventHandler(this, handler);
//new ComAwareEventInfo(typeof(ApplicationEvents4_Event), "NewDocument").AddEventHandler(Application, new ApplicationEvents4_NewDocumentEventHandler(Application_NewDocument));
//new ComAwareEventInfo(typeof(ApplicationEvents4_Event), "WindowActivate").AddEventHandler(Application, new ApplicationEvents4_WindowActivateEventHandler(Application_WindowActivate));
//new ComAwareEventInfo(typeof(ApplicationEvents4_Event), "WindowDeactivate").AddEventHandler(Application, new ApplicationEvents4_WindowDeactivateEventHandler(Application_WindowDeactivate));
}
/// <summary>
/// 隐藏所有的面板
/// </summary>
void HideAllPane()
{
foreach (CustomTaskPane customTaskPane in CustomTaskPanes)
{
customTaskPane.Visible = false;
}
}
private void Application_NewDocument(Document doc)
{
Console.WriteLine("Application_NewDocument");
InitProofreadPanel();
HideAllPane();
}
private void Application_DocumentOpen(Document doc)
{
Console.WriteLine("Application_DocumentOpen");
InitProofreadPanel();
HideAllPane();
}
private void Application_DocumentChange()
{
Console.WriteLine("Application_DocumentChange");
ShowTaskPaneForActiveDocument();
}
private void ShowTaskPaneForActiveDocument()
{
// 获取当前文档
Document activeDocument = Application.ActiveDocument;
// 如果现有任务不包含文档则直接返回
if (!myTaskPanes.ContainsKey(activeDocument))
{
return;
}
// 显示当前文档panel隐藏其他文档panel
foreach (var item in myTaskPanes)
{
if (item.Key == activeDocument)
{
item.Value.Visible = true;
}
else
{
item.Value.Visible = false;
}
}
}
public void Application_WindowActivate_WPS(Document document, Window window)
{
Console.WriteLine("Application_WindowActivate_WPS");
if (myCustomTaskPane == null)
{
InitProofreadPanel();
}
}
public void Application_WindowActivate(Document document, Window window)
{
Console.WriteLine("Application_WindowActivate");
//bool flag = true;
//if (myTaskPanes != null)
//{
// foreach (KeyValuePair<Document,CustomTaskPane> customTaskPane in myTaskPanes)
// {
// if (customTaskPane.Key == Application.ActiveDocument)
// {
// flag = false;
// myCustomTaskPane = customTaskPane.Value;
// proofreadPanel = userControls[Application.ActiveDocument];
// myCustomTaskPane.Visible = customTaskPanesVisible[Application.ActiveDocument];
// break;
// }
// }
// if (flag)
// {
// NewCustomTaskPane();
// }
//}
//else
//{
// NewCustomTaskPane();
//}
//Globals.Ribbons.Ribbon.button_viewOn.Visible = !myCustomTaskPane.Visible;
//Globals.Ribbons.Ribbon.button_viewOff.Visible = myCustomTaskPane.Visible;
}
public void Application_WindowDeactivate(Document document, Window window)
{
Console.WriteLine("Application_WindowDeactivate");
//if (customTaskPanesVisible.ContainsKey(document))
//{
// customTaskPanesVisible[document] = myCustomTaskPane.Visible;
//}
//else
//{
// customTaskPanesVisible.Add(document, myCustomTaskPane.Visible);
//}
}
/// <summary>
/// 面板最小宽度
/// </summary>
private static readonly int MIN_WIDTH = 380;
void InitProofreadPanel()
{
if(this.myCustomTaskPane == null)
{
// 创建
proofreadPanel = new ProofreadPanel();
// 添加到panes
this.myCustomTaskPane = CustomTaskPanes.Add(proofreadPanel, AppConfig.PanelName);
// 先隐藏
this.myCustomTaskPane.Visible = false;
myCustomTaskPane.Width = MIN_WIDTH;
this.myCustomTaskPane.VisibleChanged += OnProofreadPanel_VisibleChange;
// 添加到集合
//this.myTaskPanes.Add(Application.ActiveDocument, this.myCustomTaskPane);
}
}
// 当面板的显示变化时
private void OnProofreadPanel_VisibleChange(object sender, EventArgs e)
{
if(this.myCustomTaskPane != null && myCustomTaskPane.Width < MIN_WIDTH)
{
this.myCustomTaskPane.Width = MIN_WIDTH;
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
public void SetPanelVisible(bool open)
{
if (this.proofreadPanelOpen == open) return;
this.InitProofreadPanel();
this.proofreadPanelOpen = open;
this.myCustomTaskPane.Visible = this.proofreadPanelOpen;
}
public bool TogglePanelVisible()
{
this.proofreadPanelOpen = !this.proofreadPanelOpen;
this.myCustomTaskPane.Visible = this.proofreadPanelOpen;
return this.proofreadPanelOpen;
}
#region VSTO
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}