forked from f0cii/gowi
/
window.go
185 lines (135 loc) · 3.48 KB
/
window.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
package gowi
import (
"fmt"
"github.com/nvsoft/win"
"syscall"
"unsafe"
)
// Window
type Window interface {
AsWindowBase() *WindowBase
InitWindow(window MainWindow, id uintptr)
Handle() win.HWND
GetWindowText() string
SetWindowText(title string)
SetIcon(iconType int, icon *Icon)
Show()
Hide()
ShowMinimized()
ShowMaximized()
ShowFullScreen()
ShowNormal()
IsEnabled() bool
IsVisible() bool
SetVisible(value bool)
SetEnabled(b bool)
SetDisabled(disable bool)
SetFocus()
Bounds() Rect
SetBounds(value Rect)
Close()
WndProc(msg uint32, wparam, lparam uintptr) uintptr
}
// WindowBase
type WindowBase struct {
hwnd win.HWND
parent win.HWND
idd uintptr
}
func (w *WindowBase) AsWindowBase() *WindowBase {
return w
}
// 设置窗口对应资源Idd
func (w *WindowBase) InitWindow(window MainWindow, id uintptr) {
w.idd = id
h := window.AsWindowBase().Handle()
fmt.Printf("h=%v\n", h)
w.hwnd = win.GetDlgItem(h, id)
fmt.Printf("w.hwnd=%v\n", w.hwnd)
}
func (w *WindowBase) Handle() win.HWND {
return w.hwnd
}
//func (w *WidgetBase) GetWindowText() string {
// return win.GetWindowText(w.hwnd)
//}
func (w *WindowBase) SetWindowText(title string) {
fmt.Printf("SetCaption hwnd: %v, %s\n", w.hwnd, title)
win.SetWindowText(w.hwnd, title)
}
func (w *WindowBase) GetWindowText() string {
textLength := win.SendMessage(w.hwnd, win.WM_GETTEXTLENGTH, 0, 0)
buf := make([]uint16, textLength+1)
win.SendMessage(w.hwnd, win.WM_GETTEXT, uintptr(textLength+1), uintptr(unsafe.Pointer(&buf[0])))
return syscall.UTF16ToString(buf)
}
//func (w *WidgetBase) SetWindowText(text string) {
// if win.TRUE != win.SendMessage(w.hwnd, win.WM_SETTEXT, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text)))) {
//return errors.New("WM_SETTEXT failed")
// }
//return nil
//}
// IconType: 1 - ICON_BIG; 0 - ICON_SMALL
func (w *WindowBase) SetIcon(iconType int, icon *Icon) {
if iconType > 1 {
panic("IconType is invalid")
}
win.SendMessage(w.hwnd, win.WM_SETICON, uintptr(iconType), uintptr(icon.Handle()))
}
func (w *WindowBase) Show() {
win.ShowWindow(w.hwnd, win.SW_SHOW)
}
func (w *WindowBase) Hide() {
win.ShowWindow(w.hwnd, win.SW_HIDE)
}
func (w *WindowBase) ShowMinimized() {
win.ShowWindow(w.hwnd, win.SW_MINIMIZE)
}
func (w *WindowBase) ShowMaximized() {
win.ShowWindow(w.hwnd, win.SW_MAXIMIZE)
}
func (w *WindowBase) ShowFullScreen() {
win.ShowWindow(w.hwnd, win.SW_SHOWMAXIMIZED)
}
func (w *WindowBase) ShowNormal() {
win.ShowWindow(w.hwnd, win.SW_SHOWNORMAL)
}
func (w *WindowBase) IsEnabled() bool {
return win.IsWindowEnabled(w.hwnd)
}
func (w *WindowBase) IsVisible() bool {
return win.IsWindowVisible(w.hwnd)
}
func (w *WindowBase) SetVisible(value bool) {
var cmd int32
if value {
cmd = win.SW_SHOW
} else {
cmd = win.SW_HIDE
}
win.ShowWindow(w.hwnd, cmd)
}
func (w *WindowBase) SetEnabled(b bool) {
win.EnableWindow(w.hwnd, b)
}
func (w *WindowBase) SetDisabled(disable bool) {
win.EnableWindow(w.hwnd, !disable)
}
func (w *WindowBase) Close() {
win.SendMessage(w.hwnd, win.WM_CLOSE, 0, 0)
}
func (w *WindowBase) SetFocus() {
win.SetFocus(w.hwnd)
}
func (w *WindowBase) Bounds() Rect {
var rect win.RECT
win.GetWindowRect(w.hwnd, &rect)
return rectFromRECT(rect)
}
func (w *WindowBase) SetBounds(value Rect) {
win.MoveWindow(w.hwnd, int32(value.X), int32(value.Y), int32(value.Width), int32(value.Height), true)
}
func (w *WindowBase) WndProc(msg uint32, wparam, lparam uintptr) uintptr {
fmt.Println("WidgetBase.WndProc")
return uintptr(0)
}