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

132 lines
4.4 KiB
C#

using Microsoft.Office.Interop.Word;
using Microsoft.Web.WebView2.Core;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UtilLib;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Header;
namespace TestWordAddIn1
{
public partial class ProofreadPanel : UserControl
{
private CoreWebView2Environment env;
private readonly int ItemHeight = 75;
private readonly List<ProofreadItem> Items = new List<ProofreadItem>();
public ProofreadPanel()
{
InitializeComponent();
}
private void ProofreadPanel_Load(object sender, EventArgs e)
{
//InitWebview();
}
public void SendMessage(string action, object data)
{
//string json = JsonConvert.SerializeObject(new WebActionMessage(action, data));
//webView21.CoreWebView2.PostWebMessageAsJson(json);
}
//public async void InitWebview()
//{
//if (env == null)
//{
// // 自定义环境位置
// env = await CoreWebView2Environment.CreateAsync(null, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/web/user_data");
// webView21.CoreWebView2InitializationCompleted += WebView21_CoreWebView2InitializationCompleted;
// await webView21.EnsureCoreWebView2Async(env);
//}
//webView21.Source = new Uri(AppDomain.CurrentDomain.BaseDirectory + "\\web\\index.html");
////
//webView21.CoreWebView2.AddHostObjectToScript("bridge", new Bridge());
//}
private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
}
private void webView21_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
var app = Globals.ThisAddIn.Application;
var doc = app.ActiveDocument;
var content = doc.Content.Text.Trim();
if (content.Length > 0)
{
if (MessageBox.Show("将会清空文档内容并加载示例内容,确定继续操作?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK)
{
return;
}
// doc.Select();
var rng = doc.Range(0, content.Length);
rng.Delete();
}
var result = API.GetProofreadResult();
if (result == null)
{
MessageBox.Show("加载数据失败,请重试");
return;
}
var cur = app.Selection;
if (app.Options.Overtype)
{
// 禁用
app.Options.Overtype = false;
}
if (cur.Type != WdSelectionType.wdSelectionIP)
{
MessageBox.Show("异常");
return;
}
InitDocument(result);
}
private void InitDocument(ProofreadResult result)
{
var app = Globals.ThisAddIn.Application;
var cur = app.Selection;
var doc = app.ActiveDocument;
int index = 0;
foreach (var correct in result.CorrectedSet)
{
cur.TypeText(correct.Insert);
var rng = cur.Range;
if (correct.Diffs != null)
{
foreach (var it in correct.Diffs)
{
ProofreadItem item = new ProofreadItem();
item.SetProofreadItem(it, correct.Offset);
Items.Add(item);
if (it.Tag == "d")
{
var curg = doc.Range(correct.Offset + it.Start, correct.Offset + it.End);
// 设置玄宗效果
}
item.Location = new System.Drawing.Point(10, (ItemHeight + 10) * index);
panel1.Controls.Add(item);
index++;
}
}
}
}
}
}