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

直接上代码

#include "QtWidgetsApplication4.h"
#include 
#include 
#include 
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 属性的平均值。

你可能感兴趣的:(C/C++语言,Qt,qt,ui,开发语言)