Minor tweaks.

1. Use Qt functions instead of STL functions.
2. Add a comment explain the XXPtr functions.
3. Remove D2D usage. Using GetDeviceCaps is fine.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-05-02 11:00:01 +08:00
parent 5335114258
commit 201a170948
1 changed files with 51 additions and 46 deletions

View File

@ -28,8 +28,7 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QLibrary> #include <QLibrary>
#include <QOperatingSystemVersion> #include <QOperatingSystemVersion>
#include <cmath> #include <QtMath>
#include <d2d1.h>
#ifdef IsMinimized #ifdef IsMinimized
#undef IsMinimized #undef IsMinimized
@ -290,8 +289,10 @@ WNEF_GENERATE_WINAPI(GetCurrentProcess, HANDLE)
WNEF_GENERATE_WINAPI(GetProcessDpiAwareness, HRESULT, HANDLE, WNEF_GENERATE_WINAPI(GetProcessDpiAwareness, HRESULT, HANDLE,
PROCESS_DPI_AWARENESS *) PROCESS_DPI_AWARENESS *)
WNEF_GENERATE_WINAPI(IsProcessDPIAware, BOOL) WNEF_GENERATE_WINAPI(IsProcessDPIAware, BOOL)
#if 0
WNEF_GENERATE_WINAPI(D2D1CreateFactory, HRESULT, D2D1_FACTORY_TYPE, REFIID, WNEF_GENERATE_WINAPI(D2D1CreateFactory, HRESULT, D2D1_FACTORY_TYPE, REFIID,
CONST D2D1_FACTORY_OPTIONS *, void **) CONST D2D1_FACTORY_OPTIONS *, void **)
#endif
WNEF_GENERATE_WINAPI(AdjustWindowRectEx, BOOL, LPRECT, DWORD, BOOL, DWORD) WNEF_GENERATE_WINAPI(AdjustWindowRectEx, BOOL, LPRECT, DWORD, BOOL, DWORD)
WNEF_GENERATE_WINAPI(AdjustWindowRectExForDpi, BOOL, LPRECT, DWORD, BOOL, DWORD, WNEF_GENERATE_WINAPI(AdjustWindowRectExForDpi, BOOL, LPRECT, DWORD, BOOL, DWORD,
UINT) UINT)
@ -342,6 +343,7 @@ void ResolveWin32APIs() {
WNEF_RESOLVE_WINAPI(User32, ScreenToClient) WNEF_RESOLVE_WINAPI(User32, ScreenToClient)
WNEF_RESOLVE_WINAPI(User32, EqualRect) WNEF_RESOLVE_WINAPI(User32, EqualRect)
#ifdef Q_PROCESSOR_X86_64 #ifdef Q_PROCESSOR_X86_64
// These functions only exist in 64 bit User32.dll
WNEF_RESOLVE_WINAPI(User32, GetWindowLongPtrW) WNEF_RESOLVE_WINAPI(User32, GetWindowLongPtrW)
WNEF_RESOLVE_WINAPI(User32, SetWindowLongPtrW) WNEF_RESOLVE_WINAPI(User32, SetWindowLongPtrW)
WNEF_RESOLVE_WINAPI(User32, GetClassLongPtrW) WNEF_RESOLVE_WINAPI(User32, GetClassLongPtrW)
@ -375,7 +377,9 @@ void ResolveWin32APIs() {
WNEF_RESOLVE_WINAPI(UxTheme, EndBufferedPaint) WNEF_RESOLVE_WINAPI(UxTheme, EndBufferedPaint)
WNEF_RESOLVE_WINAPI(UxTheme, BeginBufferedPaint) WNEF_RESOLVE_WINAPI(UxTheme, BeginBufferedPaint)
// Available since Windows 7. // Available since Windows 7.
#if 0
WNEF_RESOLVE_WINAPI(D2D1, D2D1CreateFactory) WNEF_RESOLVE_WINAPI(D2D1, D2D1CreateFactory)
#endif
// Available since Windows 8.1 // Available since Windows 8.1
if (QOperatingSystemVersion::current() >= if (QOperatingSystemVersion::current() >=
QOperatingSystemVersion::Windows8_1) { QOperatingSystemVersion::Windows8_1) {
@ -460,6 +464,7 @@ BOOL IsTopLevel(HWND handle) {
UINT GetDotsPerInchForWindow(HWND handle) { UINT GetDotsPerInchForWindow(HWND handle) {
const auto getScreenDpi = [](UINT defaultValue) -> UINT { const auto getScreenDpi = [](UINT defaultValue) -> UINT {
#if 0
if (m_lpD2D1CreateFactory) { if (m_lpD2D1CreateFactory) {
// Using Direct2D to get the screen DPI. // Using Direct2D to get the screen DPI.
// Available since Windows 7. // Available since Windows 7.
@ -472,9 +477,10 @@ UINT GetDotsPerInchForWindow(HWND handle) {
FLOAT dpiX = defaultValue, dpiY = defaultValue; FLOAT dpiX = defaultValue, dpiY = defaultValue;
m_pDirect2dFactory->GetDesktopDpi(&dpiX, &dpiY); m_pDirect2dFactory->GetDesktopDpi(&dpiX, &dpiY);
// The values of *dpiX and *dpiY are identical. // The values of *dpiX and *dpiY are identical.
return std::round(dpiX); return qRound(dpiX);
} }
} }
#endif
// Available since Windows 2000. // Available since Windows 2000.
const HDC hdc = m_lpGetDC(nullptr); const HDC hdc = m_lpGetDC(nullptr);
if (hdc) { if (hdc) {
@ -527,7 +533,7 @@ qreal GetPreferedNumber(qreal num) {
// If the given number is not very large, we assume it's a // If the given number is not very large, we assume it's a
// device pixel ratio (DPR), otherwise we assume it's a DPI. // device pixel ratio (DPR), otherwise we assume it's a DPI.
if (in < m_defaultDotsPerInch) { if (in < m_defaultDotsPerInch) {
return std::round(in); return qRound(in);
} else { } else {
if (in < (m_defaultDotsPerInch * 1.5)) { if (in < (m_defaultDotsPerInch * 1.5)) {
return m_defaultDotsPerInch; return m_defaultDotsPerInch;
@ -557,10 +563,10 @@ qreal GetPreferedNumber(qreal num) {
result = num; result = num;
break; break;
case Qt::HighDpiScaleFactorRoundingPolicy::Floor: case Qt::HighDpiScaleFactorRoundingPolicy::Floor:
result = std::floor(num); result = qFloor(num);
break; break;
case Qt::HighDpiScaleFactorRoundingPolicy::Ceil: case Qt::HighDpiScaleFactorRoundingPolicy::Ceil:
result = std::ceil(num); result = qCeil(num);
break; break;
default: default:
// Default behavior for Qt 5.6 to 5.15 // Default behavior for Qt 5.6 to 5.15
@ -603,16 +609,16 @@ RECT GetFrameSizeForWindow(HWND handle, bool includingTitleBar = false) {
FALSE, FALSE,
m_lpGetWindowLongPtrW(handle, GWL_EXSTYLE)); m_lpGetWindowLongPtrW(handle, GWL_EXSTYLE));
const qreal dpr = GetDevicePixelRatioForWindow(handle); const qreal dpr = GetDevicePixelRatioForWindow(handle);
rect.top = std::round(rect.top * dpr); rect.top = qRound(rect.top * dpr);
rect.bottom = std::round(rect.bottom * dpr); rect.bottom = qRound(rect.bottom * dpr);
rect.left = std::round(rect.left * dpr); rect.left = qRound(rect.left * dpr);
rect.right = std::round(rect.right * dpr); rect.right = qRound(rect.right * dpr);
} }
// Some values may be negative. Make them positive unconditionally. // Some values may be negative. Make them positive unconditionally.
rect.top = std::abs(rect.top); rect.top = qAbs(rect.top);
rect.bottom = std::abs(rect.bottom); rect.bottom = qAbs(rect.bottom);
rect.left = std::abs(rect.left); rect.left = qAbs(rect.left);
rect.right = std::abs(rect.right); rect.right = qAbs(rect.right);
} }
return rect; return rect;
} }
@ -648,10 +654,10 @@ int GetSystemMetricsForWindow(HWND handle, int index) {
if (m_lpGetSystemMetricsForDpi) { if (m_lpGetSystemMetricsForDpi) {
return m_lpGetSystemMetricsForDpi( return m_lpGetSystemMetricsForDpi(
index, index,
static_cast<UINT>(std::round( static_cast<UINT>(qRound(
GetPreferedNumber(GetDotsPerInchForWindow(handle))))); GetPreferedNumber(GetDotsPerInchForWindow(handle)))));
} else { } else {
return std::round(m_lpGetSystemMetrics(index) * return qRound(m_lpGetSystemMetrics(index) *
GetDevicePixelRatioForWindow(handle)); GetDevicePixelRatioForWindow(handle));
} }
} }
@ -721,7 +727,7 @@ QVector<HWND> WinNativeEventFilter::framelessWindows() {
void WinNativeEventFilter::setFramelessWindows(QVector<HWND> windows) { void WinNativeEventFilter::setFramelessWindows(QVector<HWND> windows) {
if (!windows.isEmpty() && (windows != m_framelessWindows)) { if (!windows.isEmpty() && (windows != m_framelessWindows)) {
m_framelessWindows = windows; m_framelessWindows = windows;
for (auto &&window : std::as_const(m_framelessWindows)) { for (auto &&window : qAsConst(m_framelessWindows)) {
createUserData(window); createUserData(window);
} }
install(); install();
@ -1171,14 +1177,14 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
const auto isInSpecificAreas = [](int x, int y, const auto isInSpecificAreas = [](int x, int y,
const QVector<QRect> &areas, const QVector<QRect> &areas,
qreal dpr) -> bool { qreal dpr) -> bool {
for (auto &&area : std::as_const(areas)) { for (auto &&area : qAsConst(areas)) {
if (!area.isValid()) { if (!area.isValid()) {
continue; continue;
} }
if (QRect(std::round(area.x() * dpr), if (QRect(qRound(area.x() * dpr),
std::round(area.y() * dpr), qRound(area.y() * dpr),
std::round(area.width() * dpr), qRound(area.width() * dpr),
std::round(area.height() * dpr)) qRound(area.height() * dpr))
.contains(x, y, true)) { .contains(x, y, true)) {
return true; return true;
} }
@ -1279,29 +1285,28 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
} else { } else {
// Works fine on Windows 8/8.1/10 // Works fine on Windows 8/8.1/10
mmi.ptMaxPosition.x = mmi.ptMaxPosition.x =
std::abs(rcWorkArea.left - rcMonitorArea.left); qAbs(rcWorkArea.left - rcMonitorArea.left);
mmi.ptMaxPosition.y = mmi.ptMaxPosition.y = qAbs(rcWorkArea.top - rcMonitorArea.top);
std::abs(rcWorkArea.top - rcMonitorArea.top);
} }
if (data->windowData.maximumSize.isEmpty()) { if (data->windowData.maximumSize.isEmpty()) {
mmi.ptMaxSize.x = std::abs(rcWorkArea.right - rcWorkArea.left); mmi.ptMaxSize.x = qAbs(rcWorkArea.right - rcWorkArea.left);
mmi.ptMaxSize.y = std::abs(rcWorkArea.bottom - rcWorkArea.top); mmi.ptMaxSize.y = qAbs(rcWorkArea.bottom - rcWorkArea.top);
} else { } else {
mmi.ptMaxSize.x = mmi.ptMaxSize.x =
std::round(GetDevicePixelRatioForWindow(msg->hwnd) * qRound(GetDevicePixelRatioForWindow(msg->hwnd) *
data->windowData.maximumSize.width()); data->windowData.maximumSize.width());
mmi.ptMaxSize.y = mmi.ptMaxSize.y =
std::round(GetDevicePixelRatioForWindow(msg->hwnd) * qRound(GetDevicePixelRatioForWindow(msg->hwnd) *
data->windowData.maximumSize.height()); 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 = mmi.ptMinTrackSize.x =
std::round(GetDevicePixelRatioForWindow(msg->hwnd) * qRound(GetDevicePixelRatioForWindow(msg->hwnd) *
data->windowData.minimumSize.width()); data->windowData.minimumSize.width());
mmi.ptMinTrackSize.y = mmi.ptMinTrackSize.y =
std::round(GetDevicePixelRatioForWindow(msg->hwnd) * qRound(GetDevicePixelRatioForWindow(msg->hwnd) *
data->windowData.minimumSize.height()); data->windowData.minimumSize.height());
} }
*result = 0; *result = 0;
@ -1410,7 +1415,7 @@ int WinNativeEventFilter::getSystemMetric(HWND handle, SystemMetric metric,
case SystemMetric::BorderWidth: { case SystemMetric::BorderWidth: {
const int bw = userData->windowData.borderWidth; const int bw = userData->windowData.borderWidth;
if (bw > 0) { if (bw > 0) {
return std::round(bw * dpr); return qRound(bw * dpr);
} else { } else {
const int result = m_lpGetSystemMetrics(SM_CXSIZEFRAME) + const int result = m_lpGetSystemMetrics(SM_CXSIZEFRAME) +
m_lpGetSystemMetrics(SM_CXPADDEDBORDER); m_lpGetSystemMetrics(SM_CXPADDEDBORDER);
@ -1423,7 +1428,7 @@ int WinNativeEventFilter::getSystemMetric(HWND handle, SystemMetric metric,
case SystemMetric::BorderHeight: { case SystemMetric::BorderHeight: {
const int bh = userData->windowData.borderHeight; const int bh = userData->windowData.borderHeight;
if (bh > 0) { if (bh > 0) {
return std::round(bh * dpr); return qRound(bh * dpr);
} else { } else {
const int result = m_lpGetSystemMetrics(SM_CYSIZEFRAME) + const int result = m_lpGetSystemMetrics(SM_CYSIZEFRAME) +
m_lpGetSystemMetrics(SM_CXPADDEDBORDER); m_lpGetSystemMetrics(SM_CXPADDEDBORDER);
@ -1436,7 +1441,7 @@ int WinNativeEventFilter::getSystemMetric(HWND handle, SystemMetric metric,
case SystemMetric::TitleBarHeight: { case SystemMetric::TitleBarHeight: {
const int tbh = userData->windowData.titlebarHeight; const int tbh = userData->windowData.titlebarHeight;
if (tbh > 0) { if (tbh > 0) {
return std::round(tbh * dpr); return qRound(tbh * dpr);
} else { } else {
const int result = m_lpGetSystemMetrics(SM_CYSIZEFRAME) + const int result = m_lpGetSystemMetrics(SM_CYSIZEFRAME) +
m_lpGetSystemMetrics(SM_CXPADDEDBORDER) + m_lpGetSystemMetrics(SM_CXPADDEDBORDER) +
@ -1453,19 +1458,19 @@ int WinNativeEventFilter::getSystemMetric(HWND handle, SystemMetric metric,
switch (metric) { switch (metric) {
case SystemMetric::BorderWidth: { case SystemMetric::BorderWidth: {
if (m_borderWidth > 0) { if (m_borderWidth > 0) {
return std::round(m_borderWidth * dpr); return qRound(m_borderWidth * dpr);
} }
break; break;
} }
case SystemMetric::BorderHeight: { case SystemMetric::BorderHeight: {
if (m_borderHeight > 0) { if (m_borderHeight > 0) {
return std::round(m_borderHeight * dpr); return qRound(m_borderHeight * dpr);
} }
break; break;
} }
case SystemMetric::TitleBarHeight: { case SystemMetric::TitleBarHeight: {
if (m_titlebarHeight > 0) { if (m_titlebarHeight > 0) {
return std::round(m_titlebarHeight * dpr); return qRound(m_titlebarHeight * dpr);
} }
break; break;
} }
@ -1483,8 +1488,8 @@ void WinNativeEventFilter::setWindowGeometry(HWND handle, const int x,
// sends the WM_WINDOWPOSCHANGING, WM_WINDOWPOSCHANGED, WM_MOVE, // sends the WM_WINDOWPOSCHANGING, WM_WINDOWPOSCHANGED, WM_MOVE,
// WM_SIZE, and WM_NCCALCSIZE messages to the window. // WM_SIZE, and WM_NCCALCSIZE messages to the window.
// SetWindowPos only sends WM_WINDOWPOSCHANGED. // SetWindowPos only sends WM_WINDOWPOSCHANGED.
m_lpMoveWindow(handle, x, y, std::round(width * dpr), m_lpMoveWindow(handle, x, y, qRound(width * dpr), qRound(height * dpr),
std::round(height * dpr), TRUE); TRUE);
} }
} }
@ -1501,7 +1506,7 @@ void WinNativeEventFilter::moveWindowToDesktopCenter(HWND handle) {
monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top; monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top;
const LONG ww = windowInfo.rcWindow.right - windowInfo.rcWindow.left; const LONG ww = windowInfo.rcWindow.right - windowInfo.rcWindow.left;
const LONG wh = windowInfo.rcWindow.bottom - windowInfo.rcWindow.top; const LONG wh = windowInfo.rcWindow.bottom - windowInfo.rcWindow.top;
m_lpMoveWindow(handle, std::round((mw - ww) / 2.0), m_lpMoveWindow(handle, qRound((mw - ww) / 2.0), qRound((mh - wh) / 2.0),
std::round((mh - wh) / 2.0), ww, wh, TRUE); ww, wh, TRUE);
} }
} }