Qt5 支持高清屏

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012020854/article/details/78422496
#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);
}

猜你喜欢

转载自blog.csdn.net/u012020854/article/details/78422496
Qt5