Skip to content

Commit

Permalink
Update MainForm.cs
Browse files Browse the repository at this point in the history
只有这个的编码是gb2312,怪
  • Loading branch information
ks233 committed Dec 29, 2023
1 parent 72cf199 commit 10926b5
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions GUI/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Core;
using System.Diagnostics;
using System.Runtime.InteropServices;
using MeCab;
Expand Down Expand Up @@ -44,25 +44,25 @@ private async Task InitializeWebView()
await webView.EnsureCoreWebView2Async(null);
webView.CoreWebView2.Settings.IsStatusBarEnabled = false;
webView.CoreWebView2.Profile.PreferredColorScheme = CoreWebView2PreferredColorScheme.Light;
// 处理打开新窗口(用默认浏览器打开)
// 处理打开新窗口(用默认浏览器打开)
webView.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
// 接收js消息(查词典)
// 接收js消息(查词典)
webView.CoreWebView2.WebMessageReceived += CoreWebView2_WebMessageReceived;
}

private async void MainForm_Load(object sender, EventArgs e)
{
// 初始化 webview
// 初始化 webview
await InitializeWebView();
GptCaller.Initialize();
#if DEBUG
webView.Source = new Uri("http://localhost:5173/"); // dev
Text += " - Debug -";
#else
// 初始化 HTTP 服务器
// 初始化 HTTP 服务器
HttpServer.StartServer();
webView.Source = new Uri($"http://localhost:{HttpServer.Port}/"); // build
Text += $" - 已占用端口:{HttpServer.Port} -";
Text += $" - 已占用端口:{HttpServer.Port} -";
#endif

dictForm = new DictForm(this);
Expand All @@ -74,9 +74,9 @@ private async void MainForm_Load(object sender, EventArgs e)

private void CoreWebView2_NewWindowRequested(object sender, CoreWebView2NewWindowRequestedEventArgs e)
{
e.Handled = true; // 防止WebView2打开新窗口
e.Handled = true; // 防止WebView2打开新窗口

// 使用默认浏览器打开链接
// 使用默认浏览器打开链接
Process.Start(new ProcessStartInfo(e.Uri) { UseShellExecute = true });
}
private void CoreWebView2_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
Expand Down Expand Up @@ -120,17 +120,17 @@ private void MainForm_SizeChanged(object sender, EventArgs e)
}


private System.Drawing.Point locationBefore; // 记录普通模式下窗口的位置
private Size sizeBefore; // 记录普通模式下窗口的大小
private int heightAfter = 200; // 附着模式时,窗体通常会比较矮
private System.Drawing.Point locationBefore; // 记录普通模式下窗口的位置
private Size sizeBefore; // 记录普通模式下窗口的大小
private int heightAfter = 200; // 附着模式时,窗体通常会比较矮

private void checkBoxWindowAttach_CheckedChanged(object sender, EventArgs e)
{
WindowAttacher.TargetHwnd = IntPtr.Parse(textBoxHwnd.Text);

if (checkBoxWindowAttach.Checked)
{
// 记录普通状态的窗口位置,切换到吸附状态下的窗口位置
// 记录普通状态的窗口位置,切换到吸附状态下的窗口位置
sizeBefore = this.Size;
locationBefore = this.Location;
this.Height = heightAfter;
Expand Down Expand Up @@ -179,7 +179,7 @@ private void checkBoxClipboardMode_CheckedChanged(object sender, EventArgs e)
}
private void timerGetClipboard_Tick(object sender, EventArgs e)
{
string newSentence = Clipboard.GetText(TextDataFormat.UnicodeText).Trim().Replace(" ", "");
string newSentence = Clipboard.GetText(TextDataFormat.UnicodeText).Trim().Replace(" ", "");
if (newSentence != "" && newSentence != Sentence)
{
Sentence = newSentence;
Expand All @@ -201,15 +201,15 @@ public bool ClipBoardMode

private void btnInputText_Click(object sender, EventArgs e)
{
Sentence = Microsoft.VisualBasic.Interaction.InputBox("手动输入", "输入句子", "", 0, 0);
Sentence = Microsoft.VisualBasic.Interaction.InputBox("手动输入", "输入句子", "", 0, 0);
}

private void timerSelectWindow_Tick(object sender, EventArgs e)
{
IntPtr hwnd = WindowAttacher.GetHwndByCursorPoint();
textBoxHwnd.Text = hwnd.ToString();

// 判断鼠标是否按下
// 判断鼠标是否按下
bool mouseDown = WindowAttacher.MouseDown(MouseButtons.Left);

if (mouseDown)
Expand Down Expand Up @@ -248,8 +248,8 @@ private void textBoxHwnd_TextChanged(object sender, EventArgs e)
{
IntPtr hwnd = IntPtr.Parse(textBoxHwnd.Text);
string windowTitle = WindowAttacher.GetWindowTitle(hwnd);
checkBoxWindowAttach.Text = $"与【{windowTitle}】对齐";
// 判断窗口句柄是不是自己的
checkBoxWindowAttach.Text = $"与【{windowTitle}】对齐";
// 判断窗口句柄是不是自己的
if (hwnd == this.Handle || hwnd == dictForm.Handle)
{
checkBoxWindowAttach.Enabled = false;
Expand All @@ -271,14 +271,14 @@ async private Task TranslateSentence()
ClearTranslationText();
GptCaller.StreamResponse(chat, res => AppendTranslationText(res));
}
else if (comboBoxTranslator.Text == "谷歌生草机")
else if (comboBoxTranslator.Text == "谷歌生草机")
{
// 这个接口效果真的好烂。。。用来翻译片假名还可以,翻译句子真就生草机
// 这个接口效果真的好烂。。。用来翻译片假名还可以,翻译句子真就生草机
await webView.ExecuteScriptAsync($"runGoogleTrans(\"{sentence.Replace("\r\n", "")}\")");
}
else if (comboBoxTranslator.Text == "谷歌翻译")
else if (comboBoxTranslator.Text == "谷歌翻译")
{
// 效果与网页版一致的API,不知道能用多久
// 效果与网页版一致的API,不知道能用多久
await webView.ExecuteScriptAsync($"runGoogleTransTk(\"{sentence.Replace("\r\n", "")}\")");
}
}
Expand All @@ -296,7 +296,7 @@ private void checkBoxTopmost_CheckedChanged(object sender, EventArgs e)
private void buttonUpdateExtraPrompt_Click(object sender, EventArgs e)
{
UserConfig.UpdateExtraPrompt();
MessageBox.Show($"已重新读取文件{UserConfig.ExtraPromptFilename}:\n" + UserConfig.ExtraPrompt);
MessageBox.Show($"已重新读取文件{UserConfig.ExtraPromptFilename}:\n" + UserConfig.ExtraPrompt);
}

private void checkBoxUseExtraPrompt_CheckedChanged(object sender, EventArgs e)
Expand Down

0 comments on commit 10926b5

Please sign in to comment.