-
Notifications
You must be signed in to change notification settings - Fork 45
/
utils.go
115 lines (100 loc) · 2.62 KB
/
utils.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
package gform
import (
"fmt"
"github.com/AllenDang/w32"
"syscall"
"unsafe"
)
func internalTrackMouseEvent(hwnd w32.HWND) {
var tme w32.TRACKMOUSEEVENT
tme.CbSize = uint32(unsafe.Sizeof(tme))
tme.DwFlags = w32.TME_LEAVE
tme.HwndTrack = hwnd
tme.DwHoverTime = w32.HOVER_DEFAULT
w32.TrackMouseEvent(&tme)
}
func ToggleStyle(hwnd w32.HWND, b bool, style int) {
originalStyle := int(w32.GetWindowLongPtr(hwnd, w32.GWL_STYLE))
if originalStyle != 0 {
if b {
originalStyle |= style
} else {
originalStyle ^= style
}
w32.SetWindowLongPtr(hwnd, w32.GWL_STYLE, uintptr(originalStyle))
}
}
func ToggleExStyle(hwnd w32.HWND, b bool, style int) {
originalStyle := int(w32.GetWindowLongPtr(hwnd, w32.GWL_EXSTYLE))
if originalStyle != 0 {
if b {
originalStyle |= style
} else {
originalStyle ^= style
}
w32.SetWindowLongPtr(hwnd, w32.GWL_EXSTYLE, uintptr(originalStyle))
}
}
func CreateWindow(className string, parent Controller, exStyle, style uint) w32.HWND {
instance := GetAppInstance()
var parentHwnd w32.HWND
if parent != nil {
parentHwnd = parent.Handle()
}
var hwnd w32.HWND
hwnd = w32.CreateWindowEx(
exStyle,
syscall.StringToUTF16Ptr(className),
nil,
style,
w32.CW_USEDEFAULT,
w32.CW_USEDEFAULT,
w32.CW_USEDEFAULT,
w32.CW_USEDEFAULT,
parentHwnd,
0,
instance,
nil)
if hwnd == 0 {
errStr := fmt.Sprintf("Error occurred in CreateWindow(%s, %v, %d, %d)", className, parent, exStyle, style)
panic(errStr)
}
return hwnd
}
func RegisterClass(className string, wndproc uintptr) {
instance := GetAppInstance()
icon := w32.LoadIcon(instance, w32.MakeIntResource(w32.IDI_APPLICATION))
var wc w32.WNDCLASSEX
wc.Size = uint32(unsafe.Sizeof(wc))
wc.Style = w32.CS_HREDRAW | w32.CS_VREDRAW
wc.WndProc = wndproc
wc.Instance = instance
wc.Background = w32.COLOR_BTNFACE + 1
wc.Icon = icon
wc.Cursor = w32.LoadCursor(0, w32.MakeIntResource(w32.IDC_ARROW))
wc.ClassName = syscall.StringToUTF16Ptr(className)
wc.MenuName = nil
wc.IconSm = icon
if ret := w32.RegisterClassEx(&wc); ret == 0 {
panic(syscall.GetLastError())
}
}
func RegClassOnlyOnce(className string) {
isExists := false
for _, class := range gRegisteredClasses {
if class == className {
isExists = true
break
}
}
if !isExists {
RegisterClass(className, GeneralWndprocCallBack)
gRegisteredClasses = append(gRegisteredClasses, className)
}
}
func ScreenToClientRect(hwnd w32.HWND, rect *w32.RECT) *Rect {
l, t, r, b := rect.Left, rect.Top, rect.Right, rect.Bottom
l1, t1, _ := w32.ScreenToClient(hwnd, int(l), int(t))
r1, b1, _ := w32.ScreenToClient(hwnd, int(r), int(b))
return NewRect(l1, t1, r1, b1)
}