Handle WM_GETDPISCALEDSIZE

By handling WM_GETDPISCALEDSIZE we can keep QWindow’s
device independent size constant across DPI changes.
This is done by scaling QPlatformWindow’s native size
such that the change of scale factor and change of
QPlatformWindow size cancels out.

Qt now handles DPI change using two events:

WM_GETDPISCALEDSIZE: Compute the new size for the window.
WM_DPICHANGED: Apply the new DPI and window geometry.

The reason for this complication is that Windows retains
control over the window position during the DPI change,
in order to e.g. accurately track the cursor position
during a screen change.

The default WM_GETDPISCALEDSIZE implementation (provided
by Windows) scales the win32 window size linearly with
the DPI change. We want to use linear scaling as well,
however the win32 window size includes the margins, which
do not change linearly as the DPI changes.

Instead, scale the QPlatformWindow size, and then add
the new margins.

Pick-to: 6.2
Change-Id: I4f225be8fad56b1fa77e9e3cfd6538a206589d73
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
This commit is contained in:
Morten Johan Sørvig 2021-09-23 19:49:20 +02:00
parent cd96d87011
commit 2cfca7fd19
6 changed files with 32 additions and 2 deletions

View File

@ -124,6 +124,7 @@ public:
static QPoint mapPositionFromNative(const QPoint &pos, const QPlatformScreen *platformScreen);
static QPoint mapPositionToNative(const QPoint &pos, const QPlatformScreen *platformScreen);
static QDpi logicalDpi(const QScreen *screen);
static qreal roundScaleFactor(qreal rawFactor);
private:
struct ScreenFactor {
@ -134,7 +135,6 @@ private:
};
static qreal rawScaleFactor(const QPlatformScreen *screen);
static qreal roundScaleFactor(qreal rawFactor);
static QDpi effectiveLogicalDpi(const QPlatformScreen *screen, qreal rawFactor, qreal roundedFactor);
static qreal screenSubfactor(const QPlatformScreen *screen);
static QScreen *screenForPosition(Point position, QScreen *guess);

View File

@ -60,6 +60,10 @@
# define WM_DPICHANGED 0x02E0
#endif
#ifndef WM_GETDPISCALEDSIZE
# define WM_GETDPISCALEDSIZE 0x02E4
#endif
// WM_POINTER support from Windows 8 onwards (WINVER >= 0x0602)
#ifndef WM_POINTERUPDATE
# define WM_NCPOINTERUPDATE 0x0241
@ -129,6 +133,7 @@ enum WindowsEventType // Simplify event types
EnterSizeMoveEvent = WindowEventFlag + 22,
ExitSizeMoveEvent = WindowEventFlag + 23,
PointerActivateWindowEvent = WindowEventFlag + 24,
DpiScaledSizeEvent = WindowEventFlag + 25,
MouseEvent = MouseEventFlag + 1,
MouseWheelEvent = MouseEventFlag + 2,
CursorEvent = MouseEventFlag + 3,
@ -316,6 +321,8 @@ inline QtWindows::WindowsEventType windowsEventType(UINT message, WPARAM wParamI
return HIWORD(wParamIn) ? QtWindows::AcceleratorCommandEvent : QtWindows::MenuCommandEvent;
case WM_DPICHANGED:
return QtWindows::DpiChangedEvent;
case WM_GETDPISCALEDSIZE:
return QtWindows::DpiScaledSizeEvent;
case WM_ENTERSIZEMOVE:
return QtWindows::EnterSizeMoveEvent;
case WM_EXITSIZEMOVE:

View File

@ -1453,6 +1453,9 @@ bool QWindowsContext::windowsProc(HWND hwnd, UINT message,
return true;
#endif
} break;
case QtWindows::DpiScaledSizeEvent:
platformWindow->handleDpiScaledSize(wParam, lParam, result);
return true;
case QtWindows::DpiChangedEvent:
platformWindow->handleDpiChanged(hwnd, wParam, lParam);
return true;

View File

@ -86,7 +86,7 @@ public:
QImage::Format format() const override { return m_data.format; }
QSizeF physicalSize() const override { return m_data.physicalSizeMM; }
QDpi logicalDpi() const override { return m_data.dpi; }
QDpi logicalBaseDpi() const override { return QDpi(96, 96); }
QDpi logicalBaseDpi() const override { return QDpi(baseDpi, baseDpi); }
qreal devicePixelRatio() const override { return 1.0; }
qreal refreshRate() const override { return m_data.refreshRateHz; }
QString name() const override { return m_data.name; }
@ -115,6 +115,7 @@ public:
const QWindowsScreenData &data() const { return m_data; }
static QRect virtualGeometry(const QPlatformScreen *screen);
static inline int baseDpi = 96;
private:
QWindowsScreenData m_data;

View File

@ -1833,6 +1833,24 @@ void QWindowsWindow::handleCompositionSettingsChanged()
}
}
void QWindowsWindow::handleDpiScaledSize(WPARAM wParam, LPARAM lParam, LRESULT *result)
{
// We want to keep QWindow's device independent size constant across the
// DPI change. To accomplish this, scale QPlatformWindow's native size
// by the change of DPI (e.g. 120 -> 144 = 1.2), also taking any scale
// factor rounding into account. The win32 window size includes the margins;
// add the margins for the new DPI to the window size.
const int dpi = int(wParam);
const qreal scale = QHighDpiScaling::roundScaleFactor(qreal(dpi) / QWindowsScreen::baseDpi) /
QHighDpiScaling::roundScaleFactor(qreal(savedDpi()) / QWindowsScreen::baseDpi);
const QMargins margins = QWindowsGeometryHint::frame(style(), exStyle(), dpi);
const QSize windowSize = (geometry().size() * scale).grownBy(margins);
SIZE *size = reinterpret_cast<SIZE *>(lParam);
size->cx = windowSize.width();
size->cy = windowSize.height();
*result = true; // Inform Windows that we've set a size
}
void QWindowsWindow::handleDpiChanged(HWND hwnd, WPARAM wParam, LPARAM lParam)
{
const UINT dpi = HIWORD(wParam);

View File

@ -318,6 +318,7 @@ public:
void handleResized(int wParam);
void handleHidden();
void handleCompositionSettingsChanged();
void handleDpiScaledSize(WPARAM wParam, LPARAM lParam, LRESULT *result);
void handleDpiChanged(HWND hwnd, WPARAM wParam, LPARAM lParam);
static void displayChanged();