【问题标题】:Win32api is not giving the correct coordinates with GetCursorPos() in pythonWin32api 没有在 python 中使用 GetCursorPos() 给出正确的坐标
【发布时间】:2015-09-12 17:05:52
【问题描述】:

当使用 pywin 的 win32api 时,光标位置的值不正确。我的屏幕分辨率是 1920x1080,但是当我使用 GetCursorPos() 时,左上角有 (0,0),右下角有 (1535,863)。我使用的代码如下:

import win32api

def getCursor():
    print win32api.GetCursorPos()

我在 windows 10 上使用 python 2.7 尝试此操作,但我在 windows 8 上的 python 2.6 中也遇到此错误。是否有任何解决方案或解决方法?

【问题讨论】:

  • 我得到了正确的位置。 (0, 0) 左上角和 (1919, 1199) 右下角。 Windows 10,Python 2.7

标签: python python-2.7 winapi pywin32


【解决方案1】:

您必须遵守DPI virtualization。您的应用程序尚未声明自己知道高 DPI,并且您的字体缩放比例为 125%。

如果您想避免 DPI 虚拟化,请将高 DPI 感知选项添加到应用程序清单或调用 SetProcessDPIAwareSetProcessDPIAwareness

【讨论】:

  • import ctypes user32 = ctypes.windll.user32 user32.SetProcessDPIAware() 你是这个意思吗?
  • 请注意,SetProcessDPIAware 已弃用。
  • @Jonathan 这是您在 Vista 或 7 或 8 IIRC 上使用的。我认为 SetProcessDPIAwareness 是 8.1 中的新功能。
  • @KobeJohn 我添加了你提到的代码,现在我得到了正确的光标坐标。我尝试使用SetProcessDPIAwareness,但它给了我一个属性错误,说找不到该函数。谢谢!
【解决方案2】:

您可以像这样设置意识级别:

import ctypes
awareness = ctypes.c_int()
ctypes.windll.shcore.SetProcessDpiAwareness(2)

认知等级定义如下:

typedef enum _PROCESS_DPI_AWARENESS { 
    PROCESS_DPI_UNAWARE = 0,
    /*  DPI unaware. This app does not scale for DPI changes and is
        always assumed to have a scale factor of 100% (96 DPI). It
        will be automatically scaled by the system on any other DPI
        setting. */

    PROCESS_SYSTEM_DPI_AWARE = 1,
    /*  System DPI aware. This app does not scale for DPI changes.
        It will query for the DPI once and use that value for the
        lifetime of the app. If the DPI changes, the app will not
        adjust to the new DPI value. It will be automatically scaled
        up or down by the system when the DPI changes from the system
        value. */

    PROCESS_PER_MONITOR_DPI_AWARE = 2
    /*  Per monitor DPI aware. This app checks for the DPI when it is
        created and adjusts the scale factor whenever the DPI changes.
        These applications are not automatically scaled by the system. */
} PROCESS_DPI_AWARENESS;

检查这个答案:https://stackoverflow.com/a/44422362/8537759

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    • 2019-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多