Qt5 支持高清屏
程序员文章站
2022-03-15 21:20:58
...
#ifdef Q_OS_WIN
#include <Windows.h>
#include <winuser.h>
#endif
//===============支持高清屏幕===============================
const float DEFAULT_DPI = 96.0;
#ifdef Q_OS_WIN
float winDpiScale()
{
HDC screen = GetDC(NULL);
FLOAT dpiX = static_cast<FLOAT>( GetDeviceCaps( screen, LOGPIXELSX ) );
ReleaseDC( 0, screen );
return dpiX / DEFAULT_DPI;
}
#endif
#ifndef DPI_ENUMS_DECLARED
typedef enum PROCESS_DPI_AWARENESS
{
PROCESS_DPI_UNAWARE = 0,
PROCESS_SYSTEM_DPI_AWARE = 1,
PROCESS_PER_MONITOR_DPI_AWARE = 2
} PROCESS_DPI_AWARENESS;
#endif
typedef BOOL (WINAPI * SETPROCESSDPIAWARE_T)(void);
typedef HRESULT (WINAPI * SETPROCESSDPIAWARENESS_T)(PROCESS_DPI_AWARENESS);
bool win32_SetProcessDpiAware(void) {
bool ret = false;
#ifdef Q_OS_WIN
#ifndef DPI_ENUMS_DECLARED
typedef enum PROCESS_DPI_AWARENESS
{
PROCESS_DPI_UNAWARE = 0,
PROCESS_SYSTEM_DPI_AWARE = 1,
PROCESS_PER_MONITOR_DPI_AWARE = 2
} PROCESS_DPI_AWARENESS;
#endif
typedef BOOL (WINAPI * SETPROCESSDPIAWARE_T)(void);
typedef HRESULT (WINAPI * SETPROCESSDPIAWARENESS_T)(PROCESS_DPI_AWARENESS);
HMODULE shcore = LoadLibraryA("Shcore.dll");
SETPROCESSDPIAWARENESS_T SetProcessDpiAwareness = NULL;
if (shcore) {
SetProcessDpiAwareness = (SETPROCESSDPIAWARENESS_T) GetProcAddress(shcore, "SetProcessDpiAwareness");
}
HMODULE user32 = LoadLibraryA("User32.dll");
SETPROCESSDPIAWARE_T SetProcessDPIAware = NULL;
if (user32) {
SetProcessDPIAware = (SETPROCESSDPIAWARE_T) GetProcAddress(user32, "SetProcessDPIAware");
}
qDebug()<<"SetProcessDpiAwareness---"<<*SetProcessDpiAwareness;
if (SetProcessDpiAwareness) {
ret = SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE) == S_OK;
} else if (SetProcessDPIAware) {
ret = SetProcessDPIAware() != 0;
}
if (user32) {
FreeLibrary(user32);
}
if (shcore) {
FreeLibrary(shcore);
}
#endif // Q_OS_WIN
#if QT_VERSION >= QT_VERSION_CHECK(5,6,0)
QCoreApplication::setAttribute(Qt::AA_DisableHighDpiScaling);
#else
float sca = winDpiScale();
sca = qRound(sca);
// qDebug()<<"sca:"<<sca;
QByteArray b = QByteArray::number(sca,'f',0);
qputenv("QT_DEVICE_PIXEL_RATIO",b);
#endif // QT_VERSION
return ret;
}
//-------------------
int main(int argc, char **argv) {
#ifdef Q_OS_WIN
//支持高清屏
win32_SetProcessDpiAware();
#endif
//高清屏要在之上
QApplication app(argc,argv);
}
推荐阅读
-
Win10 Redstone2 Build 15002首发:支持高DPI/高分屏支持更完美
-
Redmi K40系列曝光:OLED柔性挖孔直屏 支持120Hz高刷
-
C#请求唯一性校验支持高并发的实现方法
-
高通为iQOO 3发声:骁龙865率先支持LPDDR5和UFS 3.1
-
小米10全系支持90帧高帧率模式:5款手游大作已适配
-
高通新旗舰芯片骁龙8150确认7nm:支持5G 第四季度量产
-
60Hz屏已落后!七款手游大作支持一加7T 90Hz高刷新率模式
-
小米11支持开启全局120Hz高刷:流畅度大提升
-
高通总裁:只要苹果开口、愿意向5G版iPhone提供支持
-
小米笔记本65W充电器发布:支持高通QC2.0快充/携带更方便