QT调用Window系统API获取屏幕分辩率及缩放比例

GetSystemMetrics()函数,获取屏幕的分辩率,这个分辩表示的是当前显示的分辨率。

通过设置标识符获取相应的值 ,例如,获取屏幕的宽度和高度

int width = GetSystemMetrics(SM_CXSCREEN); //屏幕宽度

int height = GetSystemMetrics(SM_CYSCREEN); //屏幕高度

 

GetDeviceCaps 获取指定设备的性能参数该方法将所取得的硬件设备信息保存到一个D3DCAPS9结构中

GetDeviceCaps 与GetSystemMetrics的区别的,GetDeviceCaps是设备的物理属性,而GetSystemMetrics是可调节的,

下面以2K的显示器(1920 X 1080)为例

把显示器的显示比例调成125%

这个时候,width  = 1536,  而GetDeviceCaps获取的宽度为1920,代码实现如下

int nScreenWidth = ::GetSystemMetrics(SM_CXSCREEN);
int nScreenHeight = ::GetSystemMetrics(SM_CYSCREEN);
qDebug() << "nScreenWidth=========" << nScreenWidth << "nScreenHeight====" << nScreenHeight;

HWND hwd = ::GetDesktopWindow();
HDC hdc = ::GetDC(hwd);
int width = GetDeviceCaps(hdc, DESKTOPHORZRES);
int height = GetDeviceCaps(hdc, DESKTOPVERTRES);

double dWidth = (double)width;
double dScreenWidth = (double)nScreenWidth;
double scale = dWidth / dScreenWidth;
qDebug() << "width=========" << width << "=nScreenWidth==" << nScreenWidth << "height====" << height << "==scale===" << scale;
qDebug() << "dWidth=========" << dWidth << "=dScreenWidth==" << dScreenWidth << "==scale===" << scale;
    
  
QApplication a(argc, argv);

运行结果

使用的时候在.cpp文件中要添加这几个头文件

#include <Windows.h>//这个一定要添加,不然会报"No Target Architecture" 错误
#include <WinUser.h>
#include <wingdi.h>

 

.pro文件中添加

win32 {
    msvc:LIBS += User32.lib
    msvc:LIBS += gdi32.lib
}

 

 

 

  • 3
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值