-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmisc_windows.go
95 lines (76 loc) · 2.19 KB
/
misc_windows.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
//go:build windows
package wx
import (
"math/rand"
"syscall"
"unsafe"
"fyne.io/fyne/v2"
)
var (
user32 = syscall.NewLazyDLL("user32.dll")
getWindowTextLengthW = user32.NewProc("GetWindowTextLengthW")
getWindowTextW = user32.NewProc("GetWindowTextW")
getForegroundWindow = user32.NewProc("GetForegroundWindow")
enumWindows = user32.NewProc("EnumWindows")
sendMessage = user32.NewProc("SendMessageW")
getDoubleClickTime = user32.NewProc("GetDoubleClickTime")
)
var hwnds map[fyne.Window]uintptr
func init() {
hwnds = make(map[fyne.Window]uintptr)
}
// GetHWND retreives the HWND (WINAPI Window Handle) of a fyne.Window.
func GetHWND(w fyne.Window) (hwnd uintptr) {
// check for stored HWND for this fyne.Window
if h, ok := hwnds[w]; ok {
return h
}
// generate a random 128 chars title
rndTitle := make([]byte, 128)
letterBytes := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for i := range rndTitle {
rndTitle[i] = letterBytes[rand.Int63()%int64(len(letterBytes))]
}
// keep original window title
oldTitle := w.Title()
defer w.SetTitle(oldTitle)
// set random title
w.SetTitle(string(rndTitle))
// fast check: check foreground window first
if h, _, _ := getForegroundWindow.Call(); h != 0 {
if sz, _, _ := getWindowTextLengthW.Call(h); sz == 128 {
buf := make([]uint16, sz+1)
getWindowTextW.Call(h, uintptr(unsafe.Pointer(&buf[0])), sz+1)
if string(rndTitle) == syscall.UTF16ToString(buf) {
hwnds[w] = h
return h
}
}
}
// lookup our window by the random title
enumWindows.Call(syscall.NewCallback(func(h, lparam uintptr) uintptr {
sz, _, _ := getWindowTextLengthW.Call(h)
if sz != 128 { // fast check: must be the same length
return 1
}
buf := make([]uint16, sz+1)
getWindowTextW.Call(h, uintptr(unsafe.Pointer(&buf[0])), sz+1)
if string(rndTitle) == syscall.UTF16ToString(buf) {
hwnd = h
return 0
}
return 1
}), 0)
// store HWND for further lookups
if hwnd != 0 {
hwnds[w] = hwnd
}
return
}
func MaximizeWindow(hwnd uintptr) {
sendMessage.Call(hwnd, 0x0112, 0xF030, 0) // WM_SYSCOMMAND SC_MAXIMIZE
}
func doubleClickTime() uint {
tm, _, _ := getDoubleClickTime.Call()
return uint(tm)
}