如何在golang中设置应用程序图标?
我刚刚在Windows上创建了我的第一个go应用程序 我如何给它一个图标 似乎没有任何生成标志来执行此操作,我知道golang不支持资源。您可以使用类似的工具生成一个.syso文件,其中包含嵌入在如何在golang中设置应用程序图标?,go,icons,Go,Icons,我刚刚在Windows上创建了我的第一个go应用程序 我如何给它一个图标 似乎没有任何生成标志来执行此操作,我知道golang不支持资源。您可以使用类似的工具生成一个.syso文件,其中包含嵌入在.rsrc节中的指定资源,目的是供Go链接器在生成Win32 excecutables时使用 以应用程序为例,它将其他元数据嵌入其可执行文件中 rsrc [-manifest FILE.exe.manifest] [-ico FILE.ico[,FILE2.ico...]] -o FILE.syso
.rsrc
节中的指定资源,目的是供Go链接器在生成Win32 excecutables时使用
以应用程序为例,它将其他元数据嵌入其可执行文件中
rsrc [-manifest FILE.exe.manifest] [-ico FILE.ico[,FILE2.ico...]] -o FILE.syso
-ico=”“
:要嵌入的.ico文件路径的逗号分隔列表
这与将二进制数据嵌入go程序不同。
为此,请使用 为了访问资产数据,我们使用
asset(string)[]byte
函数,该函数包含在生成的输出中
更新2021:如中所述,您还可以使用: 一个简单的命令行工具,用于在使用Go构建的Windows可执行文件中嵌入常用资源:
- 舱单
- 应用程序图标
- 版本信息(文件属性中的“详细信息”选项卡)
- 其他图标和光标
它也可以用作库。主题很长,事实上,
mingw
是唯一的要求,我们不需要第三方依赖。此外,资源文件*.rc
对于win32可执行应用程序是必需的。最后,您可以在中找到演示
1) 使用巧克力安装mingw:choco安装mingw
2) 创建main.exe.manifest
文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
version="1.0.0.0"
processorArchitecture="x86"
name="controls"
type="win32"
/>
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>
</assembly>
100 ICON "main.ico"
100 24 "main.exe.manifest"
101 RCDATA "content.zip"
4) 建造
在git bash窗口中,执行以下命令:
windres-o main-res.syso main.rc&&go build-i
有一个由Joseph Spurrier编写的github包:
Go语言的Microsoft Windows文件属性/版本信息和图标资源生成器
软件包创建一个包含Microsoft Windows版本信息和可选图标的syso文件。运行“go build”时,go将在可执行文件中嵌入版本信息、可选图标和可选清单。如果syso文件与main()函数位于同一目录中,Go将自动使用该文件
以上所有答案对我都不起作用。我能将ico嵌入GO exe的唯一方法是使用资源黑客
ResourceHacker -open main.exe -save output.exe -action addskip -res main.ico -mask ICONGROUP,MAIN,
这对我有好处
go get github.com/akavel/rsrc
rsrc -ico YOUR_ICON_FILE_NAME.ico
go build
这很有效:
go install github.com/tc-hib/go-winres@latest
go-winres simply --icon youricon.png
go build
如果您的应用程序具有GUI:
go winres simply--icon icon.png--manifest gui
您必须先更改图标,并认为您知道setIcon()方法
它只有一个字节[]
格式,所以您首先创建图像并将其转换为字节[]。下面的步骤也是如此
// define icon path using os.Open()
iconFile, err := os.Open("/home/user/image/icon.png")
if err != nil {
log.Fatal(err)
}
defer iconFile.Close()
// png.Decode() to convert image path to image
img, err := png.Decode(iconFile)
if err != nil {
log.Fatal(err)
}
// Then convert the image to byte[] format using bytes.Buffer
buf := new(bytes.Buffer)
err1 := jpeg.Encode(buf, img, nil)
_ = err1
// save the converted byte[] to variable
img_buf := buf.Bytes()
// set the coverted byte[] icon using systray.SetTemplateIcon() method
systray.SetTemplateIcon(img_buf, img_buf)
谢谢你 我不在windows上编写代码,所以我不确定,但在新版本的工具添加了对.syso文件的支持之后,是否关闭了该工具?没有听说过,似乎也找不到有关syso文件的更多信息。听起来它可能会解决资源问题,但不确定它是否会给exe一个漂亮的图标。@DenysSéguret syso文件现在(1.7.0-rc2)似乎总是包含在内:这很有效!我生成了一个syso文件并将其添加到根文件夹中,它工作得非常好。c文件的用途是什么?@GordonTruslove是让Go代码访问这些资源,并嵌入到最终的exe中。你已经失去了我,我没有添加c文件,图标仍然设置。你是说这样我就可以从go中访问文件了?就像我添加了File.dat一样,我可以在运行时使用c文件读取此文件?@GordonTruslove不,它是由go编译过程访问某些资源。显然,ico资源不需要它。如果我尝试添加我创建的文件“res.c”,编译器会抛出一个错误:.\res.c:1不是函数。\res.c:1语法错误,姓氏:L>1)使用chocolate安装mingw:choco安装mingw这需要第三方的依赖性。
// define icon path using os.Open()
iconFile, err := os.Open("/home/user/image/icon.png")
if err != nil {
log.Fatal(err)
}
defer iconFile.Close()
// png.Decode() to convert image path to image
img, err := png.Decode(iconFile)
if err != nil {
log.Fatal(err)
}
// Then convert the image to byte[] format using bytes.Buffer
buf := new(bytes.Buffer)
err1 := jpeg.Encode(buf, img, nil)
_ = err1
// save the converted byte[] to variable
img_buf := buf.Bytes()
// set the coverted byte[] icon using systray.SetTemplateIcon() method
systray.SetTemplateIcon(img_buf, img_buf)