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


    直接上代码

    1. #include "QtWidgetsApplication4.h"
    2. #include <Windows.h>
    3. #include <qdebug.h>
    4. #include <qscreen.h>
    5. QtWidgetsApplication4::QtWidgetsApplication4(QWidget *parent)
    6. : QMainWindow(parent)
    7. {
    8. ui.setupUi(this);
    9. ///windows函数获取 start
    10. //HWND hwd = ::GetDesktopWindow();
    11. //HDC hdc = ::GetDC(hwd);
    12. //int nWidth1 = GetDeviceCaps(hdc, DESKTOPHORZRES); //屏幕宽
    13. //int nHeight1 = GetDeviceCaps(hdc, DESKTOPVERTRES); //屏幕高
    14. HDC hd = GetDC(NULL);
    15. int dotPix1 = GetDeviceCaps(hd, LOGPIXELSX);
    16. int dotPix2 = GetDeviceCaps(hd, LOGPIXELSY); //获取放大比例
    17. ///windows函数获取 end/
    18. //Qt///
    19. QScreen *screen = qApp->primaryScreen();
    20. int nWidth2 = screen->size().width(); //屏幕宽
    21. int nHeight2 = screen->size().height(); //屏幕高
    22. qreal dpiVal1 = screen->logicalDotsPerInch(); ///Qt获取放大比例
    23. qreal dpiVal2 = screen->logicalDotsPerInchX(); ///Qt获取放大比例
    24. }

    屏幕放大倍数 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 属性的平均值。

  • 相关阅读:
    CSS笔记——BFC(块级格式化上下文)
    第二证券|疫情扰动叠加需求不足,11月制造业PMI回落至48%
    三款免费的AI绘画网站对比分析,真正好用的居然是它
    动态列排序
    eyb:Redis学习(3)
    mysql将id重新修改为递增
    uni-app,安卓,微信小程序,H5,代码模块判断使用
    数组的基本概念和存储结构
    如何获取l2行情接口?
    Pytorch因nn.Parameter导致实验不可复现的一种情况
  • 原文地址:https://blog.csdn.net/hanxiaoyong_/article/details/125418274