269 lines
7.9 KiB
C#
269 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Word = Microsoft.Office.Interop.Word;
|
|
using Microsoft.Office.Tools.Word;
|
|
using AIProofread.Controls;
|
|
using Microsoft.Office.Tools;
|
|
using Newtonsoft.Json;
|
|
using UtilLib;
|
|
using System.Threading;
|
|
using Microsoft.Office.Interop.Word;
|
|
using System.Linq;
|
|
|
|
namespace AIProofread
|
|
{
|
|
|
|
public partial class ThisAddIn
|
|
{
|
|
|
|
public static SynchronizationContext FmainThreadContext;
|
|
|
|
public string AddinName = "AI校对王";
|
|
/// <summary>
|
|
/// 最小宽度
|
|
/// </summary>
|
|
private const int MinWidth = 400;
|
|
/// <summary>
|
|
/// 启动路径
|
|
/// </summary>
|
|
public string applicationStartupPath;
|
|
|
|
/// <summary>
|
|
/// 校对面板
|
|
/// </summary>
|
|
public ProofreadMainControl proofreadPanel;
|
|
/// <summary>
|
|
/// 工具栏
|
|
/// </summary>
|
|
public Ribbon1 ribbon;
|
|
|
|
public bool IsWPS { get; set; }
|
|
|
|
public List<FormLogin> LoginFormList = new List<FormLogin>();
|
|
|
|
public Dictionary<Word.Document, CustomTaskPane> taskPanels = new Dictionary<Word.Document, CustomTaskPane>();
|
|
private CustomTaskPane customTaskPane;
|
|
|
|
|
|
private void Application_WindowDeactivate(Word.Document Doc, Window Wn)
|
|
{
|
|
//HidePanel(Doc);
|
|
}
|
|
|
|
private void Application_WindowActivate(Word.Document Doc, Window Wn)
|
|
{
|
|
//ShowPanel(Doc);
|
|
|
|
if (!taskPanels.ContainsKey(Doc))
|
|
{
|
|
ShowPanel(Doc);
|
|
}
|
|
HideOtherPanel(Doc);
|
|
}
|
|
|
|
private void Application_DocumentBeforeClose(Word.Document Doc, ref bool Cancel)
|
|
{
|
|
DisposePanel(Doc);
|
|
}
|
|
|
|
private void Application_NewDocument(Word.Document Doc)
|
|
{
|
|
ShowPanel(Doc);
|
|
}
|
|
|
|
private void Application_DocumentOpen(Word.Document Doc)
|
|
{
|
|
ShowPanel(Doc);
|
|
}
|
|
void DisposePanel(Word.Document doc)
|
|
{
|
|
if (taskPanels.ContainsKey(doc))
|
|
{
|
|
taskPanels[doc].Visible = false;
|
|
taskPanels[doc].Dispose();
|
|
taskPanels.Remove(doc);
|
|
}
|
|
}
|
|
|
|
void HideOtherPanel(Word.Document doc)
|
|
{
|
|
if (taskPanels.ContainsKey(doc) && taskPanels[doc].Visible)
|
|
{
|
|
return;
|
|
}
|
|
foreach (var key in taskPanels.Keys)
|
|
{
|
|
taskPanels[key].Visible = doc == key;
|
|
}
|
|
}
|
|
|
|
void HidePanel(Word.Document doc)
|
|
{
|
|
if (taskPanels.ContainsKey(doc))
|
|
{
|
|
taskPanels[doc].Visible = false;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// word创建面板
|
|
/// </summary>
|
|
private CustomTaskPane ShowPanel(Word.Document doc)
|
|
{
|
|
//if (!IsWPS)
|
|
//{
|
|
// if (Application.ActiveDocument == null) return;
|
|
// if (doc == null) doc = Application.ActiveDocument;
|
|
//}
|
|
if (Application.ActiveDocument == null) return null;
|
|
if (doc == null) doc = Application.ActiveDocument;
|
|
|
|
if (taskPanels.ContainsKey(doc))
|
|
{
|
|
taskPanels[doc].Visible = true;
|
|
return taskPanels[doc];
|
|
}
|
|
|
|
//proofreadPanel = new ProofreadMainControl();
|
|
var panel = Globals.ThisAddIn.CustomTaskPanes.Add(new ProofreadMainControl(), AddinName);
|
|
this.customTaskPane = panel;
|
|
taskPanels.Add(doc, panel);
|
|
panel.Width = MinWidth;
|
|
panel.Visible = !IsWPS;
|
|
return panel;
|
|
}
|
|
|
|
public void Send(SendOrPostCallback d)
|
|
{
|
|
FmainThreadContext.Send(d, null);
|
|
}
|
|
|
|
//private void ProofreadPanel_SizeChanged(object sender, EventArgs e)
|
|
//{
|
|
// // 处理最小宽度
|
|
// if (customTaskPane != null && customTaskPane.Width < MinWidth && customTaskPane.Visible)
|
|
// {
|
|
// SendKeys.Send("{ESC}");
|
|
// customTaskPane.Width = MinWidth;
|
|
// }
|
|
//}
|
|
|
|
private void ThisAddIn_Startup(object sender, System.EventArgs e)
|
|
{
|
|
FmainThreadContext = SynchronizationContext.Current;
|
|
// 启动地址
|
|
applicationStartupPath = System.Windows.Forms.Application.StartupPath;
|
|
if (applicationStartupPath.Contains("WPS"))
|
|
{
|
|
IsWPS = true;
|
|
}
|
|
// 处理文档事件
|
|
Application.DocumentOpen += Application_DocumentOpen;
|
|
Application.DocumentBeforeClose += Application_DocumentBeforeClose;
|
|
Application.WindowActivate += Application_WindowActivate;
|
|
Application.WindowDeactivate += Application_WindowDeactivate;
|
|
(Application as ApplicationEvents4_Event).NewDocument += Application_NewDocument;
|
|
// 选区发生变化事件
|
|
this.Application.WindowSelectionChange += Application_WindowSelectionChange;
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Application_WindowSelectionChange(Word.Selection s)
|
|
{
|
|
if (s.Bookmarks != null)
|
|
{
|
|
if (s.Range.Start == s.Range.End) // 说明是点击呀
|
|
{
|
|
var count = s.Bookmarks.Count;
|
|
if (s.Bookmarks.Count == 1) // 只有这一个
|
|
{
|
|
foreach (Microsoft.Office.Interop.Word.Bookmark item in s.Bookmarks)
|
|
{
|
|
int proofreadId = Config.GetBookmarkIdByName(item.Name);
|
|
if (proofreadId > 0)
|
|
{
|
|
Bridge.bridge.SelectMarkById(proofreadId);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Bridge.bridge.SelectMarkById(-1);
|
|
}
|
|
|
|
public int MyProperty { get; set; }
|
|
|
|
public void SendMessageToWeb(string msg, object data)
|
|
{
|
|
var panel = this.ShowPanel(Application.ActiveDocument);
|
|
var json = JsonConvert.SerializeObject(new WebMessage(msg, data));
|
|
var control = (ProofreadMainControl)panel.Control;
|
|
control.web.CoreWebView2.PostWebMessageAsJson(json);
|
|
}
|
|
|
|
// 显示面板
|
|
public void ShowPanel()
|
|
{
|
|
this.customTaskPane.Visible = true;
|
|
}
|
|
|
|
// 隐藏面板
|
|
public void HidePanel()
|
|
{
|
|
this.customTaskPane.Visible = false;
|
|
}
|
|
|
|
|
|
|
|
public void ShowLoginForm(string action)
|
|
{
|
|
FormLogin frm = new FormLogin(action);
|
|
|
|
LoginFormList.Add(frm);
|
|
frm.Show();
|
|
}
|
|
|
|
void ClearPanels()
|
|
{
|
|
taskPanels.Values.ToList().ForEach(p =>
|
|
{
|
|
try
|
|
{
|
|
p.Dispose();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message + "\n" + ex.StackTrace);
|
|
Logger.Log(ex.Message + "\n" + ex.StackTrace);
|
|
}
|
|
});
|
|
taskPanels.Clear();
|
|
}
|
|
|
|
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
|
|
{
|
|
// PanelModule.DisposeCTP();
|
|
//this.proofreadPanel.Dispose();
|
|
ClearPanels();
|
|
}
|
|
|
|
#region VSTO generated code
|
|
|
|
/// <summary>
|
|
/// Required method for Designer support - do not modify
|
|
/// the contents of this method with the code editor.
|
|
/// </summary>
|
|
private void InternalStartup()
|
|
{
|
|
this.Startup += new System.EventHandler(ThisAddIn_Startup);
|
|
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
}
|