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 );

}

 

你可能感兴趣的:(c++,开发语言)