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 n
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) as err:
print 'ERROR: %s.' % err
return res