mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-05 01:15:35 +00:00
app/internal/window: implement GetDpiForMonitor fallback for < Win 8.1
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -79,6 +79,8 @@ const (
|
|||||||
|
|
||||||
_INFINITE = 0xFFFFFFFF
|
_INFINITE = 0xFFFFFFFF
|
||||||
|
|
||||||
|
_LOGPIXELSX = 88
|
||||||
|
|
||||||
_MDT_EFFECTIVE_DPI = 0
|
_MDT_EFFECTIVE_DPI = 0
|
||||||
|
|
||||||
_MONITOR_DEFAULTTOPRIMARY = 1
|
_MONITOR_DEFAULTTOPRIMARY = 1
|
||||||
@@ -201,12 +203,7 @@ func NewWindow(window Callbacks, opts *Options) error {
|
|||||||
|
|
||||||
func createNativeWindow(opts *Options) (*window, error) {
|
func createNativeWindow(opts *Options) (*window, error) {
|
||||||
setProcessDPIAware()
|
setProcessDPIAware()
|
||||||
screenDC, err := getDC(0)
|
cfg := configForDC()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cfg := configForDC(screenDC)
|
|
||||||
releaseDC(screenDC)
|
|
||||||
hInst, err := getModuleHandle()
|
hInst, err := getModuleHandle()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -412,7 +409,7 @@ func (w *window) draw(sync bool) {
|
|||||||
getClientRect(w.hwnd, &r)
|
getClientRect(w.hwnd, &r)
|
||||||
w.width = int(r.right - r.left)
|
w.width = int(r.right - r.left)
|
||||||
w.height = int(r.bottom - r.top)
|
w.height = int(r.bottom - r.top)
|
||||||
cfg := configForDC(w.hdc)
|
cfg := configForDC()
|
||||||
cfg.now = time.Now()
|
cfg.now = time.Now()
|
||||||
w.w.Event(FrameEvent{
|
w.w.Event(FrameEvent{
|
||||||
FrameEvent: system.FrameEvent{
|
FrameEvent: system.FrameEvent{
|
||||||
@@ -483,9 +480,8 @@ func convertKeyCode(code uintptr) (rune, bool) {
|
|||||||
return r, true
|
return r, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func configForDC(hdc syscall.Handle) config {
|
func configForDC() config {
|
||||||
hmon := monitorFromPoint(point{}, _MONITOR_DEFAULTTOPRIMARY)
|
dpi := getSystemDPI()
|
||||||
dpi := getDpiForMonitor(hmon, _MDT_EFFECTIVE_DPI)
|
|
||||||
const inchPrDp = 1.0 / 96.0
|
const inchPrDp = 1.0 / 96.0
|
||||||
ppdp := float32(dpi) * inchPrDp
|
ppdp := float32(dpi) * inchPrDp
|
||||||
return config{
|
return config{
|
||||||
@@ -533,6 +529,9 @@ var (
|
|||||||
|
|
||||||
shcore = syscall.NewLazySystemDLL("shcore")
|
shcore = syscall.NewLazySystemDLL("shcore")
|
||||||
_GetDpiForMonitor = shcore.NewProc("GetDpiForMonitor")
|
_GetDpiForMonitor = shcore.NewProc("GetDpiForMonitor")
|
||||||
|
|
||||||
|
gdi32 = syscall.NewLazySystemDLL("gdi32")
|
||||||
|
_GetDeviceCaps = gdi32.NewProc("GetDeviceCaps")
|
||||||
)
|
)
|
||||||
|
|
||||||
func getModuleHandle() (syscall.Handle, error) {
|
func getModuleHandle() (syscall.Handle, error) {
|
||||||
@@ -601,12 +600,34 @@ func getDC(hwnd syscall.Handle) (syscall.Handle, error) {
|
|||||||
return syscall.Handle(hdc), nil
|
return syscall.Handle(hdc), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getDeviceCaps(hdc syscall.Handle, index int32) int {
|
||||||
|
c, _, _ := _GetDeviceCaps.Call(uintptr(hdc), uintptr(index))
|
||||||
|
return int(c)
|
||||||
|
}
|
||||||
|
|
||||||
func getDpiForMonitor(hmonitor syscall.Handle, dpiType uint32) int {
|
func getDpiForMonitor(hmonitor syscall.Handle, dpiType uint32) int {
|
||||||
var dpiX, dpiY uintptr
|
var dpiX, dpiY uintptr
|
||||||
_GetDpiForMonitor.Call(uintptr(hmonitor), uintptr(dpiType), uintptr(unsafe.Pointer(&dpiX)), uintptr(unsafe.Pointer(&dpiY)))
|
_GetDpiForMonitor.Call(uintptr(hmonitor), uintptr(dpiType), uintptr(unsafe.Pointer(&dpiX)), uintptr(unsafe.Pointer(&dpiY)))
|
||||||
return int(dpiX)
|
return int(dpiX)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getSystemDPI returns the effective DPI of the system.
|
||||||
|
func getSystemDPI() int {
|
||||||
|
// Check for GetDpiForMonitor, introduced in Windows 8.1.
|
||||||
|
if _GetDpiForMonitor.Find() == nil {
|
||||||
|
hmon := monitorFromPoint(point{}, _MONITOR_DEFAULTTOPRIMARY)
|
||||||
|
return getDpiForMonitor(hmon, _MDT_EFFECTIVE_DPI)
|
||||||
|
} else {
|
||||||
|
// Fall back to the physical device DPI.
|
||||||
|
screenDC, err := getDC(0)
|
||||||
|
if err != nil {
|
||||||
|
return 96
|
||||||
|
}
|
||||||
|
defer releaseDC(screenDC)
|
||||||
|
return getDeviceCaps(screenDC, _LOGPIXELSX)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getKeyState(nVirtKey int32) int16 {
|
func getKeyState(nVirtKey int32) int16 {
|
||||||
c, _, _ := _GetKeyState.Call(uintptr(nVirtKey))
|
c, _, _ := _GetKeyState.Call(uintptr(nVirtKey))
|
||||||
return int16(c)
|
return int16(c)
|
||||||
|
|||||||
Reference in New Issue
Block a user