39 lines
1.5 KiB
Go
Executable File
39 lines
1.5 KiB
Go
Executable File
// Package dpi declares this process as per-monitor DPI aware.
|
|
//
|
|
// Without this, Windows treats the process as DPI-unaware and silently
|
|
// virtualizes coordinates for GDI calls and window messages to a 96-DPI
|
|
// space, scaling the actual bitmap behind the scenes. That virtualization
|
|
// is what causes mouse clicks to land in the wrong place whenever the
|
|
// client and host are on displays with different resolutions or scale
|
|
// factors: capture, painting, and input event coordinates stop agreeing on
|
|
// what a "pixel" is. Declaring awareness up front makes every GetSystemMetrics/
|
|
// BitBlt/GetClientRect/mouse-message coordinate a real physical pixel on
|
|
// both ends, which is what the rest of this codebase assumes.
|
|
package dpi
|
|
|
|
import "golang.org/x/sys/windows"
|
|
|
|
var (
|
|
shcore = windows.NewLazySystemDLL("shcore.dll")
|
|
user32 = windows.NewLazySystemDLL("user32.dll")
|
|
|
|
procSetProcessDpiAwareness = shcore.NewProc("SetProcessDpiAwareness")
|
|
procSetProcessDPIAware = user32.NewProc("SetProcessDPIAware")
|
|
)
|
|
|
|
// PROCESS_PER_MONITOR_DPI_AWARE
|
|
const perMonitorDPIAware = 2
|
|
|
|
// MakeAware must be called once at process start, before any window,
|
|
// screen-capture, or input call. It prefers per-monitor awareness
|
|
// (Windows 8.1+) and falls back to system-DPI awareness on older systems.
|
|
func MakeAware() {
|
|
if err := procSetProcessDpiAwareness.Find(); err == nil {
|
|
procSetProcessDpiAwareness.Call(uintptr(perMonitorDPIAware))
|
|
return
|
|
}
|
|
if err := procSetProcessDPIAware.Find(); err == nil {
|
|
procSetProcessDPIAware.Call()
|
|
}
|
|
}
|