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
+9 -8
View File
@@ -156,7 +156,8 @@ func createNativeWindow(opts *Options) (*window, error) {
if resErr != nil {
return nil, resErr
}
cfg := configForDC()
dpi := windows.GetSystemDPI()
cfg := configForDPI(dpi)
wr := windows.Rect{
Right: int32(cfg.Px(opts.Width)),
Bottom: int32(cfg.Px(opts.Height)),
@@ -276,14 +277,14 @@ func windowProc(hwnd syscall.Handle, msg uint32, wParam, lParam uintptr) uintptr
mm := (*windows.MinMaxInfo)(unsafe.Pointer(uintptr(lParam)))
if w.minmax.minWidth > 0 || w.minmax.minHeight > 0 {
mm.PtMinTrackSize = windows.Point{
w.minmax.minWidth+w.deltas.width,
w.minmax.minHeight+w.deltas.height,
w.minmax.minWidth + w.deltas.width,
w.minmax.minHeight + w.deltas.height,
}
}
if w.minmax.maxWidth > 0 || w.minmax.maxHeight > 0 {
mm.PtMaxTrackSize = windows.Point{
w.minmax.maxWidth+w.deltas.width,
w.minmax.maxHeight+w.deltas.height,
w.minmax.maxWidth + w.deltas.width,
w.minmax.maxHeight + w.deltas.height,
}
}
}
@@ -409,7 +410,8 @@ func (w *window) draw(sync bool) {
if w.width == 0 || w.height == 0 {
return
}
cfg := configForDC()
dpi := windows.GetWindowDPI(w.hwnd)
cfg := configForDPI(dpi)
w.minmax = getWindowConstraints(cfg, w.opts, w.deltas)
w.w.Event(FrameEvent{
FrameEvent: system.FrameEvent{
@@ -631,8 +633,7 @@ func convertKeyCode(code uintptr) (string, bool) {
return r, true
}
func configForDC() unit.Metric {
dpi := windows.GetSystemDPI()
func configForDPI(dpi int) unit.Metric {
const inchPrDp = 1.0 / 96.0
ppdp := float32(dpi) * inchPrDp
return unit.Metric{
+13 -1
View File
@@ -207,6 +207,7 @@ var (
_GetClientRect = user32.NewProc("GetClientRect")
_GetClipboardData = user32.NewProc("GetClipboardData")
_GetDC = user32.NewProc("GetDC")
_GetDpiForWindow = user32.NewProc("GetDpiForWindow")
_GetKeyState = user32.NewProc("GetKeyState")
_GetMessage = user32.NewProc("GetMessageW")
_GetMessageTime = user32.NewProc("GetMessageTime")
@@ -343,7 +344,7 @@ func getDpiForMonitor(hmonitor syscall.Handle, dpiType uint32) int {
// GetSystemDPI returns the effective DPI of the system.
func GetSystemDPI() int {
// Check for getDpiForMonitor, introduced in Windows 8.1.
// Check for GetDpiForMonitor, introduced in Windows 8.1.
if _GetDpiForMonitor.Find() == nil {
hmon := monitorFromPoint(Point{}, MONITOR_DEFAULTTOPRIMARY)
return getDpiForMonitor(hmon, MDT_EFFECTIVE_DPI)
@@ -377,6 +378,17 @@ func GetMessageTime() time.Duration {
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) {
r, _, err := _GlobalAlloc.Call(GHND, uintptr(size))
if r == 0 {