LoadWindowPosition has a better default position

LoadWindowPosition now positions the window on the nearest monitor if
the window is not inside a monitor.
This commit is contained in:
mmbob 2013-05-02 13:00:13 -04:00
parent 7bc321b176
commit df14a77124

View File

@ -558,40 +558,47 @@ bool LoadWindowPosition()
int windowW = rcWindow.right - rcWindow.left - 1;
int windowH = rcWindow.bottom - rcWindow.top - 1;
int windowX = Client::Ref().GetPrefInteger("WindowX", INT_MAX);
int windowY = Client::Ref().GetPrefInteger("WindowY", INT_MAX);
int savedWindowX = Client::Ref().GetPrefInteger("WindowX", INT_MAX);
int savedWindowY = Client::Ref().GetPrefInteger("WindowY", INT_MAX);
bool setDefaultPos = true;
// Center the window on the primary desktop by default
int newWindowX = (desktopWidth - windowW) / 2;
int newWindowY = (desktopHeight - windowH) / 2;
if (windowX != INT_MAX && windowY != INT_MAX)
bool success = false;
if (savedWindowX != INT_MAX && savedWindowY != INT_MAX)
{
POINT windowPoints[] = {
{windowX, windowY}, // Top-left
{windowX + windowW, windowY + windowH} // Bottom-right
{savedWindowX, savedWindowY}, // Top-left
{savedWindowX + windowW, savedWindowY + windowH} // Bottom-right
};
MONITORINFO monitor;
monitor.cbSize = sizeof(monitor);
if (GetMonitorInfo(MonitorFromPoint(windowPoints[0], MONITOR_DEFAULTTOPRIMARY), &monitor) != 0)
if (GetMonitorInfo(MonitorFromPoint(windowPoints[0], MONITOR_DEFAULTTONEAREST), &monitor) != 0)
{
// Only use the saved window position if it lies inside the visible screen
if (PtInRect(&monitor.rcMonitor, windowPoints[0]) && PtInRect(&monitor.rcMonitor, windowPoints[1]))
{
SetWindowPos(sysInfo.window, 0, windowX, windowY, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
newWindowX = savedWindowX;
newWindowY = savedWindowY;
setDefaultPos = false;
success = true;
}
}
}
if (setDefaultPos)
else
{
// Center the window on the primary desktop by default
SetWindowPos(sysInfo.window, 0, (desktopWidth - windowW) / 2, (desktopHeight - windowH) / 2, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
// Center the window on the nearest monitor
newWindowX = monitor.rcMonitor.left + (monitor.rcMonitor.right - monitor.rcMonitor.left - windowW) / 2;
newWindowY = monitor.rcMonitor.top + (monitor.rcMonitor.bottom - monitor.rcMonitor.top - windowH) / 2;
}
}
}
SetWindowPos(sysInfo.window, 0, newWindowX, newWindowY, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
// True if we didn't use the default, i.e. the position was valid
return !setDefaultPos;
return success;
}
return false;