Skip to content

Commit a3924b7

Browse files
kbinanired7x
andauthored
Merge pull request #37 from kbinani/win-GetDisplayBounds-202501
Pin the argument object before calling enumDisplayMonitors Co-authored-by: NanoRed <[email protected]>
2 parents a8a2c5d + d6fdf5e commit a3924b7

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

windows.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,6 @@ func Capture(x, y, width, height int) (*image.RGBA, error) {
9595
return img, nil
9696
}
9797

98-
func NumActiveDisplays() int {
99-
var count int = 0
100-
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(countupMonitorCallback), uintptr(unsafe.Pointer(&count)))
101-
return count
102-
}
103-
104-
func GetDisplayBounds(displayIndex int) image.Rectangle {
105-
var ctx getMonitorBoundsContext
106-
ctx.Index = displayIndex
107-
ctx.Count = 0
108-
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(getMonitorBoundsCallback), uintptr(unsafe.Pointer(&ctx)))
109-
return image.Rect(
110-
int(ctx.Rect.Left), int(ctx.Rect.Top),
111-
int(ctx.Rect.Right), int(ctx.Rect.Bottom))
112-
}
113-
11498
func getDesktopWindow() win.HWND {
11599
ret, _, _ := syscall.Syscall(funcGetDesktopWindow, 0, 0, 0, 0)
116100
return win.HWND(ret)

windows_ge1.21.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//go:build windows && go1.21
2+
3+
package screenshot
4+
5+
import (
6+
"image"
7+
"runtime"
8+
"syscall"
9+
"unsafe"
10+
11+
"github.com/lxn/win"
12+
)
13+
14+
func NumActiveDisplays() int {
15+
count := new(int)
16+
pinner := new(runtime.Pinner)
17+
pinner.Pin(count)
18+
defer pinner.Unpin()
19+
*count = 0
20+
ptr := unsafe.Pointer(count)
21+
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(countupMonitorCallback), uintptr(ptr))
22+
return *count
23+
}
24+
25+
func GetDisplayBounds(displayIndex int) image.Rectangle {
26+
ctx := new(getMonitorBoundsContext)
27+
pinner := new(runtime.Pinner)
28+
pinner.Pin(ctx)
29+
defer pinner.Unpin()
30+
ctx.Index = displayIndex
31+
ctx.Count = 0
32+
ptr := unsafe.Pointer(ctx)
33+
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(getMonitorBoundsCallback), uintptr(ptr))
34+
return image.Rect(
35+
int(ctx.Rect.Left), int(ctx.Rect.Top),
36+
int(ctx.Rect.Right), int(ctx.Rect.Bottom))
37+
}

windows_lt1.21.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build windows && !go1.21
2+
3+
package screenshot
4+
5+
import (
6+
"image"
7+
"syscall"
8+
"unsafe"
9+
10+
"github.com/lxn/win"
11+
)
12+
13+
func NumActiveDisplays() int {
14+
var count int
15+
count = 0
16+
ptr := unsafe.Pointer(&count)
17+
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(countupMonitorCallback), uintptr(ptr))
18+
return count
19+
}
20+
21+
func GetDisplayBounds(displayIndex int) image.Rectangle {
22+
var ctx getMonitorBoundsContext
23+
ctx.Index = displayIndex
24+
ctx.Count = 0
25+
ptr := unsafe.Pointer(&ctx)
26+
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(getMonitorBoundsCallback), uintptr(ptr))
27+
return image.Rect(
28+
int(ctx.Rect.Left), int(ctx.Rect.Top),
29+
int(ctx.Rect.Right), int(ctx.Rect.Bottom))
30+
}

0 commit comments

Comments
 (0)