如何查询主显示器的NATIVE硬件分辨率在Windows? [英] How to query NATIVE hardware resolution of primary monitor in Windows?

查看:1672
本文介绍了如何查询主显示器的NATIVE硬件分辨率在Windows?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到最佳或本机在Windows下一个附加的液晶显示器的分辨率(我会再设置编程,并知道该怎么办。)让我再说一遍,我并不需要当前的Windows分辨率,也不需要担心的CRT显示器/投影机。

I need to find the "best" or native resolution for an attached LCD monitor under Windows (which I'll then set programmatically and know how to do.) Let me repeat that I do not need the Current Windows resolution, nor need to worry about CRTs/projectors.

我已经看到了这个程序的工作,所以我知道这是可能的,尽管反对者:
   http://www.entechtaiwan.com/util/moninfo.shtm

I've seen it work with this program, so I know it is possible despite the naysayers: http://www.entechtaiwan.com/util/moninfo.shtm

这将是最好直接跟显示器和查询EDID信息。不过,我已经看到了它在注册表中缓存,不会有从HKLM \\ SYSTEM挖掘出来\\ CURRENTCONTROLSET \\枚举\\显示一个问题,但无法弄清楚如何将数据与当前的主显示器相匹配。

It would be best to talk directly to the monitor and query the EDID info. However, I've seen that it is cached in the registry and wouldn't have a problem digging it out from HKLM\SYSTEM\CurrentControlSet\Enum\DISPLAY but can't figure out how to match the data with the current primary monitor.

我发现在这个C程序:
  <一href=\"http://www.tech-archive.net/Archive/Development/microsoft.public.development.device.drivers/2004-08/0294.html\" rel=\"nofollow\">http://www.tech-archive.net/Archive/Development/microsoft.public.development.device.drivers/2004-08/0294.html
一个类似的Python程序:
   http://www.koders.com/python/fid7FCCE3C908F376DC62F06CAD9B11C6D7C1CFA78F.aspx

I did find this C program at: http://www.tech-archive.net/Archive/Development/microsoft.public.development.device.drivers/2004-08/0294.html and a similar python program: http://www.koders.com/python/fid7FCCE3C908F376DC62F06CAD9B11C6D7C1CFA78F.aspx

不幸的是,我遇到了很多麻烦C程序转换为蟒蛇,为相关code似乎没有要在win32all模块。我想尝试编译它,但没有一个大的编译器的磁盘空间并没有多年使用温度。我有点出我的元素与ctypes的为好。

Unfortunately, I'm having a lot of trouble converting the C program to python, as the relevant code doesn't seem to be in the win32all modules. I'd try to compile it but don't have disk space for a big compiler and haven't used C for years. I'm a bit out of my element with ctypes as well.

我的B计划将使用EnumDisplaySettings()找到解决的最大的值,并将其设置为。在PC上的我试过它提供了正确的资源,但它仍然是有问题的。

My plan B will be to use EnumDisplaySettings() to find the largest value for resolution and set it to that. On the PC's I've tried it gives the correct res, but it could still be problematic.

我倒是preFER Python中的解决方案,但也许有人可以帮我修改C程序吐出分辨率和编译。先谢谢了。

I'd prefer a solution in python, but perhaps someone could help me modify the C program to spit out the resolution and compile it. Thanks in advance.

更新:

我已经找到了一个潜在的解决方案。我现在读WMI找到一个显示器可用(未脱机),抓住它的PNP设备ID,并与id值的子项中的注册表读取EDID。然后我分析的数据字节38和39,计算。不是很干净,但我得到结果。如果这是一种合理的方式做到这一点,我会关闭这个问题,谢谢。

I've found a potential solution. I am now reading WMI to find a monitor that is available (not offline), grabbing its PNP device id, and reading EDID from the registry in the subkey with the id value. Then I parse the data for bytes 38 and 39 and calculate. Not very clean, but am getting results. If this is a reasonable way to do it I'll close this question, thanks.

推荐答案

决定放弃直接对话的监测和分析,而不是在注册表中缓存的EDID信息。这code是不完美的,但它的作品:

Decided to give up talking directly to the monitor and instead parsing EDID information cached in the registry. This code isn't perfect, but it works:

import win32api as api, win32con as con, pywintypes
import win32com.client
_objWMIService = win32com.client.Dispatch('WbemScripting.SWbemLocator')
_objSWbemServices = _objWMIService.ConnectServer('.', 'root\\cimv2')
wmiquery = _objSWbemServices.ExecQuery

# get_regval(regkey) is simple registry reading function.
def get_monitor_res():
    dtd = 54  # start byte of detailed timing desc.

    try:  # get PNP id to find EDID in registry
        for monitor in wmiquery('Select * from Win32_DesktopMonitor'):
            # http://msdn.microsoft.com/en-us/library/aa394122%28VS.85%29.aspx
            if monitor.Availability in (3, 7, 13, 14, 15, 16): # connected
                curres = (monitor.ScreenWidth, monitor.ScreenHeight)
                print 'DEBUG: Current monitor resolution from WMI: %s' % (curres,)
                regkey = ('HKLM\\SYSTEM\\CurrentControlSet\\Enum\\' +
                    monitor.PNPDeviceID + '\\Device Parameters\\EDID')
                edid = get_regval(regkey)
                if edid:
                    print 'DEBUG: EDID Version: %s.%s' % (edid[18], edid[19])
                    # upper nibble of byte x 2^8 combined with full byte
                    hres = ((edid[dtd+4] >> 4) << 8) | edid[dtd+2]
                    vres = ((edid[dtd+7] >> 4) << 8) | edid[dtd+5]
                    print 'DEBUG: EDID DTD0: ' + str((hres, vres))
                    res = (hres, vres)
                    break  # give up on first success
                else:
                    raise RuntimeError, 'EDID not found in registry'
    except (RuntimeError, Exception), e:
        print 'ERROR: %s.' % e

    return res

这篇关于如何查询主显示器的NATIVE硬件分辨率在Windows?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆