惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
K
Kaspersky official blog
S
Schneier on Security
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
T
Threatpost
T
Tor Project blog
T
Tenable Blog
The Register - Security
The Register - Security
T
Threat Research - Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
F
Fortinet All Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
H
Help Net Security
博客园_首页
I
Intezer
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Project Zero
Project Zero
Recorded Future
Recorded Future
C
Cybersecurity and Infrastructure Security Agency CISA
Vercel News
Vercel News
A
Arctic Wolf
P
Palo Alto Networks Blog
The Hacker News
The Hacker News
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Know Your Adversary
Know Your Adversary
P
Privacy & Cybersecurity Law Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
P
Proofpoint News Feed
P
Privacy International News Feed
G
GRAHAM CLULEY
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
Simon Willison's Weblog
Simon Willison's Weblog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
T
The Exploit Database - CXSecurity.com
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Security Latest
Security Latest
Cyberwarzone
Cyberwarzone
L
Lohrmann on Cybersecurity
C
Cisco Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
AWS News Blog
AWS News Blog

博客园 - AllenDang

创业心得 - 从确定投资意向到开始发工资还有多远? 什么样的人适合创业公司? NoSql数据库使用半年后在设计上面的一些心得 Go语言实战 - 使用SendCloud群发邮件 用Go语言做产品半年的一些感觉 我是怎么拿到投资的 Go语言实战 - 我需要站内搜索 Go语言实战 - revel框架教程之CSRF(跨站请求伪造)保护 Go语言实战 - revel框架教程之缓存和Job Go语言实战 - 网站性能优化第一弹“七牛云存储” 坚持是什么? - 作为创业者在与投资人见面之后想到的 Go语言实战 - revel框架教程之MongDB的最佳搭档revmgo Go语言实战 - revel框架教程之权限控制 Go语言实战 - revel框架教程之用户注册 Go语言实战 - 创业进行时之创业伊始 我为什么喜欢Go语言 用Go写Windows桌面应用 - 使用窗体设计器 使用CGO封装Windows API 用Go写Windows桌面应用 - 使用Form
用Go写Windows桌面应用 - 使用资源文件
AllenDang · 2012-02-16 · via 博客园 - AllenDang

这篇文章具有很强的时效性,是在2012年2月16日写的,这时Go1还没有发布,当前的Go编译器还无法嵌入资源文件。

上篇。我们做出了一个简单的窗体,但有两个重大缺陷:没有程序图标和没有应用系统主题。现在我们就来着手解决。

使用图标和系统主题,并将资源文件签入exe

创建一个资源文件(推荐使用ResEdit),在其中加入图标(用作程序图标)和一个manifest文件(用于启用系统主题),如下图所示。

image

现在代码目录里面应该拥有以下文件。

image

之后,就要用到windres这个工具了,它的作用是把一个资源文件(*.rc)编译成*.o,之后就可以使用Go的pack工具把*.o一起包装到exe文件里。命令行如下。

windres resource.rc -o temp-rc.o

go tool 8g app.go

go tool pack grc _go_.8 app.8 temp-rc.o

go tool 8l –s –Hwindowsgui –o app.exe _go_.8

现在可以看到我们的app.exe已经拥有程序图标了。

image

运行之后可以看到,系统主题也有了。

image

至此,已经能用Go开发一些小工具了。另外,gform还提供了从资源文件中读取ico、png、jpg的接口,为自绘控件提供了便利。

资源文件的其他相关函数

读取图片资源

gform.NewBitmapFromResource(instance w32.HINSTANCE, resName *uint16, resType *uint16, background Color) (*Bitmap, error)

用法

//此处对应资源文件中的ID

IDR_DROPARROW = 108

bmp, err := gform.NewBitmapFromResource(

    gform.GetAppInstance(), //应用程序实例

    syscall.StringToUTF16Ptr("PNG"), //资源文件中的资源类型

    w32.MakeIntResource(IDR_DROPARROW), //资源ID

    gform.RGB(139, 190, 37)) //透明填充色

读取图标资源

gform.NewIconFromResource(instance w32.HINSTANCE, resId uint16) (*Icon, error)

用法

IDI_ICON1 = 101

ico, err := gform.NewIconFromResource(

    gform.GetAppInstance(), //应用程序实例

    w32.MakeIntResource(IDI_ICON1)) //图标资源ID

读取对话框资源

gform.NewDialogFromResId(parent Controller, resId uint) *Dialog

用法

IDD_MAIN = 103

mainform = gform.NewDialogFromResId(nil, IDD_MAIN)
mainform.Center()
mainform.Show()

这个就有意思了,在没有完整的窗体设计器出现之前,ResEdit已经可以协助很多控件定位的工作了!Smile