跨平台Golang GUI - GoVCL -Hello World

首次使用请先拉取GoVCL的代码。

获取GoVCL代码
go get -u github.com/ying32/govcl
编写代码
  • 1、通过UI设计器或者res2go工具生成GUI

此种方法是通过UI设计器或者res2go工具生成GUI。
UI设计的获取请加入QQ群中获取。

package main


import (
   "github.com/ying32/govcl/vcl"
   // 如果你使用自定义的syso文件则不要引用此包
   _ "github.com/ying32/govcl/pkgs/winappres"
)

type TMainForm struct {
    *vcl.TForm
    Btn1     *vcl.TButton
}

type TAboutForm struct {
    *vcl.TForm
    Btn1    *vcl.TButton
}

var (
    mainForm *TMainForm
    aboutForm *TAboutForm
)

func main() {
    vcl.Application.Initialize()
    vcl.Application.SetMainFormOnTaskBar(true)
    vcl.Application.CreateForm(&mainForm)
    vcl.Application.CreateForm(&aboutForm)
    vcl.Application.Run()
}

// -- TMainForm

func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
    
}

func (f *TMainForm) OnBtn1Click(sender vcl.IObject) {
    vcl.ShowMessage("Hello!")
}

// -- TAboutForm

func (f *TAboutForm) OnFormCreate(sender vcl.IObject) {
 
}

func (f *TAboutForm) OnBtn1Click(sender vcl.IObject) {
    vcl.ShowMessage("Hello!")
}
  • 2、通过纯代码编写

package main


import (
   "github.com/ying32/govcl/vcl"
   // 如果你使用自定义的syso文件则不要引用此包
   _ "github.com/ying32/govcl/pkgs/winappres"
)

type TMainForm struct {
    *vcl.TForm
    Btn1     *vcl.TButton
}

type TAboutForm struct {
    *vcl.TForm
    Btn1    *vcl.TButton
}

var (
    mainForm *TMainForm
    aboutForm *TAboutForm
)

func main() {
    vcl.Application.Initialize()
    vcl.Application.SetMainFormOnTaskBar(true)
    vcl.Application.CreateForm(&mainForm)
    // 创建完后关联子组件事件
    vcl.Application.CreateForm(&aboutForm, true)
    vcl.Application.Run()
}

// -- TMainForm

func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
    f.SetCaption("Hello")
    f.Btn1 = vcl.NewButton(f)
    f.Btn1.SetParent(f)
    f.Btn1.SetBounds(10, 10, 88, 28)
    f.Btn1.SetCaption("Button1")
    f.Btn1.SetOnClick(f.OnButtonClick)  
}

func (f *TMainForm) OnButtonClick(sender vcl.IObject) {
    vcl.ShowMessage("Hello!")
}


// -- TAboutForm

func (f *TAboutForm) OnFormCreate(sender vcl.IObject) {
    f.SetCaption("Hello")
    f.Btn1 = vcl.NewButton(f)
    //f.Btn1.SetName("Btn1")
    f.Btn1.SetParent(f)
    f.Btn1.SetBounds(10, 10, 88, 28)
    f.Btn1.SetCaption("Button1")
}

func (f *TAboutForm) OnBtn1Click(sender vcl.IObject) {
    vcl.ShowMessage("Hello!")
}

下载预编译GUI支持库。

GUI支持库可以通过三种方式下载:

复制GUI库到
  • Windows: 根据编译的二进制是32还是64位的,复制对应的"libvcl.dll"或者"libvclx64.dll"或者“liblcl.dll”到当前exe目录或系统环境路径下。

    • Go环境变量: GOARCH = amd64 386 GOOS = windows CGO_ENABLED=0
  • Linux: 复制"liblcl.so"可执行文件目录下(也可复制liblcl.so到/usr/lib/(32位liblcl)或者/usr/lib/x86_64-linux-gnu/(64位liblcl)目录中,作为公共库使用)。

    • Go环境变量: GOARCH = amd64 GOOS = linux CGO_ENABLED=1
  • MacOS: 复制"liblcl.dylib"可执行文件目录下(MacOS下注意:需要自行创建info.plist文件),或者参考:MacOS上应用打包

    • Go环境变量: GOARCH = amd64 GOOS = darwin CGO_ENABLED=1
运行截图

在这里插入图片描述

发布了17 篇原创文章 · 获赞 34 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/zyjying520/article/details/104802159