forked from lxn/walk
/
externalwidgets.go
131 lines (101 loc) · 2.46 KB
/
externalwidgets.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
// Copyright 2012 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"github.com/lxn/win"
)
const myWidgetWindowClass = "MyWidget Class"
func init() {
walk.AppendToWalkInit(func() {
walk.MustRegisterWindowClass(myWidgetWindowClass)
})
}
func main() {
var mw *walk.MainWindow
if err := (MainWindow{
AssignTo: &mw,
Title: "Walk External Widgets Example",
Size: Size{400, 300},
Layout: HBox{},
}).Create(); err != nil {
log.Fatal(err)
}
for _, name := range []string{"a", "b", "c"} {
if w, err := NewMyWidget(mw); err != nil {
log.Fatal(err)
} else {
w.SetName(name)
}
}
mpb, err := NewMyPushButton(mw)
if err != nil {
log.Fatal(err)
}
mpb.SetText("MyPushButton")
mw.Run()
}
type MyWidget struct {
walk.WidgetBase
}
func NewMyWidget(parent walk.Container) (*MyWidget, error) {
w := new(MyWidget)
if err := walk.InitWidget(
w,
parent,
myWidgetWindowClass,
win.WS_VISIBLE,
0); err != nil {
return nil, err
}
bg, err := walk.NewSolidColorBrush(walk.RGB(0, 255, 0))
if err != nil {
return nil, err
}
w.SetBackground(bg)
return w, nil
}
func (*MyWidget) CreateLayoutItem(ctx *walk.LayoutContext) walk.LayoutItem {
return &myWidgetLayoutItem{idealSize: walk.SizeFrom96DPI(walk.Size{50, 50}, ctx.DPI())}
}
type myWidgetLayoutItem struct {
walk.LayoutItemBase
idealSize walk.Size // in native pixels
}
func (li *myWidgetLayoutItem) LayoutFlags() walk.LayoutFlags {
return 0
}
func (li *myWidgetLayoutItem) IdealSize() walk.Size {
return li.idealSize
}
func (w *MyWidget) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_LBUTTONDOWN:
log.Printf("%s: WM_LBUTTONDOWN", w.Name())
}
return w.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
}
type MyPushButton struct {
*walk.PushButton
}
func NewMyPushButton(parent walk.Container) (*MyPushButton, error) {
pb, err := walk.NewPushButton(parent)
if err != nil {
return nil, err
}
mpb := &MyPushButton{pb}
if err := walk.InitWrapperWindow(mpb); err != nil {
return nil, err
}
return mpb, nil
}
func (mpb *MyPushButton) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case win.WM_LBUTTONDOWN:
log.Printf("%s: WM_LBUTTONDOWN", mpb.Text())
}
return mpb.PushButton.WndProc(hwnd, msg, wParam, lParam)
}