ui: Bug fix: DeviceScaleFactor and MonitorSize were not correct on Windows

This commit is contained in:
Hajime Hoshi 2018-10-08 03:18:24 +09:00
parent 9264e38324
commit 96a657025f
3 changed files with 27 additions and 10 deletions

View File

@ -55,5 +55,5 @@ func currentMonitor() *glfw.Monitor {
return m
}
}
return nil
return glfw.GetPrimaryMonitor()
}

View File

@ -44,5 +44,5 @@ func currentMonitor() *glfw.Monitor {
return m
}
}
return nil
return glfw.GetPrimaryMonitor()
}

View File

@ -47,10 +47,11 @@ type monitorInfo struct {
var (
// user32 is defined at hideconsole_windows.go
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
procGetActiveWindow = user32.NewProc("GetActiveWindow")
procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
procGetSystemMetrics = user32.NewProc("GetSystemMetrics")
procGetActiveWindow = user32.NewProc("GetActiveWindow")
procGetForegroundWindow = user32.NewProc("GetForegroundWindow")
procMonitorFromWindow = user32.NewProc("MonitorFromWindow")
procGetMonitorInfoW = user32.NewProc("GetMonitorInfoW")
)
func getSystemMetrics(nIndex int) (int, error) {
@ -69,6 +70,14 @@ func getActiveWindow() (uintptr, error) {
return r, nil
}
func getForegroundWindow() (uintptr, error) {
r, _, e := syscall.Syscall(procGetForegroundWindow.Addr(), 0, 0, 0, 0)
if e != 0 {
return 0, fmt.Errorf("ui: GetForegroundWindow failed: error code: %d", e)
}
return r, nil
}
func monitorFromWindow(hwnd uintptr, dwFlags uint32) (uintptr, error) {
r, _, e := syscall.Syscall(procMonitorFromWindow.Addr(), 2, hwnd, uintptr(dwFlags), 0)
if e != 0 {
@ -118,9 +127,17 @@ func currentMonitor() *glfw.Monitor {
panic(err)
}
if w == 0 {
// There is no window at launching.
// TODO: Use glfw.GetCurrentContext() like currentMonitor() in ui_unix.go.
return glfw.GetPrimaryMonitor()
// There is no window at launching, but there is a hidden initialized window.
// Get the foreground window, that is common among multiple processes.
w, err = getForegroundWindow()
if err != nil {
panic(err)
}
if w == 0 {
// GetForegroundWindow can return null according to the document. Use
// the primary monitor instead.
return glfw.GetPrimaryMonitor()
}
}
// Get the current monitor by the window handle instead of the window position. It is because the window
@ -144,5 +161,5 @@ func currentMonitor() *glfw.Monitor {
return m
}
}
return nil
return glfw.GetPrimaryMonitor()
}