pwiecz/go-fltk
GitHub: pwiecz/go-fltk
Go 语言对 FLTK 1.4 GUI 库的封装,使开发者能够用 Go 快速构建轻量、独立的跨平台桌面图形应用程序。
Stars: 177 | Forks: 32
# go-fltk
[](https://pkg.go.dev/github.com/pwiecz/go-fltk)
一个针对 FLTK 1.4 库的简单封装,这是一个轻量级的 GUI 库,允许创建小巧、独立且快速的 GUI 应用程序。
## 状态
该库目前只是一个部分封装。新的函数会根据用户的需求不断添加。
## 环境要求
要构建 go-fltk,除了 Golang 编译器之外,您还需要一个 C++11 编译器,例如 Linux 上的 GCC 或 Clang,Windows 上的 MinGW 以及 MacOS 上的 XCode。
go-fltk 为某些架构(linux/amd64、windows/amd64)提供了预构建的 FLTK 库,但您也可以轻松地自行重新构建它们,
或者为其他架构进行构建。
要为您的平台构建 FLTK 库,只需在 go-fltk 源码树的根目录下调用 `go generate` 即可。
如果构建过程对您不起作用,您可以自行修改 `fltk-build.sh` 或 `fltk-build.bat`,或者在 `https://github.com/pwiecz/go-fltk/discussions` 上提问。
要运行使用 go-fltk 构建的程序,您将需要一些通常在带有图形用户界面的操作系统上可用的系统库:
- Windows:除了 (对于 mingw64)之外没有外部依赖项
- MacOS:无外部依赖项
- Linux(以及其他 Unix 系统 - 未测试):您需要:
- X11
- Xrender
- Xcursor
- Xfixes
- Xext
- Xft
- Xinerama
- XKBCommon
- Wayland
- libdecor
- DBus
- OpenGL
## 用法
```
package main
import "github.com/pwiecz/go-fltk"
func main() {
win := fltk.NewWindow(400, 300)
win.SetLabel("Main Window")
btn := fltk.NewButton(160, 200, 80, 30, "Click")
btn.SetCallback(func() {
btn.SetLabel("Clicked")
})
win.End()
win.Show()
fltk.Run()
}
```
组件通过 `fltk.New` 函数创建,并根据您正在实例化的组件进行相应的修改。
函数和方法的名称类似于原始的 C++ 名称,但是遵循 Golang 的 PascalCase 命名约定。
Setter 方法也以 `Set` 前缀开头。
## 样式
FLTK 提供 4 种内置的 scheme:
- base(默认)
- gtk+
- gleam
- plastic
例如,可以使用 `fltk.SetScheme("gtk+")` 来设置它们。
FLTK 还允许对您的组件进行自定义样式设置:
```
package main
import (
"strconv"
"github.com/pwiecz/go-fltk"
)
// FLTK uses an RGBI color representation, the I is an index into FLTK's color map
// Passing 00 as I will use the RGB part of the value
const GRAY = 0x75757500
const LIGHT_GRAY = 0xeeeeee00
const BLUE = 0x42A5F500
const SEL_BLUE = 0x2196F300
const WIDTH = 600
const HEIGHT = 400
func main() {
curr := 0
fltk.InitStyles()
win := fltk.NewWindow(WIDTH, HEIGHT)
win.SetLabel("Flutter-like")
win.SetColor(fltk.WHITE)
bar := fltk.NewBox(fltk.FLAT_BOX, 0, 0, WIDTH, 60, " FLTK App!")
bar.SetDrawHandler(func() { // Shadow under the bar
fltk.DrawBox(fltk.FLAT_BOX, 0, 0, WIDTH, 63, LIGHT_GRAY)
})
bar.SetAlign(fltk.ALIGN_INSIDE | fltk.ALIGN_LEFT)
bar.SetLabelColor(255) // this uses the index into the color map, here it's white
bar.SetColor(BLUE)
bar.SetLabelSize(22)
text := fltk.NewBox(fltk.NO_BOX, 250, 180, 100, 40, "You have pushed the button this many times:")
text.SetLabelSize(18)
text.SetLabelFont(fltk.TIMES)
count := fltk.NewBox(fltk.NO_BOX, 250, 180+40, 100, 40, "0")
count.SetLabelSize(36)
count.SetLabelColor(GRAY)
btn := fltk.NewButton(WIDTH-100, HEIGHT-100, 60, 60, "@+6plus") // this translates into a plus sign
btn.SetColor(BLUE)
btn.SetSelectionColor(SEL_BLUE)
btn.SetLabelColor(255)
btn.SetBox(fltk.OFLAT_BOX)
btn.ClearVisibleFocus()
btn.SetCallback(func() {
curr += 1
count.SetLabel(strconv.Itoa(curr))
})
win.End()
win.Show()
fltk.Run()
}
```

Label 属性可以在[这里](https://www.fltk.org/doc-1.4/common.html#common_labels)查看。
## 图像支持
FLTK 通过几种图像类型支持矢量图和光栅图:
- SvgImage
- RgbImage
- JpegImage
- PngImage
- BmpImage
- SharedImage
其中一些可以从图像文件或数据中实例化:
```
package main
import (
"fmt"
"github.com/pwiecz/go-fltk"
)
func main() {
win := fltk.NewWindow(400, 300)
box := fltk.NewBox(fltk.FLAT_BOX, 0, 0, 400, 300, "")
image, err := fltk.NewJpegImageLoad("image.jpg")
if err != nil {
fmt.Printf("An error occured: %s\n", err)
} else {
box.SetImage(image)
}
win.End()
win.Show()
fltk.Run()
}
```
## 资源
- 指向官方 FLTK 1.4 文档的[链接](https://www.fltk.org/doc-1.4/index.html)。
- 指向 go-fltk 文档的[链接](https://pkg.go.dev/github.com/pwiecz/go-fltk)。
标签:C++绑定, FLTK, Gophish, Go语言, GUI库, 日志审计, 用户界面开发, 程序破解