Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pin the argument object before calling enumDisplayMonitors #37

Merged
merged 3 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
Loading