/
main.go
162 lines (133 loc) · 4.17 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
package main
import (
"fmt"
_ "github.com/ying32/govcl/pkgs/winappres"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/rtl"
"github.com/ying32/govcl/vcl/types"
"github.com/ying32/govcl/vcl/types/colors"
)
type TPoint struct {
X, Y int32
Down bool
}
var (
isMouseDown bool
points = make([]TPoint, 0)
)
func main() {
vcl.Application.Initialize()
vcl.Application.SetMainFormOnTaskBar(true)
jpgFileName := "./imgs/1.jpg"
canLoad := rtl.FileExists(jpgFileName)
var jpgimg *vcl.TJPEGImage
if canLoad {
jpgimg = vcl.NewJPEGImage()
jpgimg.LoadFromFile(jpgFileName)
}
mainForm := vcl.Application.CreateForm()
mainForm.SetCaption("Hello")
mainForm.SetPosition(types.PoScreenCenter)
mainForm.EnabledMaximize(false)
mainForm.SetWidth(600)
mainForm.SetHeight(600)
mainForm.SetDoubleBuffered(true)
mainForm.SetOnDestroy(func(sender vcl.IObject) {
if jpgimg != nil {
jpgimg.Free()
}
})
mainForm.SetOnPaint(func(vcl.IObject) {
canvas := mainForm.Canvas()
canvas.MoveTo(10, 10)
canvas.LineTo(50, 10)
s := "这是一段文字"
canvas.Font().SetColor(colors.ClRed) // red
canvas.Font().SetSize(20)
style := canvas.Font().Style()
canvas.Brush().SetStyle(types.BsClear)
canvas.Font().SetStyle(style.Include(types.FsBold, types.FsItalic))
canvas.TextOut(100, 30, s)
r := types.TRect{0, 0, 80, 80}
// 计算文字
//fmt.Println("TfSingleLine: ", types.TfSingleLine)
s = "由于现有第三方的Go UI库不是太庞大就是用的不习惯,或者组件太少。"
canvas.TextRect2(&r, s, types.NewSet(types.TfCenter, types.TfVerticalCenter, types.TfSingleLine))
//fmt.Println("r: ", r, ", s: ", s)
s = "测试输出"
r = types.TRect{0, 0, 80, 80}
// brush
canvas.Brush().SetColor(colors.ClGreen)
canvas.FillRect(r)
// font
canvas.Font().SetStyle(0)
canvas.Font().SetSize(9)
canvas.Font().SetColor(colors.ClBlue)
// pen
canvas.Pen().SetWidth(2)
canvas.Pen().SetColor(colors.ClFuchsia)
canvas.Rectangle(r.Left, r.Top, r.Right, r.Bottom)
textFmt := types.NewSet(types.TfCenter, types.TfSingleLine, types.TfVerticalCenter)
//fmt.Println("format: ", textFmt)
// canvas.TextRect(r, 0, 0, s)
canvas.TextRect2(&r, s, textFmt)
canvas.Draw(0, 80, jpgimg)
//canvas.Draw2(0, 200, jpgimg, 10)
// 画多边形
canvas.Brush().SetColor(colors.ClYellow)
canvas.Polygon([]types.TPoint{{15, 40}, {43, 123}, {81, 42}, {45, 11}})
canvas.Polyline([]types.TPoint{{15 + 100, 40}, {43 + 100, 123}, {81 + 100, 42}, {45 + 100, 11}})
})
paintbox := vcl.NewPaintBox(mainForm)
paintbox.SetParent(mainForm)
paintbox.SetAlign(types.AlBottom)
paintbox.SetHeight(mainForm.Height() - 280)
paintbox.SetOnPaint(func(vcl.IObject) {
canvas := paintbox.Canvas()
canvas.Pen().SetColor(colors.ClRed)
r := paintbox.ClientRect()
canvas.Rectangle(r.Left, r.Top, r.Right, r.Bottom)
canvas.Font().SetColor(colors.ClSkyblue)
rect := paintbox.ClientRect()
s := "在这可以用鼠标绘制"
textFmt := types.NewSet(types.TfCenter, types.TfSingleLine, types.TfVerticalCenter)
canvas.TextRect2(&rect, s, textFmt)
canvas.Pen().SetColor(colors.ClGreen)
for _, p := range points {
if p.Down {
canvas.MoveTo(p.X, p.Y)
} else {
canvas.LineTo(p.X, p.Y)
}
}
})
paintbox.SetOnMouseDown(func(sender vcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
fmt.Println("mouse down")
if button == types.MbLeft {
points = append(points, TPoint{X: x, Y: y, Down: true})
isMouseDown = true
}
})
paintbox.SetOnMouseMove(func(sender vcl.IObject, shift types.TShiftState, x, y int32) {
if isMouseDown {
points = append(points, TPoint{X: x, Y: y, Down: false})
paintbox.Repaint()
}
})
paintbox.SetOnMouseUp(func(sender vcl.IObject, button types.TMouseButton, shift types.TShiftState, x, y int32) {
fmt.Println("mouse SetOnMouseUp")
if button == types.MbLeft {
isMouseDown = false
}
})
btnClear := vcl.NewButton(mainForm)
btnClear.SetParent(mainForm)
btnClear.SetCaption("清除绘制")
btnClear.SetLeft(mainForm.Width() - btnClear.Width() - 20)
btnClear.SetTop(10)
btnClear.SetOnClick(func(vcl.IObject) {
points = make([]TPoint, 0)
paintbox.Repaint()
})
vcl.Application.Run()
}