Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-04-08 17:28:54 +08:00
parent 30dc4892f1
commit 22c0aa26df
1 changed files with 39 additions and 52 deletions

View File

@ -112,13 +112,19 @@
#define WM_DPICHANGED 0x02E0 #define WM_DPICHANGED 0x02E0
#endif #endif
#ifndef WNEF_GENERATE_WINAPI
#define WNEF_GENERATE_WINAPI(funcName, resultType, ...) \
using _WNEF_WINAPI_##funcName = resultType (WINAPI *) (__VA_ARGS__); \
_WNEF_WINAPI_##funcName m_lp##funcName = nullptr;
#endif
#ifndef WNEF_RESOLVE_WINAPI #ifndef WNEF_RESOLVE_WINAPI
#define WNEF_RESOLVE_WINAPI(libName, funcName) \ #define WNEF_RESOLVE_WINAPI(libName, funcName) \
if (!m_##funcName) { \ if (!m_lp##funcName) { \
QLibrary library(QString::fromUtf8(#libName)); \ QLibrary library(QString::fromUtf8(#libName)); \
m_##funcName = \ m_lp##funcName = \
reinterpret_cast<lp##funcName>(library.resolve(#funcName)); \ reinterpret_cast<_WNEF_WINAPI_##funcName>(library.resolve(#funcName)); \
if (!m_##funcName) { \ if (!m_lp##funcName) { \
qWarning().noquote() \ qWarning().noquote() \
<< "Failed to resolve" << #funcName << "from" << #libName \ << "Failed to resolve" << #funcName << "from" << #libName \
<< "-->" << library.errorString(); \ << "-->" << library.errorString(); \
@ -126,6 +132,15 @@
} }
#endif #endif
#ifndef WNEF_USE_WINAPI
#define WNEF_USE_WINAPI(funcName, ...) \
if (m_lp##funcName) {\
m_lp##funcName(__VA_ARGS__);\
} else {\
qWarning().noquote() << "Uninitialized function:" << #funcName;\
}
#endif
namespace { namespace {
using WINDOWTHEMEATTRIBUTETYPE = enum _WINDOWTHEMEATTRIBUTETYPE { using WINDOWTHEMEATTRIBUTETYPE = enum _WINDOWTHEMEATTRIBUTETYPE {
@ -199,52 +214,24 @@ using APPBARDATA = struct _APPBARDATA {
LPARAM lParam; LPARAM lParam;
}; };
using lpGetSystemDpiForProcess = UINT(WINAPI *)(HANDLE); WNEF_GENERATE_WINAPI(GetSystemDpiForProcess, UINT, HANDLE)
lpGetSystemDpiForProcess m_GetSystemDpiForProcess = nullptr; WNEF_GENERATE_WINAPI(GetDpiForWindow, UINT, HWND)
WNEF_GENERATE_WINAPI(GetDpiForSystem, UINT)
using lpGetDpiForWindow = UINT(WINAPI *)(HWND); WNEF_GENERATE_WINAPI(GetSystemMetricsForDpi, int, int, UINT)
lpGetDpiForWindow m_GetDpiForWindow = nullptr; WNEF_GENERATE_WINAPI(GetDpiForMonitor, HRESULT, HMONITOR, MONITOR_DPI_TYPE, UINT *,
UINT *)
using lpGetDpiForSystem = UINT(WINAPI *)(); WNEF_GENERATE_WINAPI(SetWindowCompositionAttribute, BOOL, HWND, const WINDOWCOMPOSITIONATTRIBDATA *)
lpGetDpiForSystem m_GetDpiForSystem = nullptr; WNEF_GENERATE_WINAPI(SetWindowThemeAttribute, HRESULT, HWND,
using lpGetSystemMetricsForDpi = int(WINAPI *)(int, UINT);
lpGetSystemMetricsForDpi m_GetSystemMetricsForDpi = nullptr;
using lpGetDpiForMonitor = HRESULT(WINAPI *)(HMONITOR, MONITOR_DPI_TYPE, UINT *,
UINT *);
lpGetDpiForMonitor m_GetDpiForMonitor = nullptr;
using lpSetWindowCompositionAttribute =
BOOL(WINAPI *)(HWND, const WINDOWCOMPOSITIONATTRIBDATA *);
lpSetWindowCompositionAttribute m_SetWindowCompositionAttribute = nullptr;
using lpSetWindowThemeAttribute = HRESULT(WINAPI *)(HWND,
WINDOWTHEMEATTRIBUTETYPE, WINDOWTHEMEATTRIBUTETYPE,
PVOID, DWORD); PVOID, DWORD)
lpSetWindowThemeAttribute m_SetWindowThemeAttribute = nullptr; WNEF_GENERATE_WINAPI(IsThemeActive, BOOL)
WNEF_GENERATE_WINAPI(DwmEnableBlurBehindWindow, HRESULT, HWND,
using lpIsThemeActive = BOOL(WINAPI *)(); const DWM_BLURBEHIND *)
lpIsThemeActive m_IsThemeActive = nullptr; WNEF_GENERATE_WINAPI(DwmExtendFrameIntoClientArea, HRESULT, HWND, const MARGINS *)
WNEF_GENERATE_WINAPI(DwmIsCompositionEnabled, HRESULT, BOOL *)
using lpDwmEnableBlurBehindWindow = HRESULT(WINAPI *)(HWND, WNEF_GENERATE_WINAPI(DwmSetWindowAttribute, HRESULT, HWND, DWORD, LPCVOID, DWORD)
const DWM_BLURBEHIND *); WNEF_GENERATE_WINAPI(SHAppBarMessage, UINT_PTR, DWORD, APPBARDATA *)
lpDwmEnableBlurBehindWindow m_DwmEnableBlurBehindWindow = nullptr; WNEF_GENERATE_WINAPI(GetDeviceCaps, int, HDC, int)
using lpDwmExtendFrameIntoClientArea = HRESULT(WINAPI *)(HWND, const MARGINS *);
lpDwmExtendFrameIntoClientArea m_DwmExtendFrameIntoClientArea = nullptr;
using lpDwmIsCompositionEnabled = HRESULT(WINAPI *)(BOOL *);
lpDwmIsCompositionEnabled m_DwmIsCompositionEnabled = nullptr;
using lpDwmSetWindowAttribute = HRESULT(WINAPI *)(HWND, DWORD, LPCVOID, DWORD);
lpDwmSetWindowAttribute m_DwmSetWindowAttribute = nullptr;
using lpSHAppBarMessage = UINT_PTR(WINAPI *)(DWORD, APPBARDATA *);
lpSHAppBarMessage m_SHAppBarMessage = nullptr;
using lpGetDeviceCaps = int(WINAPI *)(HDC, int);
lpGetDeviceCaps m_GetDeviceCaps = nullptr;
const UINT m_defaultDotsPerInch = USER_DEFAULT_SCREEN_DPI; const UINT m_defaultDotsPerInch = USER_DEFAULT_SCREEN_DPI;
@ -914,8 +901,8 @@ qreal WinNativeEventFilter::getDevicePixelRatioForWindow(HWND handle) {
int WinNativeEventFilter::getSystemMetricsForWindow(HWND handle, int index) { int WinNativeEventFilter::getSystemMetricsForWindow(HWND handle, int index) {
if (m_GetSystemMetricsForDpi) { if (m_GetSystemMetricsForDpi) {
return m_GetSystemMetricsForDpi(index, return m_GetSystemMetricsForDpi(index,
static_cast<UINT>(getPreferedNumber( static_cast<UINT>(std::round(getPreferedNumber(
getDotsPerInchForWindow(handle)))); getDotsPerInchForWindow(handle)))));
} else { } else {
return std::round(GetSystemMetrics(index) * getDevicePixelRatioForWindow(handle)); return std::round(GetSystemMetrics(index) * getDevicePixelRatioForWindow(handle));
} }