查询Windows显示缩放比例

2024-01-24

I want to query the Windows display scaling setting programmatically: In this case, I want it to return 125 since I configured my display to be at 125% scaling. According to this https://learn.microsoft.com/en-us/windows-hardware/manufacture/desktop/dpi-related-apis-and-registry-settings#scale article, the following Windows API C++ code can be used:

// Get desktop dc
desktopDc = GetDC(NULL);
// Get native resolution
horizontalDPI = GetDeviceCaps(desktopDc,LOGPIXELSX);
verticalDPI = GetDeviceCaps(desktopDc,LOGPIXELSY);

However, this code always return 96 and 96 for the horizontal as well as vertical DPI which translates to 100% scaling (according to the provided table):

这个输出是错误的,因为我仍然得到相同的结果125%缩放。如何做呢?我正在编程Java所以我可以执行C++ using JNA https://github.com/java-native-access/jna. Windows API解决方案是首选,但其他一切都像.bat脚本或registry查询也可以,只要它对所有人都是可靠的Windows版本来自7 to 10.


This https://stackoverflow.com/a/36864741/3764804答案解决了它:

#include "pch.h"
#include <iostream>
#include <windows.h>

int main()
{
    auto activeWindow = GetActiveWindow();
    HMONITOR monitor = MonitorFromWindow(activeWindow, MONITOR_DEFAULTTONEAREST);

    // Get the logical width and height of the monitor
    MONITORINFOEX monitorInfoEx;
    monitorInfoEx.cbSize = sizeof(monitorInfoEx);
    GetMonitorInfo(monitor, &monitorInfoEx);
    auto cxLogical = monitorInfoEx.rcMonitor.right - monitorInfoEx.rcMonitor.left;
    auto cyLogical = monitorInfoEx.rcMonitor.bottom - monitorInfoEx.rcMonitor.top;

    // Get the physical width and height of the monitor
    DEVMODE devMode;
    devMode.dmSize = sizeof(devMode);
    devMode.dmDriverExtra = 0;
    EnumDisplaySettings(monitorInfoEx.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
    auto cxPhysical = devMode.dmPelsWidth;
    auto cyPhysical = devMode.dmPelsHeight;

    // Calculate the scaling factor
    auto horizontalScale = ((double) cxPhysical / (double) cxLogical);
    auto verticalScale = ((double) cyPhysical / (double) cyLogical);

    std::cout << "Horizonzal scaling: " << horizontalScale << "\n";
    std::cout << "Vertical scaling: " << verticalScale;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

查询Windows显示缩放比例 的相关文章

随机推荐

  • 如何仅允许公开访问 firestore 文档的特定字段

    假设我在 firestore 数据库中有这个结构 collection document x x y y 我有这个 firebase 规则 service cloud firestore match databases database d
  • 第一次测试 Laravel 4 应用程序(PHPSpec/BDD 与 PHPUnit/TDD)

    我已经为这个问题苦苦思索太久了 我知道我需要跳入其中一个或另一个 因为它们显然都是可行 有用的工具 但几周来一直处于观望状态 对两者进行研究 PHPUnit http phpunit de vs PHPSpec http phpspec n
  • UNIX 数据报套接字向发送方返回数据

    有件事我无法集中注意力 我创建了一个unix数据报套接字 self socket socket socket socket AF UNIX socket SOCK DGRAM self socket bind SOCKET FILE 稍后在
  • VS 中插入字符串的热键或自动扩展

    在 VS 2015 2017 中输入 C 内插字符串时 我想节省一些击键次数 据我所知 没有任何内置功能 这里有些相关的问题 自动插入字符串 https stackoverflow com q 44166762 3546415 我现在正在确
  • Java 编译错误限制为 100

    我有一个 Java 文件 当我编译该文件时 在 java 编译器 javac 退出后 我将只能在控制台上看到前 100 个错误 我如何才能在控制台上看到所有编译错误 预先感谢 opensid 一般来说编译器会在100个错误后放弃 此后的大多
  • 为了一致性,控制台输出应该在每次计算时调用该函数?

    当我添加到图表或保存时 我的脚本最近刚刚开始在控制台中显示这些行 The function anonym function 10 should be called on each calculation for consistency It
  • 在 C++ 中使用 rand() 函数的正确方法是什么?

    我正在做一本书的练习 要求编写一个生成伪随机数的程序 我从简单开始 include std lib facilities h int randint int random 0 random rand return random int ma
  • ++ 和 *+ 是什么意思?

    我正在将一些库从 PHP 移植到 JavaScript 我遇到了这个正则表达式 其中的某些部分我不清楚 lt n n n lt gt gt lt 1 4 不清楚的部分是 我知道 这个表达式应该接受像这样的字符串 title class st
  • 除 1/n 总是返回 0.0 [重复]

    这个问题在这里已经有答案了 我正在尝试计算p1 1 1 1 2 1 n 但出了点问题printf给我0 000 0 include
  • R:计算两个点层之间的最短距离

    我需要计算两个点矩阵之间的最短距离 我是 R 新手 不知道如何做到这一点 这是我用来调用数据并将其转换为点的代码 library dismo laurus lt gbif Laurus nobilis locs lt subset laur
  • 由于 order by 超出了 bigquery 资源限制

    当我运行以下查询时 出现 超出资源限制 错误 如果我删除最后一行 order by 子句 它会起作用 SELECT id INTEGER position CASE WHEN fallback 0 THEN 2 ELSE 1 END AS
  • 网站目录之外的参考文件

    如何引用网站根目录之外的文件 例如我的网站位于 C dev TestSite 我正在使用 ASP NET 和 XSP Web 应用程序将使用 mod mono 部署在 Apache 上 我在 C images 中有图像 我想这样做 img
  • Unicode HTML 标题在 IE、Chrome 中显示为框

    这是我的 HTML 另请参阅http www caudillweb com test test htm http www caudillweb com test test htm h1 日历 h1 以下是我分别在 IE Chrome 和 F
  • 如何在 TensorFlow 中水平连接两个张量?

    我有 2 个形状张量 100 4 and 100 2 我想在 TensorFlow 中执行串联操作 类似于np hstack 在 NumPy 中 这样输出的形状 100 6 有 TensorFlow 函数可以做到这一点吗 您可以使用tf c
  • 如何使用vimeo高级API显示视频

    我使用 Vimeo simple API 来显示我网站上某个频道的视频 但您可能知道 它有一个限制 我想知道您能否给我一个如何使用高级 API 的示例 我已阅读文档 但我只是不知道如何使用这些方法 显然我不是 php 专家 因此 如果您能给
  • WPF 列表框按钮所选项目

    我有一个带有一些文本块和一个按钮的列表框 在按钮的代码隐藏中 它调用一个传递当前选定的列表框项目的方法 这非常有用 问题是 当我选择一个项目 然后单击另一个项目上的按钮时 它不会更新 SelectedItem 属性 有没有办法 Xaml 或
  • 如何在C++中调用所有基类的复制构造函数来复制钻石继承中的大多数派生类对象?

    考虑下面的代码 include
  • 需要图像编辑库的建议

    我正在尝试创建一个基于 WPF 的应用程序 该应用程序将允许用户编辑照片并提供以下功能 增加 减少亮度 Crop 复古外观 还有更多 我还期待提供更多奇特的功能 例如 构图 打印到 T 恤 但这些功能的优先级较低 有相关的开源库吗 P S
  • 在 C 中使用 #define 定义路径

    我想定义这样的路径 define PATH abc xyz lmn 该 PATH 是一个包含文件 foo1 foo2 foo3 foo115 的目录 如何在 open 调用中使用这个 define 来打开 foo1 foo2 foo115
  • 查询Windows显示缩放比例

    I want to query the Windows display scaling setting programmatically In this case I want it to return 125 since I config