/
main.go
129 lines (110 loc) · 3.08 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"runtime"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/rtl"
"github.com/ying32/govcl/vcl/types"
)
func main() {
vcl.Application.SetIconResId(3)
vcl.Application.Initialize()
vcl.Application.SetMainFormOnTaskBar(true)
mainForm := vcl.Application.CreateForm()
mainForm.SetCaption("Hello")
mainForm.SetPosition(types.PoScreenCenter)
mainForm.EnabledMaximize(false)
mainForm.SetWidth(600)
mainForm.SetHeight(500)
icon := vcl.NewIcon()
defer icon.Free()
if runtime.GOOS == "windows" {
icon.LoadFromResourceID(rtl.MainInstance(), 3)
} else {
icon.LoadFromFile("./brown.ico")
}
imglist := vcl.NewImageList(mainForm)
imglist.AddIcon(icon)
ico2 := vcl.NewIcon()
ico2.LoadFromFile("brown.ico")
imglist.AddIcon(ico2)
ico2.Free()
// -----------TreeView 不同Node弹出不同菜单,两个右键例程不同使用
tvpm1 := vcl.NewPopupMenu(mainForm)
mItem := vcl.NewMenuItem(mainForm)
mItem.SetCaption("第一种")
tvpm1.Items().Add(mItem)
tvpm2 := vcl.NewPopupMenu(mainForm)
mItem = vcl.NewMenuItem(mainForm)
mItem.SetCaption("第二种")
tvpm2.Items().Add(mItem)
tv := vcl.NewTreeView(mainForm)
tv.SetParent(mainForm)
tv.SetAlign(types.AlClient)
tv.SetImages(imglist)
tv.SetStateImages(imglist)
// 自动展开
//tv.SetAutoExpand(true)
tv.SetOnClick(func(vcl.IObject) {
node := tv.Selected()
if node != nil && node.IsValid() {
fmt.Println("Text:", node.Text(), ", Level:", node.Level(), ", Index:", node.Index(), ", hasChild:", node.HasChildren())
}
})
// 双击删除
tv.SetOnDblClick(func(sender vcl.IObject) {
sel := tv.Selected()
if sel.IsValid() {
sel.Delete()
}
// 或者
//tv.Items().Delete(sel)
})
tv.SetOnMouseDown(func(sender vcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
if button == types.MbRight {
node := tv.GetNodeAt(x, y)
if node != nil && node.IsValid() {
// 自由决择是否选中
node.SetSelected(true)
// 根据Level来判断,这里只是做演示
p := vcl.Mouse.CursorPos()
switch node.Level() {
case 0:
tvpm1.Popup(p.X, p.Y)
case 1:
tvpm2.Popup(p.X, p.Y)
}
fmt.Println("node.Level():", node.Level(), ", text:", node.Text())
}
}
})
// tv.Items().Clear()
// 第一个节点
node := tv.Items().AddChild(nil, "首个")
// 批量添加最好使用BeginUpdate与EndUpdate组合
tv.Items().BeginUpdate()
for i := 0; i < 30; i++ {
subnode := tv.Items().AddChild(node, fmt.Sprintf("Node%d", i))
// 设置相关imagelist中的图标索引
// 默认图标索引
subnode.SetImageIndex(1)
// 鼠标选中时索引
subnode.SetSelectedIndex(1)
// 节点展开时索引
//subnode.SetExpandedImageIndex(1)
// 状态图标索引
//subnode.SetStateIndex(1)
}
tv.Items().EndUpdate()
// 展开
node.Expand(true)
// 第二个节点
node = tv.Items().AddChild(nil, "第二个节点")
// 批量添加最好使用BeginUpdate与EndUpdate组合
tv.Items().BeginUpdate()
for i := 0; i < 30; i++ {
tv.Items().AddChild(node, fmt.Sprintf("Node%d", i))
}
tv.Items().EndUpdate()
vcl.Application.Run()
}