C++/Qt获取屏幕尺寸和放大比例

直接上代码

#include "QtWidgetsApplication4.h"
#include <Windows.h>
#include <qdebug.h>
#include <qscreen.h>
QtWidgetsApplication4::QtWidgetsApplication4(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
	///windows函数获取 start
	//HWND hwd = ::GetDesktopWindow();
	//HDC hdc = ::GetDC(hwd);
	//int nWidth1 = GetDeviceCaps(hdc, DESKTOPHORZRES);	//屏幕宽
	//int nHeight1 = GetDeviceCaps(hdc, DESKTOPVERTRES);	//屏幕高
	HDC hd = GetDC(NULL);
	int dotPix1 = GetDeviceCaps(hd, LOGPIXELSX);
	int dotPix2 = GetDeviceCaps(hd, LOGPIXELSY);	//获取放大比例
	///windows函数获取 end/


	//Qt///
	QScreen *screen = qApp->primaryScreen();
	int nWidth2 = screen->size().width();			//屏幕宽
	int nHeight2 = screen->size().height();			//屏幕高
	qreal dpiVal1 = screen->logicalDotsPerInch();	///Qt获取放大比例
	qreal dpiVal2 = screen->logicalDotsPerInchX();	///Qt获取放大比例
	
}

屏幕放大倍数 100% 125% 150% 200%,对应的dpiVal值为:96、 120、 144 、192。

即上面变量dotPix1、dotPix2、dotPix3、dotPix4获取的值都是96 120 144 192其中的一个值。

函数介绍

1、logicalDotsPerInchX : const qreal

This property holds the number of logical dots or pixels per inch in the horizontal direction

This value is used to convert font point sizes to pixel sizes.

 此属性保存每英寸的逻辑点数或像素数。 此值可用于将字体点大小转换为像素大小。这是一个方便的属性,它只是logicalDotsPerInchX 和logicalDotsPerInchY 属性的平均值。

2、logicalDotsPerInch : const qreal

This property holds the number of logical dots or pixels per inch

This value can be used to convert font point sizes to pixel sizes.

This is a convenience property that's simply the average of the logicalDotsPerInchX and logicalDotsPerInchY properties.

此属性保存每英寸的逻辑点数或像素数 此值可用于将字体点大小转换为像素大小。 这是一个方便属性,它只是logicalDotsPerInchX 和logicalDotsPerInchY 属性的平均值。

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值