Fix build with MSVC.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-04-09 19:36:39 +08:00
parent 054a160639
commit bffcc2829a
3 changed files with 173 additions and 96 deletions

View File

@ -61,8 +61,10 @@ FramelessHelper::FramelessHelper(QObject *parent) : QObject(parent) {
QWidget widget; QWidget widget;
QStyleOption styleOption; QStyleOption styleOption;
styleOption.initFrom(&widget); styleOption.initFrom(&widget);
m_titlebarHeight = widget.style()->pixelMetric( m_titlebarHeight =
QStyle::PixelMetric::PM_TitleBarHeight, &styleOption); widget.style()->pixelMetric(QStyle::PixelMetric::PM_TitleBarHeight,
&styleOption) +
m_borderHeight;
qDebug().noquote() << "Window device pixel ratio:" qDebug().noquote() << "Window device pixel ratio:"
<< widget.devicePixelRatioF(); << widget.devicePixelRatioF();
qDebug().noquote() << "Window border width:" << m_borderWidth qDebug().noquote() << "Window border width:" << m_borderWidth

View File

@ -101,19 +101,26 @@
#define WM_DPICHANGED 0x02E0 #define WM_DPICHANGED 0x02E0
#endif #endif
#ifdef IsMaximized
#undef IsMaximized
#endif
#define IsMaximized m_lpIsZoomed
#ifndef WNEF_GENERATE_WINAPI #ifndef WNEF_GENERATE_WINAPI
#define WNEF_GENERATE_WINAPI(funcName, resultType, ...) \ #define WNEF_GENERATE_WINAPI(funcName, resultType, ...) \
using _WNEF_WINAPI_##funcName = resultType (WINAPI *) (__VA_ARGS__); \ using _WNEF_WINAPI_##funcName = resultType(WINAPI *)(__VA_ARGS__); \
_WNEF_WINAPI_##funcName m_lp##funcName = nullptr; _WNEF_WINAPI_##funcName m_lp##funcName = nullptr;
#endif #endif
#ifndef WNEF_RESOLVE_WINAPI #ifndef WNEF_RESOLVE_WINAPI
#define WNEF_RESOLVE_WINAPI(libName, funcName) \ #define WNEF_RESOLVE_WINAPI(libName, funcName) \
if (!m_lp##funcName) { \ if (!m_lp##funcName) { \
QLibrary library(QString::fromUtf8(#libName)); \ QLibrary library(QString::fromUtf8(#libName)); \
m_lp##funcName = \ m_lp##funcName = reinterpret_cast<_WNEF_WINAPI_##funcName>( \
reinterpret_cast<_WNEF_WINAPI_##funcName>(library.resolve(#funcName)); \ library.resolve(#funcName)); \
Q_ASSERT_X(m_lp##funcName, __FUNCTION__, qUtf8Printable(library.errorString()));\ Q_ASSERT_X(m_lp##funcName, __FUNCTION__, \
qUtf8Printable(library.errorString())); \
} }
#endif #endif
@ -152,13 +159,36 @@ WNEF_GENERATE_WINAPI(GetSystemDpiForProcess, UINT, HANDLE)
WNEF_GENERATE_WINAPI(GetDpiForWindow, UINT, HWND) WNEF_GENERATE_WINAPI(GetDpiForWindow, UINT, HWND)
WNEF_GENERATE_WINAPI(GetDpiForSystem, UINT) WNEF_GENERATE_WINAPI(GetDpiForSystem, UINT)
WNEF_GENERATE_WINAPI(GetSystemMetricsForDpi, int, int, UINT) WNEF_GENERATE_WINAPI(GetSystemMetricsForDpi, int, int, UINT)
WNEF_GENERATE_WINAPI(GetDpiForMonitor, HRESULT, HMONITOR, MONITOR_DPI_TYPE, UINT *, WNEF_GENERATE_WINAPI(GetDpiForMonitor, HRESULT, HMONITOR, MONITOR_DPI_TYPE,
UINT *) UINT *, UINT *)
WNEF_GENERATE_WINAPI(DwmExtendFrameIntoClientArea, HRESULT, HWND, const MARGINS *) WNEF_GENERATE_WINAPI(DwmExtendFrameIntoClientArea, HRESULT, HWND,
CONST MARGINS *)
WNEF_GENERATE_WINAPI(DwmIsCompositionEnabled, HRESULT, BOOL *) WNEF_GENERATE_WINAPI(DwmIsCompositionEnabled, HRESULT, BOOL *)
WNEF_GENERATE_WINAPI(DwmSetWindowAttribute, HRESULT, HWND, DWORD, LPCVOID, DWORD) WNEF_GENERATE_WINAPI(DwmSetWindowAttribute, HRESULT, HWND, DWORD, LPCVOID,
DWORD)
WNEF_GENERATE_WINAPI(SHAppBarMessage, UINT_PTR, DWORD, APPBARDATA *) WNEF_GENERATE_WINAPI(SHAppBarMessage, UINT_PTR, DWORD, APPBARDATA *)
WNEF_GENERATE_WINAPI(GetDeviceCaps, int, HDC, int) WNEF_GENERATE_WINAPI(GetDeviceCaps, int, HDC, int)
WNEF_GENERATE_WINAPI(DefWindowProcW, LRESULT, HWND, UINT, WPARAM, LPARAM)
WNEF_GENERATE_WINAPI(SetLayeredWindowAttributes, BOOL, HWND, COLORREF, BYTE,
DWORD)
WNEF_GENERATE_WINAPI(MoveWindow, BOOL, HWND, int, int, int, int, BOOL)
WNEF_GENERATE_WINAPI(IsZoomed, BOOL, HWND)
WNEF_GENERATE_WINAPI(IsIconic, BOOL, HWND)
WNEF_GENERATE_WINAPI(GetSystemMetrics, int, int)
WNEF_GENERATE_WINAPI(GetDC, HDC, HWND)
WNEF_GENERATE_WINAPI(ReleaseDC, int, HWND, HDC)
WNEF_GENERATE_WINAPI(RedrawWindow, BOOL, HWND, CONST LPRECT, HRGN, UINT)
WNEF_GENERATE_WINAPI(GetClientRect, BOOL, HWND, LPRECT)
WNEF_GENERATE_WINAPI(GetWindowRect, BOOL, HWND, LPRECT)
WNEF_GENERATE_WINAPI(ScreenToClient, BOOL, HWND, LPPOINT)
WNEF_GENERATE_WINAPI(EqualRect, BOOL, CONST LPRECT, CONST LPRECT)
WNEF_GENERATE_WINAPI(GetWindowLongPtrW, LONG_PTR, HWND, int)
WNEF_GENERATE_WINAPI(SetWindowLongPtrW, LONG_PTR, HWND, int, LONG_PTR)
WNEF_GENERATE_WINAPI(FindWindowW, HWND, LPCWSTR, LPCWSTR)
WNEF_GENERATE_WINAPI(MonitorFromWindow, HMONITOR, HWND, DWORD)
WNEF_GENERATE_WINAPI(GetMonitorInfoW, BOOL, HMONITOR, LPMONITORINFO)
WNEF_GENERATE_WINAPI(GetAncestor, HWND, HWND, UINT)
WNEF_GENERATE_WINAPI(GetDesktopWindow, HWND)
const UINT m_defaultDotsPerInch = USER_DEFAULT_SCREEN_DPI; const UINT m_defaultDotsPerInch = USER_DEFAULT_SCREEN_DPI;
@ -228,10 +258,11 @@ void WinNativeEventFilter::clearFramelessWindows() {
} }
int WinNativeEventFilter::borderWidth(HWND handle) { int WinNativeEventFilter::borderWidth(HWND handle) {
initWin32Api();
if (handle) { if (handle) {
createUserData(handle); createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>( const auto userData = reinterpret_cast<WINDOW *>(
GetWindowLongPtrW(handle, GWLP_USERDATA)); m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int bw = userData->windowData.borderWidth; const int bw = userData->windowData.borderWidth;
if (bw > 0) { if (bw > 0) {
return std::round(bw * getDevicePixelRatioForWindow(handle)); return std::round(bw * getDevicePixelRatioForWindow(handle));
@ -245,10 +276,11 @@ int WinNativeEventFilter::borderWidth(HWND handle) {
} }
int WinNativeEventFilter::borderHeight(HWND handle) { int WinNativeEventFilter::borderHeight(HWND handle) {
initWin32Api();
if (handle) { if (handle) {
createUserData(handle); createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>( const auto userData = reinterpret_cast<WINDOW *>(
GetWindowLongPtrW(handle, GWLP_USERDATA)); m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int bh = userData->windowData.borderHeight; const int bh = userData->windowData.borderHeight;
if (bh > 0) { if (bh > 0) {
return std::round(bh * getDevicePixelRatioForWindow(handle)); return std::round(bh * getDevicePixelRatioForWindow(handle));
@ -262,10 +294,11 @@ int WinNativeEventFilter::borderHeight(HWND handle) {
} }
int WinNativeEventFilter::titlebarHeight(HWND handle) { int WinNativeEventFilter::titlebarHeight(HWND handle) {
initWin32Api();
if (handle) { if (handle) {
createUserData(handle); createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>( const auto userData = reinterpret_cast<WINDOW *>(
GetWindowLongPtrW(handle, GWLP_USERDATA)); m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int tbh = userData->windowData.titlebarHeight; const int tbh = userData->windowData.titlebarHeight;
if (tbh > 0) { if (tbh > 0) {
return std::round(tbh * getDevicePixelRatioForWindow(handle)); return std::round(tbh * getDevicePixelRatioForWindow(handle));
@ -295,33 +328,39 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
} }
if (m_framelessWindows.isEmpty()) { if (m_framelessWindows.isEmpty()) {
// Only top level windows can be frameless. // Only top level windows can be frameless.
// Try to avoid this case because it will result in strange behavior, use addFramelessWindow if possible. // Try to avoid this case because it will result in strange behavior,
if (GetWindowLongPtrW(msg->hwnd, GWL_STYLE) & WS_CHILD) { // use addFramelessWindow if possible.
if (m_lpGetWindowLongPtrW(msg->hwnd, GWL_STYLE) & WS_CHILD) {
return false; return false;
} }
const HWND parent = GetAncestor(msg->hwnd, GA_PARENT); const HWND parent = m_lpGetAncestor(msg->hwnd, GA_PARENT);
if (parent && (parent != GetDesktopWindow())) { if (parent && (parent != m_lpGetDesktopWindow())) {
return false; return false;
} }
} else if (!m_framelessWindows.contains(msg->hwnd)) { } else if (!m_framelessWindows.contains(msg->hwnd)) {
return false; return false;
} }
createUserData(msg->hwnd); createUserData(msg->hwnd);
const auto data = const auto data = reinterpret_cast<WINDOW *>(
reinterpret_cast<WINDOW *>(GetWindowLongPtrW(msg->hwnd, GWLP_USERDATA)); m_lpGetWindowLongPtrW(msg->hwnd, GWLP_USERDATA));
if (!data->initialized) { if (!data->initialized) {
data->initialized = TRUE; data->initialized = TRUE;
SetWindowLongPtrW(msg->hwnd, GWL_EXSTYLE, GetWindowLongPtrW(msg->hwnd, GWL_EXSTYLE) | WS_EX_LAYERED); m_lpSetWindowLongPtrW(msg->hwnd, GWL_EXSTYLE,
SetLayeredWindowAttributes(msg->hwnd, RGB(255, 0, 255), 0, LWA_COLORKEY); m_lpGetWindowLongPtrW(msg->hwnd, GWL_EXSTYLE) |
WS_EX_LAYERED);
m_lpSetLayeredWindowAttributes(msg->hwnd, RGB(255, 0, 255), 0,
LWA_COLORKEY);
// Make sure our window have it's frame shadow. // Make sure our window have it's frame shadow.
// The frame shadow is drawn by Desktop Window Manager (DWM), don't draw it yourself. // The frame shadow is drawn by Desktop Window Manager (DWM), don't draw
// The frame shadow will get lost if DWM composition is disabled, it's designed to be, // it yourself. The frame shadow will get lost if DWM composition is
// don't force the window to draw a frame shadow in that case. // disabled, it's designed to be, don't force the window to draw a frame
// According to MSDN, DWM composition is always enabled and can't be disabled since Win8. // shadow in that case. According to MSDN, DWM composition is always
// enabled and can't be disabled since Win8.
handleDwmCompositionChanged(data); handleDwmCompositionChanged(data);
// For debug purposes. // For debug purposes.
qDebug().noquote() << "Window handle:" << msg->hwnd; qDebug().noquote() << "Window handle:" << msg->hwnd;
qDebug().noquote() << "Window DPI:" << getDotsPerInchForWindow(msg->hwnd) qDebug().noquote() << "Window DPI:"
<< getDotsPerInchForWindow(msg->hwnd)
<< "Window DPR:" << "Window DPR:"
<< getDevicePixelRatioForWindow(msg->hwnd); << getDevicePixelRatioForWindow(msg->hwnd);
qDebug().noquote() << "Window border width:" << borderWidth(msg->hwnd) qDebug().noquote() << "Window border width:" << borderWidth(msg->hwnd)
@ -331,18 +370,27 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
} }
switch (msg->message) { switch (msg->message) {
case WM_NCCALCSIZE: { case WM_NCCALCSIZE: {
// Sent when the size and position of a window's client area must be calculated. By processing this message, an application can control the content of the window's client area when the size or position of the window changes. // Sent when the size and position of a window's client area must be
// If wParam is TRUE, lParam points to an NCCALCSIZE_PARAMS structure that contains information an application can use to calculate the new size and position of the client rectangle. // calculated. By processing this message, an application can control
// If wParam is FALSE, lParam points to a RECT structure. On entry, the structure contains the proposed window rectangle for the window. On exit, the structure should contain the screen coordinates of the corresponding window client area. // the content of the window's client area when the size or position of
const auto mode = static_cast<BOOL>(msg->wParam); // the window changes. If wParam is TRUE, lParam points to an
// NCCALCSIZE_PARAMS structure that contains information an application
// can use to calculate the new size and position of the client
// rectangle. If wParam is FALSE, lParam points to a RECT structure. On
// entry, the structure contains the proposed window rectangle for the
// window. On exit, the structure should contain the screen coordinates
// of the corresponding window client area.
const auto mode = static_cast<BOOL>(msg->wParam);
if (IsMaximized(msg->hwnd)) { if (IsMaximized(msg->hwnd)) {
const HMONITOR windowMonitor = const HMONITOR windowMonitor =
MonitorFromWindow(msg->hwnd, MONITOR_DEFAULTTONEAREST); m_lpMonitorFromWindow(msg->hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO monitorInfo; MONITORINFO monitorInfo;
SecureZeroMemory(&monitorInfo, sizeof(monitorInfo)); SecureZeroMemory(&monitorInfo, sizeof(monitorInfo));
monitorInfo.cbSize = sizeof(monitorInfo); monitorInfo.cbSize = sizeof(monitorInfo);
GetMonitorInfoW(windowMonitor, &monitorInfo); m_lpGetMonitorInfoW(windowMonitor, &monitorInfo);
const auto rect = mode ? &(reinterpret_cast<LPNCCALCSIZE_PARAMS>(msg->lParam)->rgrc[0]) : reinterpret_cast<LPRECT>(msg->lParam); const auto rect = mode
? &(reinterpret_cast<LPNCCALCSIZE_PARAMS>(msg->lParam)->rgrc[0])
: reinterpret_cast<LPRECT>(msg->lParam);
*rect = monitorInfo.rcWork; *rect = monitorInfo.rcWork;
// If the client rectangle is the same as the monitor's // If the client rectangle is the same as the monitor's
// rectangle, the shell assumes that the window has gone // rectangle, the shell assumes that the window has gone
@ -352,7 +400,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
// a certain edge. The edge is chosen based on which side of // a certain edge. The edge is chosen based on which side of
// the monitor is likely to contain an auto-hide appbar, so // the monitor is likely to contain an auto-hide appbar, so
// the missing client area is covered by it. // the missing client area is covered by it.
if (EqualRect(&monitorInfo.rcWork, &monitorInfo.rcMonitor)) { if (m_lpEqualRect(&monitorInfo.rcWork, &monitorInfo.rcMonitor)) {
APPBARDATA abd; APPBARDATA abd;
SecureZeroMemory(&abd, sizeof(abd)); SecureZeroMemory(&abd, sizeof(abd));
abd.cbSize = sizeof(abd); abd.cbSize = sizeof(abd);
@ -360,16 +408,13 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
m_lpSHAppBarMessage(ABM_GETSTATE, &abd); m_lpSHAppBarMessage(ABM_GETSTATE, &abd);
if (taskbarState & ABS_AUTOHIDE) { if (taskbarState & ABS_AUTOHIDE) {
int edge = -1; int edge = -1;
abd.hWnd = abd.hWnd = m_lpFindWindowW(L"Shell_TrayWnd", nullptr);
FindWindowW(L"Shell_TrayWnd", nullptr);
if (abd.hWnd) { if (abd.hWnd) {
const HMONITOR taskbarMonitor = const HMONITOR taskbarMonitor = m_lpMonitorFromWindow(
MonitorFromWindow( abd.hWnd, MONITOR_DEFAULTTOPRIMARY);
abd.hWnd, MONITOR_DEFAULTTOPRIMARY);
if (taskbarMonitor && if (taskbarMonitor &&
(taskbarMonitor == windowMonitor)) { (taskbarMonitor == windowMonitor)) {
m_lpSHAppBarMessage(ABM_GETTASKBARPOS, m_lpSHAppBarMessage(ABM_GETTASKBARPOS, &abd);
&abd);
edge = abd.uEdge; edge = abd.uEdge;
} }
} }
@ -384,16 +429,17 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
} }
} }
} }
// We cannot return WVR_REDRAW when there is nonclient area, or Windows // We cannot return WVR_REDRAW when there is nonclient area, or
// exhibits bugs where client pixels and child HWNDs are mispositioned by // Windows exhibits bugs where client pixels and child HWNDs are
// the width/height of the upper-left nonclient area. // mispositioned by the width/height of the upper-left nonclient
// area.
*result = 0; *result = 0;
return true; return true;
} }
// If the window bounds change, we're going to relayout and repaint anyway. // If the window bounds change, we're going to relayout and repaint
// Returning WVR_REDRAW avoids an extra paint before that of the old client // anyway. Returning WVR_REDRAW avoids an extra paint before that of the
// pixels in the (now wrong) location, and thus makes actions like resizing a // old client pixels in the (now wrong) location, and thus makes actions
// window from the left edge look slightly less broken. // like resizing a window from the left edge look slightly less broken.
*result = mode ? WVR_REDRAW : 0; *result = mode ? WVR_REDRAW : 0;
return true; return true;
} }
@ -419,7 +465,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
case WM_NCACTIVATE: { case WM_NCACTIVATE: {
// DefWindowProc won't repaint the window border if lParam (normally // DefWindowProc won't repaint the window border if lParam (normally
// a HRGN) is -1. // a HRGN) is -1.
*result = DefWindowProcW(msg->hwnd, msg->message, msg->wParam, -1); *result = m_lpDefWindowProcW(msg->hwnd, msg->message, msg->wParam, -1);
return true; return true;
} }
case WM_NCHITTEST: { case WM_NCHITTEST: {
@ -437,8 +483,10 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
if (!area.isValid()) { if (!area.isValid()) {
continue; continue;
} }
if (QRect(std::round(area.x() * dpr), std::round(area.y() * dpr), if (QRect(std::round(area.x() * dpr),
std::round(area.width() * dpr), std::round(area.height() * dpr)) std::round(area.y() * dpr),
std::round(area.width() * dpr),
std::round(area.height() * dpr))
.contains(x, y, true)) { .contains(x, y, true)) {
return true; return true;
} }
@ -446,13 +494,13 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
return false; return false;
}; };
RECT clientRect = {0, 0, 0, 0}; RECT clientRect = {0, 0, 0, 0};
GetClientRect(_hWnd, &clientRect); m_lpGetClientRect(_hWnd, &clientRect);
const LONG ww = clientRect.right; const LONG ww = clientRect.right;
const LONG wh = clientRect.bottom; const LONG wh = clientRect.bottom;
POINT mouse; POINT mouse;
mouse.x = GET_X_LPARAM(_lParam); mouse.x = GET_X_LPARAM(_lParam);
mouse.y = GET_Y_LPARAM(_lParam); mouse.y = GET_Y_LPARAM(_lParam);
ScreenToClient(_hWnd, &mouse); m_lpScreenToClient(_hWnd, &mouse);
// These values are DPI-aware. // These values are DPI-aware.
const LONG bw = borderWidth(_hWnd); const LONG bw = borderWidth(_hWnd);
const LONG bh = borderHeight(_hWnd); const LONG bh = borderHeight(_hWnd);
@ -479,7 +527,8 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
// Make the border wider to let the user easy to resize on corners. // Make the border wider to let the user easy to resize on corners.
const int factor = (isTop || isBottom) ? 2 : 1; const int factor = (isTop || isBottom) ? 2 : 1;
const bool isLeft = isInsideWindow && (mouse.x < (bw * factor)); const bool isLeft = isInsideWindow && (mouse.x < (bw * factor));
const bool isRight = isInsideWindow && (mouse.x > (ww - (bw * factor))); const bool isRight =
isInsideWindow && (mouse.x > (ww - (bw * factor)));
const bool fixedSize = _data->windowData.fixedSize; const bool fixedSize = _data->windowData.fixedSize;
const auto getBorderValue = [fixedSize](int value) -> int { const auto getBorderValue = [fixedSize](int value) -> int {
// HTBORDER: un-resizeable window border. // HTBORDER: un-resizeable window border.
@ -520,11 +569,11 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
case WM_GETMINMAXINFO: { case WM_GETMINMAXINFO: {
// Don't cover the taskbar when maximized. // Don't cover the taskbar when maximized.
const HMONITOR monitor = const HMONITOR monitor =
MonitorFromWindow(msg->hwnd, MONITOR_DEFAULTTONEAREST); m_lpMonitorFromWindow(msg->hwnd, MONITOR_DEFAULTTONEAREST);
MONITORINFO monitorInfo; MONITORINFO monitorInfo;
SecureZeroMemory(&monitorInfo, sizeof(monitorInfo)); SecureZeroMemory(&monitorInfo, sizeof(monitorInfo));
monitorInfo.cbSize = sizeof(monitorInfo); monitorInfo.cbSize = sizeof(monitorInfo);
GetMonitorInfoW(monitor, &monitorInfo); m_lpGetMonitorInfoW(monitor, &monitorInfo);
const RECT rcWorkArea = monitorInfo.rcWork; const RECT rcWorkArea = monitorInfo.rcWork;
const RECT rcMonitorArea = monitorInfo.rcMonitor; const RECT rcMonitorArea = monitorInfo.rcMonitor;
auto &mmi = *reinterpret_cast<LPMINMAXINFO>(msg->lParam); auto &mmi = *reinterpret_cast<LPMINMAXINFO>(msg->lParam);
@ -546,18 +595,22 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
mmi.ptMaxSize.x = std::abs(rcWorkArea.right - rcWorkArea.left); mmi.ptMaxSize.x = std::abs(rcWorkArea.right - rcWorkArea.left);
mmi.ptMaxSize.y = std::abs(rcWorkArea.bottom - rcWorkArea.top); mmi.ptMaxSize.y = std::abs(rcWorkArea.bottom - rcWorkArea.top);
} else { } else {
mmi.ptMaxSize.x = std::round(getDevicePixelRatioForWindow(msg->hwnd) * mmi.ptMaxSize.x =
data->windowData.maximumSize.width()); std::round(getDevicePixelRatioForWindow(msg->hwnd) *
mmi.ptMaxSize.y = std::round(getDevicePixelRatioForWindow(msg->hwnd) * data->windowData.maximumSize.width());
data->windowData.maximumSize.height()); mmi.ptMaxSize.y =
std::round(getDevicePixelRatioForWindow(msg->hwnd) *
data->windowData.maximumSize.height());
} }
mmi.ptMaxTrackSize.x = mmi.ptMaxSize.x; mmi.ptMaxTrackSize.x = mmi.ptMaxSize.x;
mmi.ptMaxTrackSize.y = mmi.ptMaxSize.y; mmi.ptMaxTrackSize.y = mmi.ptMaxSize.y;
if (!data->windowData.minimumSize.isEmpty()) { if (!data->windowData.minimumSize.isEmpty()) {
mmi.ptMinTrackSize.x = std::round(getDevicePixelRatioForWindow(msg->hwnd) * mmi.ptMinTrackSize.x =
data->windowData.minimumSize.width()); std::round(getDevicePixelRatioForWindow(msg->hwnd) *
mmi.ptMinTrackSize.y = std::round(getDevicePixelRatioForWindow(msg->hwnd) * data->windowData.minimumSize.width());
data->windowData.minimumSize.height()); mmi.ptMinTrackSize.y =
std::round(getDevicePixelRatioForWindow(msg->hwnd) *
data->windowData.minimumSize.height());
} }
*result = 0; *result = 0;
return true; return true;
@ -570,13 +623,14 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
// when composition is enabled and blocking WM_NCUAHDRAWCAPTION should // when composition is enabled and blocking WM_NCUAHDRAWCAPTION should
// be enough to prevent painting when theming is enabled. // be enough to prevent painting when theming is enabled.
if (!data->dwmCompositionEnabled) { if (!data->dwmCompositionEnabled) {
const LONG_PTR oldStyle = GetWindowLongPtrW(msg->hwnd, GWL_STYLE); const LONG_PTR oldStyle =
m_lpGetWindowLongPtrW(msg->hwnd, GWL_STYLE);
// Prevent Windows from drawing the default title bar by temporarily // Prevent Windows from drawing the default title bar by temporarily
// toggling the WS_VISIBLE style. // toggling the WS_VISIBLE style.
SetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle & ~WS_VISIBLE); m_lpSetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle & ~WS_VISIBLE);
const LRESULT ret = DefWindowProcW(msg->hwnd, msg->message, const LRESULT ret = m_lpDefWindowProcW(msg->hwnd, msg->message,
msg->wParam, msg->lParam); msg->wParam, msg->lParam);
SetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle); m_lpSetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle);
*result = ret; *result = ret;
return true; return true;
} }
@ -590,9 +644,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
case WM_SIZE: { case WM_SIZE: {
#if 1 #if 1
const HWND _hWnd = msg->hwnd; const HWND _hWnd = msg->hwnd;
QTimer::singleShot(50, [this, _hWnd](){ QTimer::singleShot(50, [this, _hWnd]() { redrawWindow(_hWnd); });
redrawWindow(_hWnd);
});
#else #else
InvalidateRect(msg->hwnd, nullptr, FALSE); InvalidateRect(msg->hwnd, nullptr, FALSE);
redrawWindow(msg->hwnd); redrawWindow(msg->hwnd);
@ -621,18 +673,18 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
// more than resizing, so it's safer to let Qt do the scaling. // more than resizing, so it's safer to let Qt do the scaling.
QTimer::singleShot(50, [this, _hWnd]() { QTimer::singleShot(50, [this, _hWnd]() {
RECT rect = {0, 0, 0, 0}; RECT rect = {0, 0, 0, 0};
GetWindowRect(_hWnd, &rect); m_lpGetWindowRect(_hWnd, &rect);
const int x = rect.left; const int x = rect.left;
const int y = rect.top; const int y = rect.top;
const int width = std::abs(rect.right - rect.left); const int width = std::abs(rect.right - rect.left);
const int height = std::abs(rect.bottom - rect.top); const int height = std::abs(rect.bottom - rect.top);
// Don't increase the window size too much, otherwise it would be // Don't increase the window size too much, otherwise it would be
// too obvious for the user and the experience is not good. // too obvious for the user and the experience is not good.
MoveWindow(_hWnd, x, y, width + 1, height + 1, TRUE); m_lpMoveWindow(_hWnd, x, y, width + 1, height + 1, TRUE);
// Re-paint the window after resizing. // Re-paint the window after resizing.
redrawWindow(_hWnd); redrawWindow(_hWnd);
// Restore and repaint. // Restore and repaint.
MoveWindow(_hWnd, x, y, width, height, TRUE); m_lpMoveWindow(_hWnd, x, y, width, height, TRUE);
redrawWindow(_hWnd); redrawWindow(_hWnd);
}); });
break; break;
@ -679,11 +731,11 @@ UINT WinNativeEventFilter::getDotsPerInchForWindow(HWND handle) {
} }
#endif #endif
// Available since Windows 2000. // Available since Windows 2000.
const HDC hdc = GetDC(nullptr); const HDC hdc = m_lpGetDC(nullptr);
if (hdc) { if (hdc) {
const int dpiX = m_lpGetDeviceCaps(hdc, LOGPIXELSX); const int dpiX = m_lpGetDeviceCaps(hdc, LOGPIXELSX);
const int dpiY = m_lpGetDeviceCaps(hdc, LOGPIXELSY); const int dpiY = m_lpGetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(nullptr, hdc); m_lpReleaseDC(nullptr, hdc);
// The values of dpiX and dpiY are identical actually, just to // The values of dpiX and dpiY are identical actually, just to
// silence a compiler warning. // silence a compiler warning.
return dpiX == dpiY ? dpiY : dpiX; return dpiX == dpiY ? dpiY : dpiX;
@ -704,8 +756,9 @@ UINT WinNativeEventFilter::getDotsPerInchForWindow(HWND handle) {
} }
if (m_lpGetDpiForMonitor) { if (m_lpGetDpiForMonitor) {
UINT dpiX = m_defaultDotsPerInch, dpiY = m_defaultDotsPerInch; UINT dpiX = m_defaultDotsPerInch, dpiY = m_defaultDotsPerInch;
m_lpGetDpiForMonitor(MonitorFromWindow(handle, MONITOR_DEFAULTTONEAREST), m_lpGetDpiForMonitor(
MDT_EFFECTIVE_DPI, &dpiX, &dpiY); m_lpMonitorFromWindow(handle, MONITOR_DEFAULTTONEAREST),
MDT_EFFECTIVE_DPI, &dpiX, &dpiY);
// The values of *dpiX and *dpiY are identical. // The values of *dpiX and *dpiY are identical.
return dpiX; return dpiX;
} }
@ -721,11 +774,13 @@ qreal WinNativeEventFilter::getDevicePixelRatioForWindow(HWND handle) {
int WinNativeEventFilter::getSystemMetricsForWindow(HWND handle, int index) { int WinNativeEventFilter::getSystemMetricsForWindow(HWND handle, int index) {
if (m_lpGetSystemMetricsForDpi) { if (m_lpGetSystemMetricsForDpi) {
return m_lpGetSystemMetricsForDpi(index, return m_lpGetSystemMetricsForDpi(
static_cast<UINT>(std::round(getPreferedNumber( index,
getDotsPerInchForWindow(handle))))); static_cast<UINT>(std::round(
getPreferedNumber(getDotsPerInchForWindow(handle)))));
} else { } else {
return std::round(GetSystemMetrics(index) * getDevicePixelRatioForWindow(handle)); return std::round(m_lpGetSystemMetrics(index) *
getDevicePixelRatioForWindow(handle));
} }
} }
@ -740,7 +795,7 @@ WinNativeEventFilter::windowData(HWND window) {
if (window) { if (window) {
createUserData(window); createUserData(window);
return &reinterpret_cast<WINDOW *>( return &reinterpret_cast<WINDOW *>(
GetWindowLongPtrW(window, GWLP_USERDATA)) m_lpGetWindowLongPtrW(window, GWLP_USERDATA))
->windowData; ->windowData;
} }
return nullptr; return nullptr;
@ -749,7 +804,7 @@ WinNativeEventFilter::windowData(HWND window) {
void WinNativeEventFilter::createUserData(HWND handle, const WINDOWDATA *data) { void WinNativeEventFilter::createUserData(HWND handle, const WINDOWDATA *data) {
if (handle) { if (handle) {
const auto userData = reinterpret_cast<WINDOW *>( const auto userData = reinterpret_cast<WINDOW *>(
GetWindowLongPtrW(handle, GWLP_USERDATA)); m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
if (userData) { if (userData) {
if (data) { if (data) {
userData->windowData = *data; userData->windowData = *data;
@ -760,14 +815,34 @@ void WinNativeEventFilter::createUserData(HWND handle, const WINDOWDATA *data) {
if (data) { if (data) {
_data->windowData = *data; _data->windowData = *data;
} }
SetWindowLongPtrW(handle, GWLP_USERDATA, m_lpSetWindowLongPtrW(handle, GWLP_USERDATA,
reinterpret_cast<LONG_PTR>(_data)); reinterpret_cast<LONG_PTR>(_data));
} }
} }
} }
void WinNativeEventFilter::initWin32Api() { void WinNativeEventFilter::initWin32Api() {
// Available since Windows 2000. // Available since Windows 2000.
WNEF_RESOLVE_WINAPI(User32, GetDesktopWindow)
WNEF_RESOLVE_WINAPI(User32, GetAncestor)
WNEF_RESOLVE_WINAPI(User32, DefWindowProcW)
WNEF_RESOLVE_WINAPI(User32, SetLayeredWindowAttributes)
WNEF_RESOLVE_WINAPI(User32, MoveWindow)
WNEF_RESOLVE_WINAPI(User32, IsZoomed)
WNEF_RESOLVE_WINAPI(User32, IsIconic)
WNEF_RESOLVE_WINAPI(User32, GetSystemMetrics)
WNEF_RESOLVE_WINAPI(User32, GetDC)
WNEF_RESOLVE_WINAPI(User32, ReleaseDC)
WNEF_RESOLVE_WINAPI(User32, RedrawWindow)
WNEF_RESOLVE_WINAPI(User32, GetClientRect)
WNEF_RESOLVE_WINAPI(User32, GetWindowRect)
WNEF_RESOLVE_WINAPI(User32, ScreenToClient)
WNEF_RESOLVE_WINAPI(User32, EqualRect)
WNEF_RESOLVE_WINAPI(User32, GetWindowLongPtrW)
WNEF_RESOLVE_WINAPI(User32, SetWindowLongPtrW)
WNEF_RESOLVE_WINAPI(User32, FindWindowW)
WNEF_RESOLVE_WINAPI(User32, MonitorFromWindow)
WNEF_RESOLVE_WINAPI(User32, GetMonitorInfoW)
WNEF_RESOLVE_WINAPI(Gdi32, GetDeviceCaps) WNEF_RESOLVE_WINAPI(Gdi32, GetDeviceCaps)
// Available since Windows XP. // Available since Windows XP.
WNEF_RESOLVE_WINAPI(Shell32, SHAppBarMessage) WNEF_RESOLVE_WINAPI(Shell32, SHAppBarMessage)
@ -866,9 +941,9 @@ qreal WinNativeEventFilter::getPreferedNumber(qreal num) {
return result; return result;
} }
void WinNativeEventFilter::redrawWindow(HWND handle) void WinNativeEventFilter::redrawWindow(HWND handle) {
{
if (handle) { if (handle) {
RedrawWindow(handle, nullptr, nullptr, RDW_INVALIDATE | RDW_ALLCHILDREN); m_lpRedrawWindow(handle, nullptr, nullptr,
RDW_INVALIDATE | RDW_ALLCHILDREN);
} }
} }

View File

@ -87,8 +87,8 @@ public:
// DPI-aware border height of the given window (if the pointer is null, // DPI-aware border height of the given window (if the pointer is null,
// return the system's standard value). // return the system's standard value).
static int borderHeight(HWND handle); static int borderHeight(HWND handle);
// DPI-aware titlebar height (including the border height) of the given window (if the pointer is null, // DPI-aware titlebar height (including the border height) of the given
// return the system's standard value). // window (if the pointer is null, return the system's standard value).
static int titlebarHeight(HWND handle); static int titlebarHeight(HWND handle);
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
@ -100,7 +100,7 @@ public:
#endif #endif
private: private:
void initWin32Api(); static void initWin32Api();
void redrawWindow(HWND handle); void redrawWindow(HWND handle);
static void createUserData(HWND handle, const WINDOWDATA *data = nullptr); static void createUserData(HWND handle, const WINDOWDATA *data = nullptr);
void handleDwmCompositionChanged(WINDOW *data); void handleDwmCompositionChanged(WINDOW *data);