在使用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))#运行结果:屏幕数量、总屏幕尺寸、主屏幕尺寸、副屏尺寸
这就是全部的内容了,如果有错误欢迎慷慨指正!
版权声明:本文为weixin_43945855原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。