Python如何来识别两个显示器屏幕各自的分辨率

在使用Python做桌面开发时如何识别两个显示器屏幕的分辨率

很多朋友应该在做桌面开发时会遇到多屏下如何解决布局问题,下面实现的就是获取准确的屏幕分辨率由于这是获取的两个屏幕的分辨率,所以三个屏幕的话可以在这基础上进行逻辑思考

当前显示器的主流分辨率:

分辨率宽高
HD720p1280*720
FHD1080p1920*1080
QHD 2K2560*1440
UHD 4K3840*2160
QUHD 8K7680*4320

下面是实现代码

# -*- coding: utf-8 -*-
"""
功能:识别两块显示器各自的分辨率
"""
"""模块导入"""
from win32api import GetSystemMetrics
from win32con import SM_CMONITORS, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN

#显示器数量检测
MonitorNumber = GetSystemMetrics(SM_CMONITORS)
#主屏幕尺寸检测
MajorScreenWidth=GetSystemMetrics(0)#主屏幕宽
MajorScreenHeight=GetSystemMetrics(1)#主屏幕高
print("主屏幕尺寸:", GetSystemMetrics(0),"*", GetSystemMetrics(1))
# 屏幕最大尺寸
aScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN)  # 屏幕最大宽度
aScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN)  # 屏幕最大高度
print("屏幕总尺寸:",aScreenWidth,"*",aScreenHeight)
#当前主流的分辨率基数是宽,偶数是高
ResolvingPower=[1280,720,1920,1080,2560,1440,3840,2160,4096,2160,7680,4320]

if MonitorNumber > 1:#屏幕数量判断print(MonitorNumber)就可以知道有多少块屏幕
    SecondaryScreenWidth=aScreenWidth-MajorScreenWidth#副屏宽=总屏幕宽-当前屏幕宽
    # print("副屏宽",SecondaryScreenWidth)

    #主屏横竖屏检测
    if GetSystemMetrics(0)>GetSystemMetrics(1):
        print("主屏幕是横屏")
    else:
        print("主屏幕是竖屏")

    #横屏状态
    for i in range(0, len(ResolvingPower) - 1,2):
        # print("i",ResolvingPower[i])
        if SecondaryScreenWidth == ResolvingPower[i]:
            print("副屏(横屏)尺寸:", ResolvingPower[i], ResolvingPower[i + 1])
            break
    #竖屏状态
    for i in range(1, len(ResolvingPower) - 1,2):
        # print("i",ResolvingPower[i])
        if SecondaryScreenWidth == ResolvingPower[i]:
            print("副屏(竖屏)尺寸:", ResolvingPower[i], ResolvingPower[i - 1])
            break

为了方便还是将其模块化吧

# -*- coding: utf-8 -*-
"""
功能:识别两块显示器各自的分辨率
"""
"""模块导入"""
from win32api import GetSystemMetrics
from win32con import SM_CMONITORS, SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN

def Display_Detection():
    # 显示器数量检测
    MonitorNumber = GetSystemMetrics(SM_CMONITORS)
    # 主屏幕尺寸检测
    MajorScreenWidth = GetSystemMetrics(0)  # 主屏幕宽
    MajorScreenHeight = GetSystemMetrics(1)  # 主屏幕高
    # print("主屏幕尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))
    # 屏幕最大尺寸
    aScreenWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN)  # 屏幕最大宽度
    aScreenHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN)  # 屏幕最大高度
    AllScreen=(aScreenWidth, aScreenHeight)
    # print("屏幕总尺寸:", aScreenWidth, "*", aScreenHeight)
    # 当前主流的分辨率基数是宽,偶数是高
    ResolvingPower = [1280, 720, 1920, 1080, 2560, 1440, 3840, 2160, 4096, 2160, 7680, 4320]

    if MonitorNumber > 1:  # 屏幕数量判断print(MonitorNumber)就可以知道有多少块屏幕
        SecondaryScreenWidth = aScreenWidth - MajorScreenWidth  # 副屏宽=总屏幕宽-当前屏幕宽
        # print("副屏宽",SecondaryScreenWidth)

        # 主屏横竖屏检测
        if GetSystemMetrics(0) > GetSystemMetrics(1):
            MianScreen = (GetSystemMetrics(0), GetSystemMetrics(1))
            # print("主屏(横屏)尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))
        else:
            MianScreen = (GetSystemMetrics(0), GetSystemMetrics(1))
            # print("主屏(竖屏)尺寸:", GetSystemMetrics(0), "*", GetSystemMetrics(1))

        # 横屏状态
        for i in range(0, len(ResolvingPower) - 1, 2):
            # print("i",ResolvingPower[i])
            if SecondaryScreenWidth == ResolvingPower[i]:
                SecondaryScreen = (ResolvingPower[i], ResolvingPower[i + 1])
                # print("副屏(横屏)尺寸:", ResolvingPower[i], ResolvingPower[i + 1])
                # return "副屏(竖屏)尺寸:",SecondaryScreen
                break
        # 竖屏状态
        for i in range(1, len(ResolvingPower) - 1, 2):
            # print("i",ResolvingPower[i])
            if SecondaryScreenWidth == ResolvingPower[i]:
                SecondaryScreen = (ResolvingPower[i], ResolvingPower[i + 1])
                # print("副屏(竖屏)尺寸:", ResolvingPower[i], ResolvingPower[i - 1])
                # return "副屏(竖屏)尺寸",SecondaryScreen
                break
    return MonitorNumber,AllScreen,MianScreen,SecondaryScreen

#调用
a=Display_Detection()
print(a)#a可以任意遍历其中的内容a[0]代表屏幕数量等等...

#(2, (4480, 1440), (2560, 1440), (1920, 1080))#运行结果:屏幕数量、总屏幕尺寸、主屏幕尺寸、副屏尺寸

这就是全部的内容了,如果有错误欢迎慷慨指正!

  • 9
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值