-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
356 lines (306 loc) · 9.73 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// govcl project main.go
// go.exe build -i -ldflags="-H windowsgui"
package main
import (
"fmt"
_ "github.com/ying32/govcl/pkgs/winappres"
"github.com/ying32/govcl/vcl"
_ "github.com/ying32/govcl/vcl/locales/zh_CN"
"github.com/ying32/govcl/vcl/rtl"
"github.com/ying32/govcl/vcl/rtl/version"
"github.com/ying32/govcl/vcl/types"
"github.com/ying32/govcl/vcl/win"
)
var (
mainForm *vcl.TForm
trayicon *vcl.TTrayIcon
)
func main() {
// 异常捕获
defer func() {
err := recover()
if err != nil {
fmt.Println("Exception: ", err)
vcl.ShowMessage(err.(error).Error())
}
}()
fmt.Println("InheritsFrom:", vcl.Application.Is().Object())
fmt.Println("InheritsFrom:", vcl.Application.Is().Component())
fmt.Println("InheritsFrom:", vcl.Application.Is().Control())
fmt.Println(rtl.LibAbout())
guid := rtl.CreateGUID()
fmt.Println("guid:", guid)
guidstr := rtl.GUIDToString(guid)
fmt.Println("guidToStr:", guidstr)
fmt.Println("strToGUID: ", rtl.StringToGUID(guidstr))
fmt.Println("main")
vcl.Application.Initialize()
vcl.Application.SetOnException(func(sender vcl.IObject, e *vcl.Exception) {
fmt.Println("exception.")
})
vcl.Application.SetTitle("Hello World! 系统信息:" + version.OSVersion.ToString())
vcl.Application.SetMainFormOnTaskBar(true)
// 窗口自动根据系统绽放,默认为false
//vcl.Application.SetScaled(true)
mainForm = vcl.Application.CreateForm()
mainForm.SetWidth(800)
mainForm.SetHeight(600)
mainForm.SetOnClose(func(Sender vcl.IObject, Action *types.TCloseAction) {
fmt.Println("close")
})
// 窗口大小约束
mainForm.SetOnConstrainedResize(func(sender vcl.IObject, minWidth, minHeight, maxWidth, maxHeight *int32) {
*minWidth = 800
*minHeight = 600
*maxWidth = 800
*maxHeight = 600
})
mainForm.SetOnDestroy(func(sender vcl.IObject) {
fmt.Println("Form Destroy.")
})
fmt.Println("MainForm ClientRect: ", mainForm.ClientRect())
filename := vcl.Application.ExeName()
fmt.Println("application.ExeName: ", filename)
fmt.Println("path: ", rtl.ExtractFilePath(filename))
fmt.Println("fileExists: ", rtl.FileExists(filename))
mainForm.SetOnCloseQuery(func(Sender vcl.IObject, CanClose *bool) {
*CanClose = vcl.MessageDlg("是否退出?", types.MtInformation, types.MbYes, types.MbNo) == types.MrYes
fmt.Println("OnCloseQuery")
})
mainForm.SetCaption(vcl.Application.Title())
mainForm.EnabledMaximize(false)
mainForm.SetDoubleBuffered(true)
//mainForm.SetPosition(types.PoScreenCenter)
//mainForm.ScreenCenter()
mainForm.WorkAreaCenter()
mainForm.SetKeyPreview(true)
mainForm.SetOnKeyDown(func(Sender vcl.IObject, Key *types.Char, Shift types.TShiftState) {
fmt.Println(Shift.In(types.SsCtrl))
fmt.Println("key:", *Key)
})
mainForm.SetOnMouseDown(func(sender vcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
fmt.Println("Button:", button == types.MbLeft, ", X:", x, ", y:", y)
fmt.Println("OnMouseDown")
})
chk := vcl.NewCheckBox(mainForm)
chk.SetParent(mainForm)
chk.SetChecked(true)
chk.SetCaption("测试")
chk.SetLeft(1)
chk.SetTop(60)
chk.SetOnClick(func(vcl.IObject) {
fmt.Println("chk.Checked=", chk.Checked())
})
// action
action := vcl.NewAction(mainForm)
action.SetCaption("action1")
action.SetOnUpdate(func(sender vcl.IObject) {
vcl.AsAction(sender).SetEnabled(chk.Checked())
})
action.SetOnExecute(func(vcl.IObject) {
fmt.Println("action execute")
})
btn := vcl.NewButton(mainForm)
btn.SetParent(mainForm)
btn.SetBounds(250, 30, 90, 25)
btn.SetCaption("action")
btn.SetAction(action)
trayicon = vcl.NewTrayIcon(mainForm)
trayicon.SetIcon(vcl.Application.Icon()) //不设置会自动使用Application.Icon
trayicon.SetHint(mainForm.Caption())
trayicon.SetVisible(true)
trayicon.SetOnClick(func(vcl.IObject) {
trayicon.SetBalloonTitle("test")
trayicon.SetBalloonTimeout(10000)
trayicon.SetBalloonHint("我是提示正文啦")
trayicon.ShowBalloonHint()
fmt.Println("TrayIcon Click.")
})
// img
img := vcl.NewImage(mainForm)
img.SetBounds(132, 30, 156, 97)
img.SetParent(mainForm)
img.Picture().LoadFromFile("./imgs/1.jpg")
//img.SetStretch(true)
img.SetProportional(true)
// linklabel
linklbl := vcl.NewLinkLabel(mainForm)
linklbl.SetAlign(types.AlBottom)
linklbl.SetCaption("<a href=\"https://github.com/ying32/govcl\">govcl测试链接</a>")
linklbl.SetParent(mainForm)
linklbl.SetOnLinkClick(func(sender vcl.IObject, link string, linktype types.TSysLinkType) {
fmt.Println("link label: ", link, ", type: ", linktype)
rtl.SysOpen(link)
})
// menu
mainMenu := vcl.NewMainMenu(mainForm)
item := vcl.NewMenuItem(mainForm)
item.SetCaption("File(&F)")
mainMenu.Items().Add(item)
item2 := vcl.NewMenuItem(mainForm)
item2.SetCaption("MemoryStreamTest")
item2.SetOnClick(func(vcl.IObject) {
mem := vcl.NewMemoryStream()
defer mem.Free()
mem.Write([]byte("测试"))
mem.SaveToFile("test.txt")
mem.SetPosition(0)
n, bs := mem.Read(int32(mem.Size()))
fmt.Println("n:", n, ", bs:", bs, ", str:", string(bs))
})
item.Add(item2)
item2 = vcl.NewMenuItem(mainForm)
item2.SetCaption("Exit(&E)")
item2.SetShortCutFromString("Ctrl+Q")
item2.SetOnClick(func(vcl.IObject) {
mainForm.Close()
})
item.Add(item2)
// mainForm.EnabledMinimize(false)
// mainForm.EnabledSystemMenu(false)
button := vcl.NewButton(mainForm)
button.SetCaption("消息")
button.SetParent(mainForm)
button.SetOnClick(func(vcl.IObject) {
fmt.Println("button click")
vcl.ShowMessage("这是一个消息")
vcl.Application.MessageBox("Hello!", "Message", win.MB_YESNO+win.MB_ICONINFORMATION)
})
button.SetLeft(50)
button.SetTop(50)
button.SetAlign(types.AlRight)
edit := vcl.NewEdit(mainForm)
edit.SetParent(mainForm)
edit.SetLeft(1)
edit.SetTop(30)
edit.SetTextHint("测试")
edit.SetOnChange(func(vcl.IObject) {
fmt.Println("edit OnChange")
})
button2 := vcl.NewButton(mainForm)
button2.SetParent(mainForm)
button2.SetCaption("a")
button2.SetWidth(100)
button2.SetHeight(28)
button2.SetOnClick(func(vcl.IObject) {
fmt.Println("button2 click")
edit.SetText("Hello!")
fmt.Println("ScreenWidth:", vcl.Screen.Width(), ", ScreenHeight:", vcl.Screen.Height())
})
button2.SetAlign(types.AlTop)
combo := vcl.NewComboBox(mainForm)
combo.SetAlign(types.AlBottom)
combo.SetParent(mainForm)
combo.SetText("ffff")
combo.Items().Add("1")
combo.Items().Add("2")
combo.SetItemIndex(0)
combo.SetOnChange(func(vcl.IObject) {
if combo.ItemIndex() != -1 {
fmt.Println("combo Change: ", combo.Items().Strings(combo.ItemIndex()))
}
})
page := vcl.NewPageControl(mainForm)
page.SetParent(mainForm)
page.SetAlign(types.AlBottom)
sheet := vcl.NewTabSheet(mainForm)
sheet.SetPageControl(page)
sheet.SetCaption("第一页")
// 需要先将TabSheet设置了父窗口,TListView才可用,不然就会报错
lv1 := vcl.NewListView(mainForm)
lv1.SetAlign(types.AlClient)
lv1.SetParent(sheet)
lv1.SetViewStyle(types.VsReport)
lv1.SetRowSelect(true)
lv1.SetReadOnly(true)
lv1.SetGridLines(true)
col := lv1.Columns().Add()
col.SetCaption("序号")
col.SetWidth(100)
// 强制柱头宽,即使被调整也会被还原
col.SetMaxWidth(100)
col.SetMinWidth(100)
col = lv1.Columns().Add()
col.SetCaption("名称")
col.SetWidth(200)
col = lv1.Columns().Add()
col.SetCaption("内容")
col.SetWidth(200)
lv1.SetOnClick(func(vcl.IObject) {
if lv1.ItemIndex() != -1 {
item := lv1.Selected() // lv1.Items().Item(lv1.ItemIndex())
fmt.Println(item.Caption(),
item.SubItems().Strings(0),
item.SubItems().Strings(1))
}
})
lv1.Items().BeginUpdate()
for i := 1; i <= 50; i++ {
lstitem := lv1.Items().Add()
lstitem.SetCaption(fmt.Sprintf("%d", i))
lstitem.SubItems().Add(fmt.Sprintf("第%d", i))
lstitem.SubItems().Add(fmt.Sprintf("内容%d", i))
}
lv1.Items().EndUpdate()
sheet = vcl.NewTabSheet(mainForm)
sheet.SetCaption("第二页")
sheet.SetPageControl(page)
// -----------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)
tv1 := vcl.NewTreeView(mainForm)
tv1.SetAutoExpand(true)
tv1.SetParent(sheet)
tv1.SetAlign(types.AlClient)
// tv1.SetRightClickSelect(true)
tv1.SetOnClick(func(vcl.IObject) {
if tv1.SelectionCount() > 0 {
node := tv1.Selected()
fmt.Println("text:", node.Text(), ", index:", node.Index())
}
})
tv1.SetOnMouseDown(func(sender vcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
if button == types.MbRight {
node := tv1.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())
}
}
})
tv1.Items().BeginUpdate()
node := tv1.Items().AddChild(nil, "首个")
for i := 1; i <= 50; i++ {
tv1.Items().AddChild(node, fmt.Sprintf("Node%d", i))
}
node = tv1.Items().AddChild(nil, "第二个")
for i := 1; i <= 50; i++ {
tv1.Items().AddChild(node, fmt.Sprintf("Node%d", i))
}
tv1.Items().EndUpdate()
fmt.Println("Compoment Count:", mainForm.ComponentCount())
// mainForm.ScreenCenter()
lbl := vcl.NewLabel(mainForm)
lbl.SetCaption("标签")
lbl.SetAlign(types.AlBottom)
fmt.Println("InheritsFromControl:", mainForm.Is().Control())
fmt.Println("InheritsFromWinControl:", mainForm.Is().WinControl())
fmt.Println("InheritsFromComponent:", mainForm.Is().Component())
fmt.Println("InheritsFromWinControl:", lbl.Is().WinControl())
vcl.Application.Run()
}