-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from kbinani/win-GetDisplayBounds-202501
Pin the argument object before calling enumDisplayMonitors Co-authored-by: NanoRed <[email protected]>
- Loading branch information
Showing
3 changed files
with
67 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |