/
main.go
183 lines (156 loc) · 4.62 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"fmt"
"runtime"
"github.com/ying32/govcl/vcl/rtl"
"github.com/ying32/govcl/vcl/types/colors"
"time"
"math/rand"
_ "github.com/ying32/govcl/pkgs/winappres"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/types"
)
type TMainFrom struct {
*vcl.TForm
ListView *vcl.TListView
stateImages *vcl.TImageList
isWindows bool
}
type TTempItem struct {
Caption string
Sub1 string
Sub2 string
Sub3 string
Sub4 string
Sub5 string
Checked bool
}
var (
MainFrom *TMainFrom
tempData []TTempItem
)
func main() {
vcl.Application.Initialize()
vcl.Application.CreateForm(&MainFrom)
vcl.Application.Run()
}
func (f *TMainFrom) OnFormCreate(sender vcl.IObject) {
f.isWindows = runtime.GOOS == "windows"
fmt.Println("OnCreate")
f.SetWidth(800)
f.SetHeight(600)
f.ScreenCenter()
f.SetDoubleBuffered(true)
f.ListView = vcl.NewListView(f)
f.ListView.SetAlign(types.AlClient)
f.ListView.SetParent(f)
f.ListView.SetViewStyle(types.VsReport)
f.ListView.SetOwnerData(true)
f.ListView.SetGridLines(true)
f.ListView.SetReadOnly(true)
f.ListView.SetRowSelect(true)
f.ListView.SetOnData(f.OnListView1Data)
// 要显示状态图标就得添加
f.ListView.SetCheckboxes(true)
// windows下OwnerData不能显示checkbox
// linux和macOS在OwnerData下支持显示CheckBox
if f.isWindows {
// 这里模拟显示
f.stateImages = vcl.NewImageList(f)
bmpFileName := "checkbox.png"
if rtl.FileExists(bmpFileName) {
pic := vcl.NewPicture()
pic.LoadFromFile(bmpFileName)
f.stateImages.AddSliced(pic.Bitmap(), 1, 2)
pic.Free()
}
f.ListView.SetStateImages(f.stateImages)
f.ListView.SetOnMouseDown(f.OnListView1MouseDown)
}
col := f.ListView.Columns().Add()
col.SetCaption("行号")
col.SetWidth(100)
col = f.ListView.Columns().Add()
col.SetCaption("子项1")
col.SetWidth(100)
col = f.ListView.Columns().Add()
col.SetCaption("子项2")
col.SetWidth(100)
col = f.ListView.Columns().Add()
col.SetCaption("子项3")
col.SetWidth(100)
col = f.ListView.Columns().Add()
col.SetCaption("子项4")
col.SetWidth(100)
col = f.ListView.Columns().Add()
col.SetCaption("子项5")
col.SetWidth(100)
// 产生100w条数据
tempData = make([]TTempItem, 1000000)
t := time.Now().UnixNano()
for i := 0; i < len(tempData); i++ {
tempData[i].Caption = fmt.Sprintf("%d", i+1)
tempData[i].Sub1 = fmt.Sprintf("子项1:%d", rand.Intn(1000000))
tempData[i].Sub2 = fmt.Sprintf("子项2:%d", rand.Intn(1000000))
tempData[i].Sub3 = fmt.Sprintf("子项3:%d", rand.Intn(1000000))
tempData[i].Sub4 = fmt.Sprintf("子项4:%d", rand.Intn(1000000))
tempData[i].Sub5 = fmt.Sprintf("子项5:%d", rand.Intn(1000000))
tempData[i].Checked = false
}
ns := time.Now().UnixNano() - t // 1e-6
fmt.Println("t:", ns, "ns, ", ns/1e6, "ms")
f.ListView.Items().SetCount(int32(len(tempData))) // 必须主动的设置Virtual List的行数
// 解决subitem数据为空时,不能连续绘制背景色
if f.isWindows {
f.ListView.SetOnAdvancedCustomDrawItem(f.onAdvancedCustomDrawItem)
} else {
f.ListView.SetOnItemChecked(f.onListView1ItemChecked)
}
}
func (f *TMainFrom) onListView1ItemChecked(sender vcl.IObject, item *vcl.TListItem) {
tempData[item.Index()].Checked = item.Checked()
}
func (f *TMainFrom) onAdvancedCustomDrawItem(sender *vcl.TListView, item *vcl.TListItem, state types.TCustomDrawState, Stage types.TCustomDrawStage, defaultDraw *bool) {
if state.In(types.CdsSelected) && Stage == types.CdPrePaint {
r := item.DisplayRect(types.DrBounds)
canvas := sender.Canvas()
canvas.Brush().SetColor(types.TColor(colors.RGB(0, 120, 215)))
canvas.FillRect(r)
}
}
func (f *TMainFrom) OnListView1Data(sender vcl.IObject, item *vcl.TListItem) {
data := tempData[int(item.Index())]
if f.isWindows {
if data.Checked {
item.SetStateIndex(1)
} else {
item.SetStateIndex(0)
}
} else {
item.SetChecked(data.Checked)
}
item.SetCaption(data.Caption)
item.SubItems().Add(data.Sub1)
item.SubItems().Add("") //data.Sub2)
item.SubItems().Add(data.Sub3)
item.SubItems().Add(data.Sub4)
item.SubItems().Add(data.Sub5)
}
func (f *TMainFrom) OnListView1MouseDown(sender vcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
if f.ListView.Checkboxes() && x <= 16 { //16= f.stateImages.Width
item := f.ListView.GetItemAt(x, y)
if item != nil {
idx := item.Index()
r := item.DisplayRect(types.DrIcon)
if y >= r.Top && y <= r.Bottom {
tempData[idx].Checked = !tempData[idx].Checked
// 不知道为啥idx=0时要repaint,但Repaint效率不如Invalidate
if idx == 0 {
f.ListView.Repaint()
} else {
f.ListView.Invalidate()
}
}
}
}
}