-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathwindows.base.go
192 lines (161 loc) · 4.72 KB
/
windows.base.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
package wingui
import (
"syscall"
"unsafe"
"github.com/lxn/win"
)
// WindowBase is an interface that provides operations common to all windows.
// if need subclass window ,must set Subclassing to true.
// hwnd,if custom window could set by Init.
// idd Reource Id. if custom window could set zero by Init
type WindowBase struct {
hwnd win.HWND
idd uintptr
// lpPrevWndFunc is a WndProc point if Subclassing set to true.
lpPrevWndFunc uintptr
// Subclassing indicate that this window need Subclass,
// make sure set this flag before bind to Dialog.
Subclassing bool
}
// AsWindowBase return a *WindowBase.
func (w *WindowBase) AsWindowBase() *WindowBase {
return w
}
// Init could init new WindowBase by youself .
func (w *WindowBase) Init(hwnd win.HWND, idd uintptr) {
w.hwnd = hwnd
w.idd = idd
}
// Handle get hwnd.
func (w *WindowBase) Handle() win.HWND {
return w.hwnd
}
// SetWindowText set text
func (w *WindowBase) SetWindowText(title string) {
// log.Printf("SetCaption hwnd: %v, %s\n", w.hwnd, title)
//win.SetWindowText(w.hwnd, title)
win.SendMessage(w.hwnd, win.WM_SETTEXT, 0, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))))
}
// GetWindowText get text.
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)
}
// Text alias to GetWindowText
func (w *WindowBase) Text() string {
return w.GetWindowText()
}
// SetText alias to SetWindowText
func (w *WindowBase) SetText(str string) {
w.SetWindowText(str)
}
// SetIcon set window icon.
// IconType: 1 - ICON_BIG; 0 - ICON_SMALL
// Icon: Resource Id or Icon Handle
// LoadIcon: If Icon is ResourceId then invoke LoadIcon
func (w *WindowBase) SetIcon(iconType int, icon uintptr, loadIcon bool) {
if iconType > 1 {
panic("IconType is invalid")
}
if loadIcon {
icon = uintptr(win.LoadIcon(hInstance, win.MAKEINTRESOURCE(icon)))
}
win.SendMessage(w.hwnd, win.WM_SETICON, uintptr(iconType), icon)
}
// Show set window show.
func (w *WindowBase) Show() {
win.ShowWindow(w.hwnd, win.SW_SHOW)
}
//Hide set window hide.
func (w *WindowBase) Hide() {
win.ShowWindow(w.hwnd, win.SW_HIDE)
}
//ShowMinimized show minimized btn.
func (w *WindowBase) ShowMinimized() {
win.ShowWindow(w.hwnd, win.SW_MINIMIZE)
}
//ShowMaximized show maximized btn.
func (w *WindowBase) ShowMaximized() {
win.ShowWindow(w.hwnd, win.SW_MAXIMIZE)
}
//ShowFullScreen ShowFullScreen
func (w *WindowBase) ShowFullScreen() {
win.ShowWindow(w.hwnd, win.SW_SHOWMAXIMIZED)
}
// ShowNormal ShowNormal
func (w *WindowBase) ShowNormal() {
win.ShowWindow(w.hwnd, win.SW_SHOWNORMAL)
}
//IsEnabled check windows is enabled.
func (w *WindowBase) IsEnabled() bool {
return win.IsWindowEnabled(w.hwnd)
}
// IsVisible check window is visible.
func (w *WindowBase) IsVisible() bool {
return win.IsWindowVisible(w.hwnd)
}
// SetVisible set window visible status.
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)
}
// SetEnabled set window enable status.
func (w *WindowBase) SetEnabled(b bool) {
win.EnableWindow(w.hwnd, b)
}
// SetDisabled reverse of SetEnabled
func (w *WindowBase) SetDisabled(disable bool) {
win.EnableWindow(w.hwnd, !disable)
}
//Close close window.
func (w *WindowBase) Close() {
win.SendMessage(w.hwnd, win.WM_CLOSE, 0, 0)
}
// SetFocus set focus.
func (w *WindowBase) SetFocus() {
win.SetFocus(w.hwnd)
}
//GetWindowRect get window rect
func (w *WindowBase) GetWindowRect() win.RECT {
var rect win.RECT
win.GetWindowRect(w.hwnd, &rect)
return rect
}
// BoundsPixels returns the outer bounding box Rectangle of the *WindowBase, including
// decorations.
// The coordinates are relative to the screen.
func (w *WindowBase) BoundsPixels() Rectangle {
var r win.RECT
if !win.GetWindowRect(w.hwnd, &r) {
return Rectangle{}
}
return Rectangle{
int(r.Left),
int(r.Top),
int(r.Right - r.Left),
int(r.Bottom - r.Top),
}
}
// SetBounds set window rect.
func (w *WindowBase) SetBounds(value Rectangle) {
win.MoveWindow(w.hwnd, int32(value.X), int32(value.Y), int32(value.Width), int32(value.Height), true)
}
// WndProc process window message.
func (w *WindowBase) WndProc(msg uint32, wParam, lParam uintptr) uintptr {
// log.Println("WidgetBase.WndProc")
if w.lpPrevWndFunc != 0 {
return win.CallWindowProc(w.lpPrevWndFunc, w.hwnd, msg, wParam, lParam)
}
return uintptr(0)
}
// SendMessage sends a message to the window and returns the result.
func (w *WindowBase) SendMessage(msg uint32, wParam, lParam uintptr) uintptr {
return win.SendMessage(w.hwnd, msg, wParam, lParam)
}