ai_office_plugin/AIProofread/Controls/ProofreadMainControl.cs
callmeyan 32e85c62c0 优化日志记录、资源管理及功能支持
- 引入 log4net 库,统一日志记录方式,提升可维护性。
- 优化异常处理,增加详细日志记录,增强代码健壮性。
- 调整资源文件引用,新增图标资源,删除无用资源。
- 优化文档事件处理逻辑,改进面板显示与隐藏逻辑。
- 增加对 WPS 环境的支持,动态调整功能行为。
- 禁用部分功能(如常识性检测、客服、升级和帮助)。
- 删除冗余代码,清理注释,统一代码风格。
- 更新程序集版本至 2.2.5,改进调试与生产环境配置。
2025-05-08 13:57:12 +08:00

89 lines
2.8 KiB
C#

using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Windows.Forms;
namespace AIProofread.Controls
{
public partial class ProofreadMainControl : UserControl
{
public ProofreadMainControl()
{
InitializeComponent();
Bridge.InitWebEnvAsync("main", web);
//this.minWidth = 420 * LabelWidth() / 42;
//this.MinimumSize = new System.Drawing.Size(this.minWidth, 0);
}
private bool isShowingErrorPage = false;
public void WebView2NavigationCompleted(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
if (isShowingErrorPage)
{
return;
}
if (e.IsSuccess)
{
isShowingErrorPage = false;
return;
}
isShowingErrorPage = true;
string errorPageHtml = @"
<html>
<head>
<title>加载失败</title>
<style>
body { font-family: sans-serif; text-align: center; padding-top: 50px; user-select: none; }
h1 { color: #d00; }
</style>
</head>
<body oncontextmenu='return false;'>
<h1>加载组件失败</h1>
<p>请检查您的网络连接并重启软件。</p>
<a href=" + "\"javascript:window.chrome.webview.postMessage('reload');\"" + @">重新加载</a>
</body>
</html>";
(sender as WebView2).CoreWebView2.NavigateToString(errorPageHtml);
}
private void ProofreadMainControl_Load(object sender, EventArgs e)
{
this.web.NavigationCompleted += WebView2NavigationCompleted;
this.web.WebMessageReceived += (s, ex) =>
{
if (ex.TryGetWebMessageAsString() == "reload")
{
isShowingErrorPage = false;
web.Source = new Uri(Config.WebPath("correct"));
}
};
this.web.Source = new Uri(Config.WebPath("correct" ));
//this.SizeChanged += ProofreadMainControl_SizeChanged;
}
private void ProofreadMainControl_SizeChanged(object sender, EventArgs e)
{
//if(this.minWidth > 0 && this.Width < this.minWidth)
//{
// SendKeys.Send("{ESC}");
// this.Width = this.minWidth;
//}
}
public int LabelWidth()
{
return label1.Width;
}
public void ResetWeb()
{
if (!this.web.IsDisposed)
{
this.web.Stop();
this.web.Dispose();
}
}
}
}