窗口大小改变后输出对应的窗口大小。
package main
import (
"fmt"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
//"github.com/mattn/go-gtk/gtk"
//"github.com/mattn/go-gtk/glib"
"log"
"os"
)
func main() {
const appId = "com.nayoso.example"
app, _ := gtk.ApplicationNew(appId, glib.APPLICATION_FLAGS_NONE)
_, err := app.Connect("activate", func() {
createWindow(app)
})
if err != nil {
log.Fatal(err)
}
app.Run(os.Args)
}
func createWindow(application *gtk.Application) {
// 从文件中创建Builder
builder, err := gtk.BuilderNewFromFile("19_大小改变事件/builder.ui")
if err != nil {
log.Fatal(err)
}
// 获取window窗口
winObj, _ := builder.GetObject("window1")
window := winObj.(*gtk.Window)
application.AddWindow(window)
// window 窗口设置
window.SetSizeRequest(300, 240) //设置窗口大小
window.SetTitle("hello go") //设置标题
//window.SetResizable(false) //设置不可伸缩
window.SetPosition(gtk.WIN_POS_CENTER) //设置居中显示
err = window.SetIconFromFile("images/app.ico") //设置icon
if err != nil {
log.Fatal(err)
}
//大小改变事件,改变窗口大小,自动触发此事件
_, _ = window.Connect("configure_event", func() {
//获取窗口尺寸
w, h := window.GetSize()
fmt.Println(w, h)
})
// 显示所有界面
window.ShowAll()
}
/*
事件标识:
事件标识 触发条件
“configure_event” 窗口大小改变时触发
*/
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="2.24"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
</object>
</interface>