===============
LIBRARY RULES
===============
From library maintainers:
- When examples are needed, you can use context7 MCP to retrieve example code from the repository with ID /twgh/xcgui-example.
### Create and Configure XCGUI Windows in Go
Source: https://context7.com/twgh/xcgui/llms.txt
This example shows how to create different types of windows and configure their appearance properties, including window size, border, icon, transparency, shadow, and minimum size. It demonstrates both code-based window creation and the use of SVG for icons.
```go
package main
import (
_ "embed"
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/imagex"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
// Method 1: Create window purely with code
w := window.New(100, 100, 430, 300, "Colorful Window", 0,
xcc.Window_Style_Default|xcc.Window_Style_Drag_Window)
// Set window border size (left, top, right, bottom)
w.SetBorderSize(0, 30, 0, 0)
// Create SVG icon and set it as the global window icon
svgIcon := ``
img := imagex.NewBySvgStringW(svgIcon)
a.SetWindowIcon(img.Handle)
// Set window transparency type to shadow mode
w.SetTransparentType(xcc.Window_Transparent_Shadow)
// Set window shadow: size 8, opacity 255, border radius 10, not inheriting parent, color black
w.SetShadowInfo(8, 255, 10, false, 0)
// Set window minimum size
w.SetMinimumSize(300, 200)
// Adjust window layout elements
w.AdjustLayout()
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Install XCGUI Library (Go)
Source: https://github.com/twgh/xcgui/blob/main/README.md
This command fetches and installs the XCGUI Go library using the go get command. It ensures you have the latest version of the library, including any updates.
```bash
go get -u github.com/twgh/xcgui
```
--------------------------------
### Implement Animations with XCGI in Go
Source: https://context7.com/twgh/xcgui/llms.txt
This Go code snippet demonstrates how to implement various animation effects using the XCGI library, including sequential, grouped, and looping animations. It covers creating animations for moving, rotating, and scaling UI elements, and provides examples of how to start, stop, and manage these animations. The 'xcgui' and 'xcgui/ani' packages are required.
```go
package main
import (
"github.com/twgh/xcgui/ani"
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 600, 500, "动画示例", 0, xcc.Window_Style_Default)
// 创建动画目标按钮
btn := widget.NewButton(50, 50, 120, 40, "点击我", w.Handle)
// 创建动画序列(参数: 目标元素句柄, 循环次数)
anima := ani.NewAnima(btn.Handle, 1)
// 添加移动动画: 持续时间1000ms, 移动到(300, 200)
anima.Move(1000, 300, 200, 0, xcc.Ease_Flag_Linear, false)
// 添加旋转动画: 持续时间500ms, 旋转360度
anima.Rotate(500, 360, 0, xcc.Ease_Flag_InOutSine, false)
// 添加缩放动画: 持续时间800ms, 缩放到1.5倍
anima.Scale(800, 1.5, 0, xcc.Ease_Flag_OutBounce, false)
// 点击按钮启动动画
btn.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
anima.Start()
return 0
})
// 创建动画组(多个动画同时执行)
btn2 := widget.NewButton(50, 150, 120, 40, "并行动画", w.Handle)
animaGroup := ani.NewAnimaGroup()
// 创建移动动画项并添加到组
moveItem := ani.NewAnimaItem()
moveItem.Move(1000, 300, 300, 0, xcc.Ease_Flag_Linear, false)
animaGroup.AddItem(moveItem.Handle)
// 创建旋转动画项并添加到组
rotateItem := ani.NewAnimaRotate()
rotateItem.Rotate(1000, 360, 0, xcc.Ease_Flag_Linear, false)
animaGroup.AddItem(rotateItem.Handle)
// 将动画组绑定到元素
animaGroup.BindEle(btn2.Handle)
btn2.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
animaGroup.Start()
return 0
})
// 创建循环动画按钮
btn3 := widget.NewButton(50, 250, 120, 40, "循环动画", w.Handle)
animaLoop := ani.NewAnima(btn3.Handle, -1) // -1表示无限循环
animaLoop.MoveEx(1000, 50, 250, 400, 250, 0, xcc.Ease_Flag_InOutQuad, true) // 来回移动
btn3.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
animaLoop.Start()
return 0
})
// 创建停止按钮
btnStop := widget.NewButton(50, 350, 120, 40, "停止所有动画", w.Handle)
btnStop.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
anima.Stop()
animaGroup.Stop()
animaLoop.Stop()
return 0
})
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Create and Display a Simple Window in Go using xcgui
Source: https://github.com/twgh/xcgui/blob/main/README.md
This Go code snippet demonstrates the fundamental process of creating a graphical window using the xcgui library. It initializes the application, creates a window with specified dimensions and style, sets window properties like border size and transparency, adds a button with a click event handler that displays a message box, and finally shows the window and runs the application loop. Dependencies include the 'app', 'imagex', 'widget', 'window', and 'xcc' packages from the xcgui library.
```go
package main
import (
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/imagex"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
// 1.初始化UI库
app.Init()
a := app.New(true)
// 启用自适应 DPI
a.EnableAutoDPI(true).EnableDPI(true)
// 2.创建窗口
w := window.New(0, 0, 430, 300, "xcgui window", 0, xcc.Window_Style_Default|xcc.Window_Style_Drag_Window)
// 设置窗口边框大小
w.SetBorderSize(0, 30, 0, 0)
// 设置全局窗口图标
a.SetWindowIcon(imagex.NewBySvgString(svgIcon).Handle)
// 设置窗口透明类型
w.SetTransparentType(xcc.Window_Transparent_Shadow)
// 设置窗口阴影
w.SetShadowInfo(8, 255, 10, false, 0)
// 创建按钮
btn := widget.NewButton(165, 135, 100, 30, "Button", w.Handle)
// 添加按钮点击事件
btn.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
w.MessageBox("提示", btn.GetText(), xcc.MessageBox_Flag_Ok|xcc.MessageBox_Flag_Icon_Info, xcc.Window_Style_Modal)
return 0
})
// 3.显示窗口
w.Show(true)
// 4.运行程序
a.Run()
// 5.释放UI库
a.Exit()
}
const svgIcon = ``
```
--------------------------------
### Initialize and Manage XCGUI Application Lifecycle in Go
Source: https://context7.com/twgh/xcgui/llms.txt
Demonstrates how to initialize the XCGUI library, create an application instance with Direct2D acceleration, enable DPI support, set the paint frequency, create and show the main window, run the message loop, and exit the application.
```go
package main
import (
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
// Initialize XCGUI library, release xcgui.dll to temp directory
app.Init()
// Create application instance, true enables Direct2D hardware acceleration
a := app.New(true)
if a == nil {
panic("Initialize XCGUI failed")
}
// Enable high DPI support and DPI adaptation
a.EnableAutoDPI(true).EnableDPI(true)
// Set global drawing frequency (milliseconds), controls UI refresh rate
a.SetPaintFrequency(20)
// Create main window
w := window.New(0, 0, 800, 600, "My Application", 0,
xcc.Window_Style_Default|xcc.Window_Style_Drag_Window)
// Show the window
w.Show(true)
// Run the message loop, automatically exits when no windows are open
a.Run()
// Exit the application and release all resources
a.Exit()
}
```
--------------------------------
### Load UI Layout from Memory Zip (Go)
Source: https://github.com/twgh/xcgui/blob/main/README.md
This Go code snippet demonstrates how to initialize the XCGUI application, load UI resources from a zip file embedded in memory, and create a main window using a layout defined in an XML file within that zip. It also shows how to access and interact with UI elements by their names.
```go
package main
import (
_ "embed"
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
)
//go:embed res/qqmusic.zip
var qqmusic []byte
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
// 从内存 zip 中加载资源文件
a.LoadResourceZipMem(qqmusic, "resource.res", "")
// 从内存 zip 中加载布局文件, 创建窗口对象
w := window.NewByLayoutZipMem(qqmusic, "main.xml", "", 0, 0)
// songTitle 是在 main.xml 中给歌曲名(shapeText 组件)设置的 name 属性的值.
// 通过 GetObjectByName 可以获取布局文件中设置了 name 属性的组件的句柄.
// 可简化为: widget.NewShapeTextByName("songTitle").
song := widget.NewShapeTextByHandle(app.GetObjectByName("songTitle"))
println(song.GetText()) // 输出: 两只老虎爱跳舞
// 调整布局
w.AdjustLayout()
// 显示窗口
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Create Lists and Tables with Data Binding in Go
Source: https://context7.com/twgh/xcgui/llms.txt
Demonstrates how to create multi-column list and table controls using XCgui in Go. It covers adding columns, setting headers, creating and binding data using adapters, and handling item selection and double-click events. Includes functionality to add and delete rows.
```go
package main
import (
"fmt"
"github.com/twgh/xcgui/adapter"
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 700, 500, "列表与表格示例", 0, xcc.Window_Style_Default)
// 创建列表控件,3列
list := widget.NewListEx(20, 20, 660, 200, w.Handle, 3)
// 添加列头
list.AddColumn(200) // 第0列,宽度200
list.AddColumn(200) // 第1列
list.AddColumn(260) // 第2列
// 设置列头文本
list.SetItemText(xcc.List_Header_ItemID, 0, "姓名")
list.SetItemText(xcc.List_Header_ItemID, 1, "年龄")
list.SetItemText(xcc.List_Header_ItemID, 2, "邮箱")
// 启用列头
list.EnableItemBkClip(true)
list.EnableMultiSel(true) // 启用多选
// 创建数据适配器
listAdapter := adapter.NewAdapterTable()
listAdapter.AddColumn("姓名")
listAdapter.AddColumn("年龄")
listAdapter.AddColumn("邮箱")
// 添加数据行
listAdapter.AddItemText("张三")
listAdapter.SetItemText(0, 1, "25")
listAdapter.SetItemText(0, 2, "zhangsan@example.com")
listAdapter.AddItemText("李四")
listAdapter.SetItemText(1, 1, "30")
listAdapter.SetItemText(1, 2, "lisi@example.com")
listAdapter.AddItemText("王五")
listAdapter.SetItemText(2, 1, "28")
listAdapter.SetItemText(2, 2, "wangwu@example.com")
// 绑定适配器到列表
list.BindAdapter(listAdapter.Handle)
// 列表项选中事件
list.AddEvent_List_ItemSelect(func(hEle, iItem int32, pbHandled *bool) int {
name := list.GetItemText(iItem, 0)
age := list.GetItemText(iItem, 1)
email := list.GetItemText(iItem, 2)
info := fmt.Sprintf("选中: %s, %s岁, %s", name, age, email)
println(info)
return 0
})
// 列表项双击事件
list.AddEvent_List_ItemDbClick(func(hEle, iItem int32, pbHandled *bool) int {
w.MessageBox("双击", fmt.Sprintf("双击了第 %d 项", iItem),
xcc.MessageBox_Flag_Ok, xcc.Window_Style_Modal)
return 0
})
// 创建按钮添加新行
btnAdd := widget.NewButton(20, 230, 100, 30, "添加行", w.Handle)
btnAdd.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
count := listAdapter.GetCount()
listAdapter.AddItemText(fmt.Sprintf("用户%d", count+1))
listAdapter.SetItemText(count, 1, "20")
listAdapter.SetItemText(count, 2, "user@example.com")
list.RefreshData() // 刷新显示
return 0
})
// 创建按钮删除选中行
btnDel := widget.NewButton(130, 230, 100, 30, "删除选中", w.Handle)
btnDel.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
iItem := list.GetSelectItem()
if iItem != -1 {
listAdapter.DeleteItem(iItem)
list.RefreshData()
}
return 0
})
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Create System Tray Icon with Menu in Go
Source: https://context7.com/twgh/xcgui/llms.txt
This Go code demonstrates how to create a system tray icon, add a context menu, and handle related events like right-click, double-click, and menu selections. It also shows how to minimize the window to the tray on close. Dependencies include the xcgui library. It takes an SVG string for the icon and returns a tray icon object.
```go
package main
import (
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/imagex"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 400, 300, "托盘图标示例", 0, xcc.Window_Style_Default)
// 加载托盘图标
svgIcon := ``
iconImage := imagex.NewBySvgStringW(svgIcon)
// 创建托盘图标
tray := window.NewTrayIcon(w.Handle, iconImage.Handle, "我的应用程序")
tray.Show(true)
// 创建托盘菜单
trayMenu := widget.NewMenu()
trayMenu.AddItem(1, "显示主窗口", 0, xcc.Menu_Item_Flag_Normal)
trayMenu.AddItem(2, "隐藏主窗口", 0, xcc.Menu_Item_Flag_Normal)
trayMenu.AddSeparator()
trayMenu.AddItem(3, "退出程序", 0, xcc.Menu_Item_Flag_Normal)
// 托盘图标点击事件
w.AddEvent_SysTrayRButtonUp(func(hWindow int, pbHandled *bool) int {
// 右键点击托盘图标,弹出菜单
trayMenu.PopupTray(w.Handle)
return 0
})
// 托盘图标左键双击事件
w.AddEvent_SysTrayLButtonDbClick(func(hWindow int, pbHandled *bool) int {
// 双击托盘图标,显示/隐藏窗口
if w.IsShowWindow() {
w.Show(false)
} else {
w.Show(true)
}
return 0
})
// 菜单项点击事件
w.AddEvent_Menu_Select(func(hWindow, nID int32, pbHandled *bool) int {
switch nID {
case 1: // 显示主窗口
w.Show(true)
case 2: // 隐藏主窗口
w.Show(false)
case 3: // 退出程序
tray.Destroy()
w.Destroy()
}
return 0
})
// 窗口关闭事件,最小化到托盘
w.AddEvent_WndClose(func(hWindow int, pbHandled *bool) int {
w.Show(false) // 隐藏窗口
*pbHandled = true // 阻止窗口真正关闭
return 0
})
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Create XCGUI Windows from Layout Files and Load Resources in Go
Source: https://context7.com/twgh/xcgui/llms.txt
This snippet demonstrates how to create an XCGUI window using an XML layout file and load resources such as images and fonts from a ZIP archive embedded in memory. It also shows how to access and modify UI elements defined in the layout file by their name or ID.
```go
package main
import (
_ "embed"
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
)
// Embed resource file into executable
//go:embed res/qqmusic.zip
var resourceZip []byte
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
// Load resource files (images, fonts, etc.) from memory ZIP
a.LoadResourceZipMem(resourceZip, "resource.res", "")
// Load style file from memory ZIP
a.LoadStyleZipMem(resourceZip, "style.xml", "")
// Create window from layout file in memory ZIP
w := window.NewByLayoutZipMem(resourceZip, "main.xml", "", 0, 0)
// Get component from layout file by name attribute
// In layout file:
songTitle := widget.NewShapeTextByName("songTitle")
if songTitle != nil {
text := songTitle.GetText()
println("Song Title:", text) // Output: Song Title: Two Tigers Dancing
// Modify text content and color
songTitle.SetText("New Song Title").SetTextColor(0xFFFF0000)
}
// Get component by ID
btn := widget.NewButtonByID(101)
if btn != nil {
btn.SetText("Click Me")
}
// Adjust layout
w.AdjustLayout()
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Create Dropdown ComboBox and Handle Selection Events (Go)
Source: https://context7.com/twgh/xcgui/llms.txt
Demonstrates how to create a dropdown combo box, add items, set a default selection, and handle selection change events. It also shows how to retrieve the currently selected item using a button click. This widget is useful for selecting one option from a list.
```go
package main
import (
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 400, 300, "下拉框示例", 0, xcc.Window_Style_Default)
// 创建下拉组合框
combo := widget.NewComboBox(50, 50, 300, 30, w.Handle)
// 添加选项
combo.AddItem("北京")
combo.AddItem("上海")
combo.AddItem("广州")
combo.AddItem("深圳")
combo.AddItem("杭州")
// 设置默认选中项(索引从0开始)
combo.SetSelItem(0)
// 选择改变事件
combo.AddEvent_ComboBox_Select(func(hEle, iItem int32, pbHandled *bool) int {
text := combo.GetItemText(iItem)
println("选中城市:", text)
return 0
})
// 创建按钮获取选中项
btn := widget.NewButton(50, 100, 150, 30, "获取选中项", w.Handle)
btn.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
iSel := combo.GetSelItem()
if iSel != -1 {
text := combo.GetItemText(iSel)
w.MessageBox("选中项", text, xcc.MessageBox_Flag_Ok, xcc.Window_Style_Modal)
}
return 0
})
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Load and Display Images in Various Formats (Go)
Source: https://context7.com/twgh/xcgui/llms.txt
Shows how to load and display images from different sources including files, ZIP archives, SVG strings, and GIF animations. It also covers loading specific regions of an image and using adaptive images for UI elements like buttons. Supports multiple image formats.
```go
package main
import (
_ "embed"
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/imagex"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
//go:embed res/image.png
var imageData []byte
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 600, 500, "图片示例", 0, xcc.Window_Style_Default)
// 方式1: 从文件加载图片
img1 := imagex.NewByFile("path/to/image.png")
if img1 != nil {
shapePic1 := widget.NewShapePicture(20, 20, 200, 150, w.Handle)
shapePic1.SetImage(img1.Handle)
}
// 方式2: 从ZIP文件加载图片
img2 := imagex.NewByZipMem(imageData, "image.png", "")
if img2 != nil {
shapePic2 := widget.NewShapePicture(240, 20, 200, 150, w.Handle)
shapePic2.SetImage(img2.Handle)
}
// 方式3: 从SVG字符串创建图片
svgString := ``
imgSvg := imagex.NewBySvgStringW(svgString)
if imgSvg != nil {
shapePic3 := widget.NewShapePicture(460, 20, 100, 100, w.Handle)
shapePic3.SetImage(imgSvg.Handle)
}
// 方式4: 从文件加载GIF动画
gifShape := widget.NewShapeGif(20, 200, 200, 150, w.Handle)
gifShape.SetImage(imagex.NewByFile("path/to/animation.gif").Handle)
gifShape.EnableAutoPlay(true) // 自动播放
// 方式5: 加载图片的指定区域
img5 := imagex.NewByFileRect("path/to/sprite.png", 0, 0, 64, 64)
if img5 != nil {
shapePic5 := widget.NewShapePicture(240, 200, 64, 64, w.Handle)
shapePic5.SetImage(img5.Handle)
}
// 方式6: 自适应图片(九宫格拉伸)
img6 := imagex.NewByFileAdaptive("path/to/button_bg.png", 10, 10, 10, 10)
if img6 != nil {
btn := widget.NewButton(20, 370, 200, 50, "九宫格背景", w.Handle)
btn.SetBkImage(img6.Handle)
}
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Create Tree Control and Handle Node Events (Go)
Source: https://context7.com/twgh/xcgui/llms.txt
Illustrates how to create a tree view widget, populate it with hierarchical data using an adapter, and handle node selection and expansion/collapse events. This is useful for displaying hierarchical data structures.
```go
package main
import (
"github.com/twgh/xcgui/adapter"
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 400, 500, "树形控件示例", 0, xcc.Window_Style_Default)
// 创建树控件
tree := widget.NewTree(20, 20, 360, 450, w.Handle)
// 创建树适配器
treeAdapter := adapter.NewAdapterTree()
// 插入根节点,返回项ID
rootId := treeAdapter.InsertItemText("根节点", 0, xcc.Tree_InsertPos_Last, 0)
// 在根节点下插入子节点
childId1 := treeAdapter.InsertItemText("子节点1", 0, xcc.Tree_InsertPos_Last, rootId)
childId2 := treeAdapter.InsertItemText("子节点2", 0, xcc.Tree_InsertPos_Last, rootId)
childId3 := treeAdapter.InsertItemText("子节点3", 0, xcc.Tree_InsertPos_Last, rootId)
// 在子节点1下插入孙节点
treeAdapter.InsertItemText("孙节点1-1", 0, xcc.Tree_InsertPos_Last, childId1)
treeAdapter.InsertItemText("孙节点1-2", 0, xcc.Tree_InsertPos_Last, childId1)
// 插入另一个根节点
root2Id := treeAdapter.InsertItemText("根节点2", 0, xcc.Tree_InsertPos_Last, 0)
treeAdapter.InsertItemText("子节点2-1", 0, xcc.Tree_InsertPos_Last, root2Id)
treeAdapter.InsertItemText("子节点2-2", 0, xcc.Tree_InsertPos_Last, root2Id)
// 绑定适配器
tree.BindAdapter(treeAdapter.Handle)
// 展开根节点
tree.ExpandItem(rootId, true)
// 节点选择事件
tree.AddEvent_Tree_Select(func(hEle, nItemID int32, pbHandled *bool) int {
text := tree.GetItemText(nItemID)
println("选中节点:", text)
return 0
})
// 节点展开/折叠事件
tree.AddEvent_Tree_Expand(func(hEle, nItemID int32, bExpand bool, pbHandled *bool) int {
text := tree.GetItemText(nItemID)
if bExpand {
println("展开节点:", text)
} else {
println("折叠节点:", text)
}
return 0
})
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Create and Handle Edit Boxes in Go
Source: https://context7.com/twgh/xcgui/llms.txt
Demonstrates the creation of single-line, multi-line, password, and read-only edit boxes using the XCgui framework in Go. It also covers setting placeholder text, handling text change events, and managing focus events.
```go
package main
import (
"fmt"
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 500, 400, "编辑框示例", 0, xcc.Window_Style_Default)
// 创建单行编辑框
edit1 := widget.NewEdit(20, 20, 300, 30, w.Handle)
edit1.SetText("单行文本")
// 设置提示文本
edit1.SetPlaceholderText("请输入用户名")
// 文本变化事件
edit1.AddEvent_Edit_Change(func(hEle int, pbHandled *bool) int {
text := edit1.GetText()
println("文本已变化:", text)
return 0
})
// 创建密码输入框
editPwd := widget.NewEdit(20, 60, 300, 30, w.Handle)
editPwd.SetPassword(true) // 设置为密码框
editPwd.SetPasswordCharacter('*') // 设置密码字符
editPwd.SetPlaceholderText("请输入密码")
// 创建多行编辑框
editMulti := widget.NewEdit(20, 100, 460, 200, w.Handle)
editMulti.SetMultiLine(true) // 设置为多行模式
editMulti.EnableAutoWrap(true) // 启用自动换行
editMulti.EnableVScrollBar(true) // 启用垂直滚动条
editMulti.SetText("这是多行文本编辑框\n可以输入多行内容")
// 创建只读编辑框
editReadOnly := widget.NewEdit(20, 310, 460, 30, w.Handle)
editReadOnly.SetReadOnly(true)
editReadOnly.SetText("这是只读文本,无法编辑")
// 获取焦点事件
edit1.AddEvent_Edit_SetFocus(func(hEle int, pbHandled *bool) int {
println("编辑框获得焦点")
return 0
})
// 失去焦点事件
edit1.AddEvent_Edit_KillFocus(func(hEle int, pbHandled *bool) int {
println("编辑框失去焦点")
return 0
})
// 创建获取按钮
btnGet := widget.NewButton(340, 20, 120, 30, "获取所有文本", w.Handle)
btnGet.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
text1 := edit1.GetText()
textMulti := editMulti.GetText()
info := fmt.Sprintf("单行: %s\n多行: %s", text1, textMulti)
w.MessageBox("文本内容", info, xcc.MessageBox_Flag_Ok, xcc.Window_Style_Modal)
return 0
})
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Create and Handle Button Click Events in XCGUI Go
Source: https://context7.com/twgh/xcgui/llms.txt
This code snippet shows how to create different types of buttons (normal, checkbox, radio) and register event handlers for them. It demonstrates using the `AddEvent_BnClick` and `AddEvent_Button_Check` methods for click and check events, respectively, and how to set button properties like text color and background color.
```go
package main
import (
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 430, 300, "Button Example", 0, xcc.Window_Style_Default)
// Create a normal button: x, y, width, height, text, parent window handle
btn1 := widget.NewButton(50, 50, 120, 35, "Click Me", w.Handle)
// Use AddEvent_ prefix functions to register events (recommended, no 2000 callback limit)
btn1.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
// Display a message box
w.MessageBox("Tip", "Button was clicked!",
xcc.MessageBox_Flag_Ok|xcc.MessageBox_Flag_Icon_Info,
xcc.Window_Style_Modal)
return 0
})
// Create a checkbox button
checkBox := widget.NewButton(50, 100, 120, 30, "Checkbox", w.Handle)
checkBox.SetType(xcc.Button_Type_Check)
// Checkbox checked/unchecked event
checkBox.AddEvent_Button_Check(func(hEle int, pbHandled *bool) int {
if checkBox.IsCheck() {
println("Checkbox is checked")
} else {
println("Checkbox is unchecked")
}
return 0
})
// Create radio buttons
radio1 := widget.NewButton(50, 140, 100, 30, "Option 1", w.Handle)
radio1.SetType(xcc.Button_Type_Radio)
radio1.SetGroupID(1) // Set radio group ID
radio2 := widget.NewButton(160, 140, 100, 30, "Option 2", w.Handle)
radio2.SetType(xcc.Button_Type_Radio)
radio2.SetGroupID(1)
// Radio button checked event
radio1.AddEvent_Button_RadioBox_Check(func(hEle int, pbHandled *bool) int {
println("Option 1 is selected")
return 0
})
// Set button text color and background color, supports method chaining
btn1.SetTextColor(0xFFFFFFFF).SetBkColor(0xFF0078D7)
w.Show(true)
a.Run()
a.Exit()
}
```
--------------------------------
### Go - 事件拦截与传递控制
Source: https://context7.com/twgh/xcgui/llms.txt
此Go代码示例演示了XCGUI中事件处理的顺序和拦截机制。通过为按钮和元素注册多个事件处理函数,可以观察到事件的传递顺序(后注册的先执行)以及如何通过设置pbHandled为true来拦截事件,阻止后续处理函数的执行。
```go
package main
import (
"github.com/twgh/xcgui/app"
"github.com/twgh/xcgui/widget"
"github.com/twgh/xcgui/window"
"github.com/twgh/xcgui/xcc"
)
func main() {
app.Init()
a := app.New(true)
a.EnableAutoDPI(true).EnableDPI(true)
w := window.New(0, 0, 450, 350, "事件拦截示例", 0, xcc.Window_Style_Default)
btn := widget.NewButton(100, 100, 250, 50, "多事件处理按钮", w.Handle)
// 第一个事件处理函数(最后注册的最先执行)
btn.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
println("第三个处理函数执行")
// 不设置pbHandled,事件继续传递
return 0
})
// 第二个事件处理函数
btn.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
println("第二个处理函数执行")
// 不设置pbHandled,事件继续传递
return 0
})
// 第三个事件处理函数(最先注册的最后执行)
btn.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
println("第一个处理函数执行")
return 0
})
// 创建拦截按钮
btnIntercept := widget.NewButton(100, 170, 250, 50, "拦截事件按钮", w.Handle)
// 拦截事件的处理函数
btnIntercept.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
println("拦截处理函数执行")
*pbHandled = true // 设置为true,阻止事件继续传递
w.MessageBox("拦截", "事件已被拦截,后续处理函数不会执行",
xcc.MessageBox_Flag_Ok, xcc.Window_Style_Modal)
return 0
})
// 这个处理函数不会被执行,因为上面的处理函数拦截了事件
btnIntercept.AddEvent_BnClick(func(hEle int, pbHandled *bool) int {
println("这个不会执行")
return 0
})
// 鼠标事件拦截示例
ele := widget.NewElement(100, 240, 250, 80, w.Handle)
ele.SetBkColor(0xFFE0E0E0)
// 鼠标移动事件
ele.AddEvent_MouseMove(func(hEle int, wParam, lParam uint32, pbHandled *bool) int {
println("鼠标移动")
return 0
})
// 鼠标左键按下事件
ele.AddEvent_LButtonDown(func(hEle int, wParam, lParam uint32, pbHandled *bool) int {
println("左键按下")
ele.SetBkColor(0xFFFFCDD2) // 改变背景色
ele.Redraw(true)
*pbHandled = true // 拦截事件
return 0
})
// 鼠标左键弹起事件
ele.AddEvent_LButtonUp(func(hEle int, wParam, lParam uint32, pbHandled *bool) int {
println("左键弹起")
ele.SetBkColor(0xFFC8E6C9) // 改变背景色
ele.Redraw(true)
return 0
})
w.Show(true)
a.Run()
a.Exit()
}
```