forked from wailsapp/wails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
149 lines (129 loc) · 3.54 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
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
/*
* Copyright (C) 2019 The Winc Authors. All Rights Reserved.
* Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
*/
package winc
import (
"fmt"
"syscall"
"unsafe"
"github.com/frankfang/wails/v2/internal/frontend/desktop/windows/winc/w32"
)
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 SetStyle(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 SetExStyle(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.LoadIconWithResourceID(instance, 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.LoadCursorWithResourceID(0, 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 RegisterWindowMessage(name string) uint32 {
n := syscall.StringToUTF16Ptr(name)
ret := w32.RegisterWindowMessage(n)
if ret == 0 {
panic(syscall.GetLastError())
}
return ret
}
func getMonitorInfo(hwnd w32.HWND) *w32.MONITORINFO {
currentMonitor := w32.MonitorFromWindow(hwnd, w32.MONITOR_DEFAULTTONEAREST)
var info w32.MONITORINFO
info.CbSize = uint32(unsafe.Sizeof(info))
w32.GetMonitorInfo(currentMonitor, &info)
return &info
}
func getWindowInfo(hwnd w32.HWND) *w32.WINDOWINFO {
var info w32.WINDOWINFO
info.CbSize = uint32(unsafe.Sizeof(info))
w32.GetWindowInfo(hwnd, &info)
return &info
}
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)
}
// ScaleWithDPI scales the pixels from the default DPI-Space (96) to the target DPI-Space.
func ScaleWithDPI(pixels int, dpi uint) int {
return (pixels * int(dpi)) / 96
}