修复图标加载问题

## 问题解决
- 将app.ico嵌入到项目资源中(EmbeddedResource)
- 更新图标加载方式使用GetManifestResourceStream
- 修复Form1和按钮的图标显示问题

## 技术改进
- 避免了外部文件依赖问题
- 图标编译时嵌入到exe中
- 提高应用部署的便携性

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Developer 2025-11-20 15:03:33 +08:00
parent ee620a3255
commit 0aae370ed4
3 changed files with 26 additions and 7 deletions

View File

@ -80,8 +80,12 @@ namespace WinFormsAppTemplate
// 设置浏览按钮图标 // 设置浏览按钮图标
try try
{ {
btnBrowseSource.Image = Image.FromFile("app.ico"); var stream = GetType().Assembly.GetManifestResourceStream("WinFormsAppTemplate.app.ico");
btnBrowseSource.ImageAlign = ContentAlignment.MiddleCenter; if (stream != null)
{
btnBrowseSource.Image = Image.FromStream(stream);
btnBrowseSource.ImageAlign = ContentAlignment.MiddleCenter;
}
} }
catch catch
{ {
@ -113,8 +117,12 @@ namespace WinFormsAppTemplate
// 设置浏览按钮图标 // 设置浏览按钮图标
try try
{ {
btnBrowseOutput.Image = Image.FromFile("app.ico"); var stream = GetType().Assembly.GetManifestResourceStream("WinFormsAppTemplate.app.ico");
btnBrowseOutput.ImageAlign = ContentAlignment.MiddleCenter; if (stream != null)
{
btnBrowseOutput.Image = Image.FromStream(stream);
btnBrowseOutput.ImageAlign = ContentAlignment.MiddleCenter;
}
} }
catch catch
{ {

View File

@ -9,14 +9,19 @@ namespace WinFormsAppTemplate
{ {
InitializeComponent(); InitializeComponent();
// 设置窗体图标 // 设置窗体图标 - 使用嵌入资源
try try
{ {
this.Icon = new Icon("app.ico"); var stream = GetType().Assembly.GetManifestResourceStream("WinFormsAppTemplate.app.ico");
if (stream != null)
{
this.Icon = new Icon(stream);
}
} }
catch catch
{ {
// 如果图标文件不存在或格式错误,忽略 // 如果图标加载失败,使用默认图标
// this.Icon = SystemIcons.Application;
} }
} }

View File

@ -7,6 +7,12 @@
<AssemblyName>WinFormsAppTemplate</AssemblyName> <AssemblyName>WinFormsAppTemplate</AssemblyName>
<RootNamespace>WinFormsAppTemplate</RootNamespace> <RootNamespace>WinFormsAppTemplate</RootNamespace>
<ApplicationIcon>app.ico</ApplicationIcon> <ApplicationIcon>app.ico</ApplicationIcon>
<Win32Resource />
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="app.ico" />
</ItemGroup>
<!-- 用于单文件发布的配置 --> <!-- 用于单文件发布的配置 -->
<PublishSingleFile>false</PublishSingleFile> <PublishSingleFile>false</PublishSingleFile>
<SelfContained>false</SelfContained> <SelfContained>false</SelfContained>