Win32api没有在python中给出GetCursorPos()的正确坐标

时间:2015-09-12 17:05:53

标签: python python-2.7 winapi pywin32

当使用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中也遇到了这个错误。这个问题是否有任何解决方案或解决方法?

2 个答案:

答案 0 :(得分:5)

您受DPI virtualization的约束。您的应用程序尚未声明自己意识到高DPI,并且您的字体缩放比例为125%。

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

答案 1 :(得分:0)

您可以这样设置意识等级:

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