app/internal/window: use GetDpiForWindow if available

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-07-19 10:41:47 +02:00
parent ae07c5f470
commit 39fd161bea
2 changed files with 22 additions and 9 deletions
+5 -4
View File
@@ -156,7 +156,8 @@ func createNativeWindow(opts *Options) (*window, error) {
if resErr != nil { if resErr != nil {
return nil, resErr return nil, resErr
} }
cfg := configForDC() dpi := windows.GetSystemDPI()
cfg := configForDPI(dpi)
wr := windows.Rect{ wr := windows.Rect{
Right: int32(cfg.Px(opts.Width)), Right: int32(cfg.Px(opts.Width)),
Bottom: int32(cfg.Px(opts.Height)), Bottom: int32(cfg.Px(opts.Height)),
@@ -409,7 +410,8 @@ func (w *window) draw(sync bool) {
if w.width == 0 || w.height == 0 { if w.width == 0 || w.height == 0 {
return return
} }
cfg := configForDC() dpi := windows.GetWindowDPI(w.hwnd)
cfg := configForDPI(dpi)
w.minmax = getWindowConstraints(cfg, w.opts, w.deltas) w.minmax = getWindowConstraints(cfg, w.opts, w.deltas)
w.w.Event(FrameEvent{ w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{ FrameEvent: system.FrameEvent{
@@ -631,8 +633,7 @@ func convertKeyCode(code uintptr) (string, bool) {
return r, true return r, true
} }
func configForDC() unit.Metric { func configForDPI(dpi int) unit.Metric {
dpi := windows.GetSystemDPI()
const inchPrDp = 1.0 / 96.0 const inchPrDp = 1.0 / 96.0
ppdp := float32(dpi) * inchPrDp ppdp := float32(dpi) * inchPrDp
return unit.Metric{ return unit.Metric{
+13 -1
View File
@@ -207,6 +207,7 @@ var (
_GetClientRect = user32.NewProc("GetClientRect") _GetClientRect = user32.NewProc("GetClientRect")
_GetClipboardData = user32.NewProc("GetClipboardData") _GetClipboardData = user32.NewProc("GetClipboardData")
_GetDC = user32.NewProc("GetDC") _GetDC = user32.NewProc("GetDC")
_GetDpiForWindow = user32.NewProc("GetDpiForWindow")
_GetKeyState = user32.NewProc("GetKeyState") _GetKeyState = user32.NewProc("GetKeyState")
_GetMessage = user32.NewProc("GetMessageW") _GetMessage = user32.NewProc("GetMessageW")
_GetMessageTime = user32.NewProc("GetMessageTime") _GetMessageTime = user32.NewProc("GetMessageTime")
@@ -343,7 +344,7 @@ func getDpiForMonitor(hmonitor syscall.Handle, dpiType uint32) int {
// GetSystemDPI returns the effective DPI of the system. // GetSystemDPI returns the effective DPI of the system.
func GetSystemDPI() int { func GetSystemDPI() int {
// Check for getDpiForMonitor, introduced in Windows 8.1. // Check for GetDpiForMonitor, introduced in Windows 8.1.
if _GetDpiForMonitor.Find() == nil { if _GetDpiForMonitor.Find() == nil {
hmon := monitorFromPoint(Point{}, MONITOR_DEFAULTTOPRIMARY) hmon := monitorFromPoint(Point{}, MONITOR_DEFAULTTOPRIMARY)
return getDpiForMonitor(hmon, MDT_EFFECTIVE_DPI) return getDpiForMonitor(hmon, MDT_EFFECTIVE_DPI)
@@ -377,6 +378,17 @@ func GetMessageTime() time.Duration {
return time.Duration(r) * time.Millisecond return time.Duration(r) * time.Millisecond
} }
// GetWindowDPI returns the effective DPI of the window.
func GetWindowDPI(hwnd syscall.Handle) int {
// Check for GetDpiForWindow, introduced in Windows 10.
if _GetDpiForWindow.Find() == nil {
dpi, _, _ := _GetDpiForWindow.Call(uintptr(hwnd))
return int(dpi)
} else {
return GetSystemDPI()
}
}
func GlobalAlloc(size int) (syscall.Handle, error) { func GlobalAlloc(size int) (syscall.Handle, error) {
r, _, err := _GlobalAlloc.Call(GHND, uintptr(size)) r, _, err := _GlobalAlloc.Call(GHND, uintptr(size))
if r == 0 { if r == 0 {