Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-04-13 20:33:54 +08:00
parent eefb3feb44
commit 6bba0cbe18
2 changed files with 86 additions and 55 deletions

View File

@ -339,59 +339,15 @@ void WinNativeEventFilter::clearFramelessWindows() {
} }
int WinNativeEventFilter::borderWidth(HWND handle) { int WinNativeEventFilter::borderWidth(HWND handle) {
initWin32Api(); return getBorderWidthForWindow(handle, false);
if (handle && m_lpIsWindow(handle)) {
createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>(
m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int bw = userData->windowData.borderWidth;
if (bw > 0) {
return std::round(bw * getDevicePixelRatioForWindow(handle));
}
}
if (m_borderWidth > 0) {
return std::round(m_borderWidth * getDevicePixelRatioForWindow(handle));
}
return getSystemMetricsForWindow(handle, SM_CXSIZEFRAME) +
getSystemMetricsForWindow(handle, SM_CXPADDEDBORDER);
} }
int WinNativeEventFilter::borderHeight(HWND handle) { int WinNativeEventFilter::borderHeight(HWND handle) {
initWin32Api(); return getBorderHeightForWindow(handle, false);
if (handle && m_lpIsWindow(handle)) {
createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>(
m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int bh = userData->windowData.borderHeight;
if (bh > 0) {
return std::round(bh * getDevicePixelRatioForWindow(handle));
}
}
if (m_borderHeight > 0) {
return std::round(m_borderHeight *
getDevicePixelRatioForWindow(handle));
}
return getSystemMetricsForWindow(handle, SM_CYSIZEFRAME) +
getSystemMetricsForWindow(handle, SM_CXPADDEDBORDER);
} }
int WinNativeEventFilter::titlebarHeight(HWND handle) { int WinNativeEventFilter::titlebarHeight(HWND handle) {
initWin32Api(); return getTitlebarHeightForWindow(handle, false);
if (handle && m_lpIsWindow(handle)) {
createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>(
m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int tbh = userData->windowData.titlebarHeight;
if (tbh > 0) {
return std::round(tbh * getDevicePixelRatioForWindow(handle));
}
}
if (m_titlebarHeight > 0) {
return std::round(m_titlebarHeight *
getDevicePixelRatioForWindow(handle));
}
return borderHeight(handle) +
getSystemMetricsForWindow(handle, SM_CYCAPTION);
} }
#if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
@ -440,6 +396,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
// disabled, it's designed to be, don't force the window to draw a frame // disabled, it's designed to be, don't force the window to draw a frame
// shadow in that case. // shadow in that case.
updateGlass(msg->hwnd); updateGlass(msg->hwnd);
#ifdef _DEBUG
// For debug purposes. // For debug purposes.
qDebug().noquote() << "Window handle:" << msg->hwnd; qDebug().noquote() << "Window handle:" << msg->hwnd;
qDebug().noquote() << "Window DPI:" qDebug().noquote() << "Window DPI:"
@ -450,6 +407,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
<< "Window border height:" << borderHeight(msg->hwnd) << "Window border height:" << borderHeight(msg->hwnd)
<< "Window titlebar height:" << "Window titlebar height:"
<< titlebarHeight(msg->hwnd); << titlebarHeight(msg->hwnd);
#endif
} }
switch (msg->message) { switch (msg->message) {
case WM_NCCALCSIZE: { case WM_NCCALCSIZE: {
@ -467,8 +425,8 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
if (IsMaximized(_hWnd) || IsFullScreened(_hWnd)) { if (IsMaximized(_hWnd) || IsFullScreened(_hWnd)) {
// Windows automatically adds a standard width border to all // Windows automatically adds a standard width border to all
// sides when a window is maximized. // sides when a window is maximized.
int frameThickness_x = borderWidth(_hWnd); int frameThickness_x = getBorderWidthForWindow(_hWnd);
int frameThickness_y = borderHeight(_hWnd); int frameThickness_y = getBorderHeightForWindow(_hWnd);
// The following two lines are two seperate functions in // The following two lines are two seperate functions in
// Chromium, it uses them to judge whether the window // Chromium, it uses them to judge whether the window
// should draw it's own frame or not. But here we will always // should draw it's own frame or not. But here we will always
@ -611,9 +569,9 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
mouse.y = GET_Y_LPARAM(_lParam); mouse.y = GET_Y_LPARAM(_lParam);
m_lpScreenToClient(_hWnd, &mouse); m_lpScreenToClient(_hWnd, &mouse);
// These values are DPI-aware. // These values are DPI-aware.
const LONG bw = borderWidth(_hWnd); const LONG bw = getBorderWidthForWindow(_hWnd);
const LONG bh = borderHeight(_hWnd); const LONG bh = getBorderHeightForWindow(_hWnd);
const LONG tbh = titlebarHeight(_hWnd); const LONG tbh = getTitlebarHeightForWindow(_hWnd);
const qreal dpr = getDevicePixelRatioForWindow(_hWnd); const qreal dpr = getDevicePixelRatioForWindow(_hWnd);
const bool isTitlebar = (mouse.y < tbh) && const bool isTitlebar = (mouse.y < tbh) &&
!isInSpecificAreas(mouse.x, mouse.y, !isInSpecificAreas(mouse.x, mouse.y,
@ -1038,3 +996,73 @@ void WinNativeEventFilter::updateWindow(HWND handle, bool triggerFrameChange) {
RDW_INVALIDATE | RDW_UPDATENOW | RDW_NOCHILDREN); RDW_INVALIDATE | RDW_UPDATENOW | RDW_NOCHILDREN);
} }
} }
int WinNativeEventFilter::getBorderWidthForWindow(HWND handle, bool dpiAware) {
initWin32Api();
const qreal dpr = dpiAware ? getDevicePixelRatioForWindow(handle)
: m_defaultDevicePixelRatio;
if (handle && m_lpIsWindow(handle)) {
createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>(
m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int bw = userData->windowData.borderWidth;
if (bw > 0) {
return std::round(bw * dpr);
}
}
if (m_borderWidth > 0) {
return std::round(m_borderWidth * dpr);
}
const int result = m_lpGetSystemMetrics(SM_CXSIZEFRAME) +
m_lpGetSystemMetrics(SM_CXPADDEDBORDER);
const int result_dpi = getSystemMetricsForWindow(handle, SM_CXSIZEFRAME) +
getSystemMetricsForWindow(handle, SM_CXPADDEDBORDER);
return dpiAware ? result_dpi : result;
}
int WinNativeEventFilter::getBorderHeightForWindow(HWND handle, bool dpiAware) {
initWin32Api();
const qreal dpr = dpiAware ? getDevicePixelRatioForWindow(handle)
: m_defaultDevicePixelRatio;
if (handle && m_lpIsWindow(handle)) {
createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>(
m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int bh = userData->windowData.borderHeight;
if (bh > 0) {
return std::round(bh * dpr);
}
}
if (m_borderHeight > 0) {
return std::round(m_borderHeight * dpr);
}
const int result = m_lpGetSystemMetrics(SM_CYSIZEFRAME) +
m_lpGetSystemMetrics(SM_CXPADDEDBORDER);
const int result_dpi = getSystemMetricsForWindow(handle, SM_CYSIZEFRAME) +
getSystemMetricsForWindow(handle, SM_CXPADDEDBORDER);
return dpiAware ? result_dpi : result;
}
int WinNativeEventFilter::getTitlebarHeightForWindow(HWND handle,
bool dpiAware) {
initWin32Api();
const qreal dpr = dpiAware ? getDevicePixelRatioForWindow(handle)
: m_defaultDevicePixelRatio;
if (handle && m_lpIsWindow(handle)) {
createUserData(handle);
const auto userData = reinterpret_cast<WINDOW *>(
m_lpGetWindowLongPtrW(handle, GWLP_USERDATA));
const int tbh = userData->windowData.titlebarHeight;
if (tbh > 0) {
return std::round(tbh * dpr);
}
}
if (m_titlebarHeight > 0) {
return std::round(m_titlebarHeight * dpr);
}
const int result = getBorderHeightForWindow(handle, false) +
m_lpGetSystemMetrics(SM_CYCAPTION);
const int result_dpi = getBorderHeightForWindow(handle) +
getSystemMetricsForWindow(handle, SM_CYCAPTION);
return dpiAware ? result_dpi : result;
}

View File

@ -84,13 +84,13 @@ public:
static void setBorderHeight(int bh); static void setBorderHeight(int bh);
static void setTitlebarHeight(int tbh); static void setTitlebarHeight(int tbh);
// DPI-aware border width of the given window (if the pointer is null, // DPI-unaware border width of the given window (if the pointer is null,
// return the system's standard value). // return the system's standard value).
static int borderWidth(HWND handle); static int borderWidth(HWND handle);
// DPI-aware border height of the given window (if the pointer is null, // DPI-unaware 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 // DPI-unaware titlebar height (including the border height) of the given
// window (if the pointer is null, 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);
@ -112,4 +112,7 @@ private:
static UINT getDotsPerInchForWindow(HWND handle); static UINT getDotsPerInchForWindow(HWND handle);
static qreal getDevicePixelRatioForWindow(HWND handle); static qreal getDevicePixelRatioForWindow(HWND handle);
static int getSystemMetricsForWindow(HWND handle, int index); static int getSystemMetricsForWindow(HWND handle, int index);
static int getBorderWidthForWindow(HWND handle, bool dpiAware = true);
static int getBorderHeightForWindow(HWND handle, bool dpiAware = true);
static int getTitlebarHeightForWindow(HWND handle, bool dpiAware = true);
}; };