-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindowScale.go
More file actions
222 lines (191 loc) · 5.54 KB
/
windowScale.go
File metadata and controls
222 lines (191 loc) · 5.54 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"flag"
"fmt"
"log"
"strings"
"syscall"
"unsafe"
)
var (
windowToMaxOpt = flag.String("windowName", "Visual Studio Code", "Pass the name of the window you wish to maximize over multiple monitors")
)
var (
user32 = syscall.MustLoadDLL("user32.dll")
procEnumWindows = user32.MustFindProc("EnumWindows")
procGetWindowTextW = user32.MustFindProc("GetWindowTextW")
procMoveWindow = user32.MustFindProc("MoveWindow")
procGetDesktopWindow = user32.MustFindProc("GetDesktopWindow")
procGetWindowRect = user32.MustFindProc("GetWindowRect")
procMonitorFromPoint = user32.MustFindProc("MonitorFromPoint")
procGetMonitorInfo = user32.MustFindProc("GetMonitorInfoA")
procEnumDisplayMonitors = user32.MustFindProc("EnumDisplayMonitors")
)
func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func EnumDisplayMonitors(enumFunc uintptr, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall6(procEnumDisplayMonitors.Addr(), 4, uintptr(0), uintptr(0), uintptr(enumFunc), uintptr(lparam), uintptr(0), uintptr(0))
if r1 == 0 {
if e1 != 0 {
err = error(e1)
}
}
return
}
func FindMonitors() (resultRect rect, err error) {
resultRect = rect{}
largestHeight := int32(0)
cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
monInfo, monInfoErr := GetMonitorInfo(h)
if monInfoErr != nil {
//ignore error
return 1
}
height := monInfo.rcWork.bottom - monInfo.rcWork.top
// width := monInfo.rcWork.right - monInfo.rcWork.left
if height > largestHeight {
largestHeight = height
resultRect.top = monInfo.rcWork.top
resultRect.bottom = monInfo.rcWork.bottom
resultRect.left = 0
resultRect.right = 0
}
if height == largestHeight {
if monInfo.rcWork.right > resultRect.right {
resultRect.right = monInfo.rcWork.right
}
if monInfo.rcWork.left < resultRect.left {
resultRect.left = monInfo.rcWork.left
}
}
fmt.Println(monInfo)
fmt.Println(resultRect)
return 1
})
EnumDisplayMonitors(cb, 0)
return
}
func FindWindow(title string) (syscall.Handle, error) {
var hwnd syscall.Handle
cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
b := make([]uint16, 200)
_, err := GetWindowText(h, &b[0], int32(len(b)))
if err != nil {
// ignore the error
return 1 // continue enumeration
}
fmt.Println(syscall.UTF16ToString(b))
if strings.Contains(syscall.UTF16ToString(b), title) {
// note the window
hwnd = h
return 0 // stop enumeration
}
return 1 // continue enumeration
})
EnumWindows(cb, 0)
if hwnd == 0 {
return 0, fmt.Errorf("No window with title '%s' found", title)
}
return hwnd, nil
}
func MoveWindow(hwnd syscall.Handle, x int, y int, nWidth int, nHeight int, bRepaint bool) (err error) {
_, _, e1 := syscall.Syscall6(procMoveWindow.Addr(), 6, uintptr(hwnd), uintptr(x), uintptr(y), uintptr(nWidth), uintptr(nHeight), 1)
if e1 != 0 {
err = error(e1)
}
return
}
func GetDesktopWindow() (hwnd syscall.Handle, err error) {
_, _, e1 := syscall.Syscall(procGetDesktopWindow.Addr(), 1, uintptr(hwnd), uintptr(0), uintptr(0))
if e1 != 0 {
err = error(e1)
}
return
}
type rect struct {
left int32
top int32
right int32
bottom int32
}
func GetWindowRect(hwnd syscall.Handle) (lprect rect, err error) {
lprect = rect{}
r0, _, e1 := syscall.Syscall(procGetWindowRect.Addr(), 2, uintptr(hwnd), uintptr(unsafe.Pointer(&lprect)), uintptr(0))
if r0 == 0 {
fmt.Println("blab")
}
if e1 != 0 {
fmt.Println("Flab")
err = error(e1)
}
return
}
type point struct {
x int32
y int32
}
func MonitorFromPoint(pt point, dwFlags uint16) (hmonitor syscall.Handle, err error) {
fmt.Println(pt)
h, _, e1 := syscall.Syscall(procMonitorFromPoint.Addr(), 2, uintptr(unsafe.Pointer(&pt)), uintptr(dwFlags), uintptr(0))
if e1 != 0 {
err = error(e1)
}
fmt.Println(h)
hmonitor = syscall.Handle(h)
return
}
type monitorinfo struct {
cbsize uint16
rcMonitor rect
rcWork rect
dwFlags uint16
}
func GetMonitorInfo(hmonitor syscall.Handle) (lpmonitorInfo monitorinfo, err error) {
lpmonitorInfo = monitorinfo{}
lpmonitorInfo.cbsize = uint16(unsafe.Sizeof(lpmonitorInfo))
_, _, e1 := syscall.Syscall(procGetMonitorInfo.Addr(), 2, uintptr(hmonitor), uintptr(unsafe.Pointer(&lpmonitorInfo)), uintptr(0))
if e1 != 0 {
err = error(e1)
}
return
}
func main() {
flag.Parse()
title := *windowToMaxOpt
h, err := FindWindow(title)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found '%s' window: handle=0x%x\n", title, h)
rect, fme := FindMonitors()
if fme != nil {
log.Fatal(fme)
}
fmt.Println("Resulting rect is", rect)
borderLen := 8
moveErr := MoveWindow(h, int(rect.left)-borderLen, int(rect.top), int(rect.right-rect.left)+2*borderLen, int(rect.bottom-rect.top)+borderLen, true)
if moveErr != nil {
log.Fatal(moveErr)
}
}