Skip to content

Commit

Permalink
load png.logo error
Browse files Browse the repository at this point in the history
  • Loading branch information
trueai-org committed Jun 26, 2024
1 parent 62e9a0a commit 638e17b
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/MDriveSync.Client.App.WinForm/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Web.WebView2.WinForms;
using Serilog;
using System.Reflection;

namespace MDriveSync.Client.App.WinForm
Expand All @@ -16,38 +17,49 @@ public MainForm()
{
InitializeComponent();

// 从配置文件中读取 URL,如果没有则使用默认值
var configuration = Program.Configuration;
apiUrl = configuration.GetValue<string>("urls")?.Replace("*", "localhost") ?? "http://localhost:8080";

// Initialize WebView2
webView = new WebView2
try
{
Dock = DockStyle.Fill,
Source = new Uri($"{apiUrl}") // 指向 Web API 的 URL
};
this.Controls.Add(webView);
// 从配置文件中读取 URL,如果没有则使用默认值
var configuration = Program.Configuration;
apiUrl = configuration.GetValue<string>("urls")?.Replace("*", "localhost") ?? "http://localhost:8080";

// Initialize NotifyIcon
notifyIcon = new NotifyIcon
{
Icon = this.Icon,
Text = "MDrive",
Visible = true
};
// Initialize WebView2
webView = new WebView2
{
Dock = DockStyle.Fill,
Source = new Uri($"{apiUrl}") // 指向 Web API 的 URL
};
this.Controls.Add(webView);

// Initialize NotifyIcon
notifyIcon = new NotifyIcon
{
Icon = this.Icon,
Text = "MDrive",
Visible = true
};

// 使用资源中的 PNG 图像
this.Icon = LoadIconFromResource("MDriveSync.Client.WinFormAPI.Resources.logo.png", 64, 64);
// 使用资源中的 PNG 图像
// 获取当前程序集名称,而不是写死
var assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
this.Icon = LoadIconFromResource($"{assemblyName}.Resources.logo.png", 64, 64);

notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;

// Initialize ContextMenuStrip
contextMenuStrip = new ContextMenuStrip();
exitMenuItem = new ToolStripMenuItem("退出", null, ExitMenuItem_Click);
contextMenuStrip.Items.Add(exitMenuItem);
notifyIcon.ContextMenuStrip = contextMenuStrip;
// Initialize ContextMenuStrip
contextMenuStrip = new ContextMenuStrip();
exitMenuItem = new ToolStripMenuItem("退出", null, ExitMenuItem_Click);
contextMenuStrip.Items.Add(exitMenuItem);
notifyIcon.ContextMenuStrip = contextMenuStrip;

this.Resize += MainForm_Resize;
this.Resize += MainForm_Resize;
}
catch (Exception ex)
{
Log.Logger.Error(ex, "初始化失败,请重试");

MessageBox.Show("初始化失败,请重试");
}
}

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

0 comments on commit 638e17b

Please sign in to comment.