Skip to content

Commit

Permalink
Merge pull request #37 from kbinani/win-GetDisplayBounds-202501
Browse files Browse the repository at this point in the history
Pin the argument object before calling enumDisplayMonitors

Co-authored-by: NanoRed <[email protected]>
  • Loading branch information
kbinani and red7x authored Jan 18, 2025
2 parents a8a2c5d + d6fdf5e commit a3924b7
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
16 changes: 0 additions & 16 deletions windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,6 @@ func Capture(x, y, width, height int) (*image.RGBA, error) {
return img, nil
}

func NumActiveDisplays() int {
var count int = 0
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(countupMonitorCallback), uintptr(unsafe.Pointer(&count)))
return count
}

func GetDisplayBounds(displayIndex int) image.Rectangle {
var ctx getMonitorBoundsContext
ctx.Index = displayIndex
ctx.Count = 0
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(getMonitorBoundsCallback), uintptr(unsafe.Pointer(&ctx)))
return image.Rect(
int(ctx.Rect.Left), int(ctx.Rect.Top),
int(ctx.Rect.Right), int(ctx.Rect.Bottom))
}

func getDesktopWindow() win.HWND {
ret, _, _ := syscall.Syscall(funcGetDesktopWindow, 0, 0, 0, 0)
return win.HWND(ret)
Expand Down
37 changes: 37 additions & 0 deletions windows_ge1.21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//go:build windows && go1.21

package screenshot

import (
"image"
"runtime"
"syscall"
"unsafe"

"github.com/lxn/win"
)

func NumActiveDisplays() int {
count := new(int)
pinner := new(runtime.Pinner)
pinner.Pin(count)
defer pinner.Unpin()
*count = 0
ptr := unsafe.Pointer(count)
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(countupMonitorCallback), uintptr(ptr))
return *count
}

func GetDisplayBounds(displayIndex int) image.Rectangle {
ctx := new(getMonitorBoundsContext)
pinner := new(runtime.Pinner)
pinner.Pin(ctx)
defer pinner.Unpin()
ctx.Index = displayIndex
ctx.Count = 0
ptr := unsafe.Pointer(ctx)
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(getMonitorBoundsCallback), uintptr(ptr))
return image.Rect(
int(ctx.Rect.Left), int(ctx.Rect.Top),
int(ctx.Rect.Right), int(ctx.Rect.Bottom))
}
30 changes: 30 additions & 0 deletions windows_lt1.21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build windows && !go1.21

package screenshot

import (
"image"
"syscall"
"unsafe"

"github.com/lxn/win"
)

func NumActiveDisplays() int {
var count int
count = 0
ptr := unsafe.Pointer(&count)
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(countupMonitorCallback), uintptr(ptr))
return count
}

func GetDisplayBounds(displayIndex int) image.Rectangle {
var ctx getMonitorBoundsContext
ctx.Index = displayIndex
ctx.Count = 0
ptr := unsafe.Pointer(&ctx)
enumDisplayMonitors(win.HDC(0), nil, syscall.NewCallback(getMonitorBoundsCallback), uintptr(ptr))
return image.Rect(
int(ctx.Rect.Left), int(ctx.Rect.Top),
int(ctx.Rect.Right), int(ctx.Rect.Bottom))
}

0 comments on commit a3924b7

Please sign in to comment.