天天看點

Python如何來識别兩個顯示器螢幕各自的分辨率在使用Python做桌面開發時如何識别兩個顯示器螢幕的分辨率

在使用Python做桌面開發時如何識别兩個顯示器螢幕的分辨率

很多朋友應該在做桌面開發時會遇到多屏下如何解決布局問題,下面實作的就是擷取準确的螢幕分辨率由于這是擷取的兩個螢幕的分辨率,是以三個螢幕的話可以在這基礎上進行邏輯思考

目前顯示器的主流分辨率:

分辨率 寬高
HD720p 1280*720
FHD1080p 1920*1080
QHD 2K 2560*1440
UHD 4K 3840*2160
QUHD 8K 7680*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))#運作結果:螢幕數量、總螢幕尺寸、主螢幕尺寸、副屏尺寸
           

這就是全部的内容了,如果有錯誤歡迎慷慨指正!