C++ 获取屏幕缩放比例

 【WIN10】查看系统屏幕缩放比例,在电脑桌面右击-->显示设置-->屏幕-->缩放与布局

float GetScreenDisplayScale()
{
#define TABLE_LEN 4
    const int zoom_table[TABLE_LEN] = {96,120,144,192};
    const float dpi_table[TABLE_LEN] = {1,1.25,1.5,2};

    HWND hd = GetDesktopWindow();
    int zoom = GetDpiForWindow(hd);

    float ret = 1.0;
    for (size_t i = 0; i < TABLE_LEN; i++)
    {
        if (zoom == zoom_table[i])
        {
            ret = dpi_table[i];
            break;
        }
    }

    printf("zoom = %d ,scale=%d%%.\n",zoom,(int)ret*100);
    return ret;
}

void DisplayScale()
{
    // 获取创口句柄
    HWND hWnd = GetDesktopWindow();
    HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
    // 获取监视器逻辑尺寸
    MONITORINFOEX miex;
    miex.cbSize = sizeof(miex);
    GetMonitorInfo(hMonitor, &miex);
    int cxLogical = (miex.rcMonitor.right - miex.rcMonitor.left);
    int cyLogical = (miex.rcMonitor.bottom - miex.rcMonitor.top);

    // 获取监视器物理宽度与高度
    DEVMODE dm;
    dm.dmSize = sizeof(dm);
    dm.dmDriverExtra = 0;
    EnumDisplaySettings(miex.szDevice, ENUM_CURRENT_SETTINGS, &dm);
    int cxPhysical = dm.dmPelsWidth;
    int cyPhysical = dm.dmPelsHeight;

    // 缩放比例计算  实际上使用任何一个即可
    double horzScale = ((double)cxPhysical / (double)cxLogical);
    double vertScale = ((double)cyPhysical / (double)cyLogical);
    printf("cxPhysical = %d px.cyPhysical = %d px.\n", cxPhysical, cyPhysical);
    printf("cxLogical = %d px.cyLogical = %d px.\n", cxLogical, cyLogical);
    printf("horzScale = %.2f ,vertScale = %.2f \n", horzScale, vertScale );

}

 

评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值