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 = 80; private readonly List Items = new List(); 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); item.Click += Item_Click; item.BookmarkClick += Item_Click; item.Location = new System.Drawing.Point(0, (ItemHeight + 10) * index); panel1.Controls.Add(item); index++; } } } } private void Item_Click(object sender, EventArgs e) { ProofreadItem item = (ProofreadItem)sender; foreach (var it in Items) { if(it == item) { it.SetSelect(); } else { it.SetNormal(); } } } } }