【问题标题】:Programmatically change windows resolution以编程方式更改 Windows 分辨率
【发布时间】:2018-08-23 13:49:55
【问题描述】:

我正在使用以下代码段通过 python 更改窗口的屏幕分辨率,它适用于 1366x768、1024x768、800x600 分辨率。但它不适用于 1440x810 分辨率。这里有什么问题?

import ctypes
import struct
import sys

def set_res(width, height, bpp=32):
    DM_BITSPERPEL = 0x00040000
    DM_PELSWIDTH = 0x00080000
    DM_PELSHEIGHT = 0x00100000
    CDS_UPDATEREGISTRY = 0x00000001
    SIZEOF_DEVMODE = 148

    user32 = ctypes.WinDLL('user32.dll')
    DevModeData = struct.calcsize("32BHH") * '\x00'
    DevModeData += struct.pack("H", SIZEOF_DEVMODE)
    DevModeData += struct.calcsize("H") * '\x00'
    dwFields = (width and DM_PELSWIDTH or 0) | (height and DM_PELSHEIGHT or 0) | (bpp and DM_BITSPERPEL or 0)
    DevModeData += struct.pack("L", dwFields)
    DevModeData += struct.calcsize("l9h32BHL") * '\x00'
    DevModeData += struct.pack("LLL", bpp or 0, width or 0, height or 0)
    DevModeData += struct.calcsize("8L") * '\x00'
    result = user32.ChangeDisplaySettingsA(DevModeData, CDS_UPDATEREGISTRY)
    return result == 0 # success if zero, some failure otherwise

if(__name__ == "__main__"):
    result = set_res(1440, 810)
    sys.exit(result)

【问题讨论】:

  • ChangeDisplaySettings 返回什么结果? GetLastError 中还有更多内容吗?但我猜这是因为这不是支持的分辨率:我从未听说过 810 高。
  • @Rup 它返回 -2
  • 那是DISP_CHANGE_BADMODE:不支持图形模式。
  • 找到要链接的副本,但页面非常大且速度慢:github.com/tpn/winsdk-10/blob/master/Include/10.0.10240.0/um/…

标签: python windows screen-resolution


【解决方案1】:

检查了我的系统屏幕分辨率选项是否可用。

它有以下可用的分辨率选项。

该脚本适用于上述所有选项。

请注意,1440 x 810 分辨率选项不可用,因此它在我的系统上也不起作用。

问题可能与该系统对屏幕分辨率的支持有关,而不是特定于上述代码。

【讨论】:

    【解决方案2】:

    您必须具有 Windows 支持的分辨率才能使其工作。

    对于那些想要这个 python 3.x 脚本的人来说,使用这个:

    import ctypes
    import struct
    import sys
    
    def set_res(width, height, bpp=32):
        DM_BITSPERPEL = 0x00040000
        DM_PELSWIDTH = 0x00080000
        DM_PELSHEIGHT = 0x00100000
        CDS_UPDATEREGISTRY = 0x00000001
        SIZEOF_DEVMODE = 148
    
        user32 = ctypes.WinDLL('user32.dll')
        DevModeData = struct.calcsize("32BHH") * bytes('\x00','utf')
        DevModeData += struct.pack("H", SIZEOF_DEVMODE)
        DevModeData += struct.calcsize("H") * bytes('\x00','utf')
        dwFields = (width and DM_PELSWIDTH or 0) | (height and DM_PELSHEIGHT or 0) | (bpp and DM_BITSPERPEL or 0)
        DevModeData += struct.pack("L", dwFields)
        DevModeData += struct.calcsize("l9h32BHL") * bytes('\x00','utf')
        DevModeData += struct.pack("LLL", bpp or 0, width or 0, height or 0)
        DevModeData += struct.calcsize("8L") * bytes('\x00','utf')
        result = user32.ChangeDisplaySettingsA(DevModeData, CDS_UPDATEREGISTRY)
        return result == 0  # success if zero, some failure otherwise
    
    if(__name__ == "__main__"):
        result = set_res(1280, 720)
        sys.exit(result)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多