尝试在Python中检索活动的监视器ID

时间:2015-11-13 03:39:06

标签: python ctypes maya

我试图通过Maya在Python 2.7中检索监视器ID。我正在玩ctypes,因为我在Python中找不到合适的支持。我们的想法是检索用户当前监视器的ID(在多监视器环境中),并解析我创建的EDID转储以检索该监视器的对角线规格(以英寸为单位)。我试图避免使用插件,因为这将由艺术团队使用,但显然如果没有办法解决它,那就是必须要做的事情。

* note - 我也可以通过以下方法确定鼠标位置。可能是一种计算我正在寻找的东西的方法吗?

from ctypes import windll, Structure, c_ulong, byref


class POINT(Structure):
    _fields_ = [("x", c_ulong), ("y", c_ulong)]



def queryMousePosition():
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))
    return { "x": pt.x, "y": pt.y}


pos = queryMousePosition()
print(pos)

先谢谢

干杯, Capt.Solo

2 个答案:

答案 0 :(得分:2)

以下是您问题的一些可能答案,我尝试仅使用“仅限Python”代码而不依赖于任何第三方库。

我找不到使用“仅限Python”代码获取显示器物理尺寸的方法。

我将调用我的主屏幕Monitor 1,另一个调用Monitor 2。

答:获取哪个监视器具有当前活动窗口。

您可以从Maya的脚本编辑器执行此操作,根据您的脚本编辑器所在的监视器,您将获得不同的结果。

文件:

GetForegroundWindow

MonitorFromWindow

备注:

  

如果窗口与一个或多个显示器矩形相交,则   返回值是具有的显示监视器的HMONITOR句柄   与窗户交汇的最大区域。   如果窗口不与显示监视器相交,则返回值取决于dwFlags的值。

<强>代码:

from ctypes import windll

#Get active window id
# https://msdn.microsoft.com/en-us/library/ms633505
winID = windll.user32.GetForegroundWindow()
print "This is your current window ID: ",winID
# MonitorFromWindow constants 
# https://msdn.microsoft.com/en-us/library/dd145064
MONITOR_DEFAULTTONULL    = 0
MONITOR_DEFAULTTOPRIMARY = 1
MONITOR_DEFAULTTONEAREST = 2
monitorID = windll.user32.MonitorFromWindow(winID, MONITOR_DEFAULTTONEAREST)
print "This is your active monitor ID: ", monitorID

B:获取哪个监视器具有Maya的主窗口

您也可以从Maya的脚本编辑器执行此操作,结果取决于Maya的主窗口所在的监视器。 PySide现在包含在最新版本的Maya中。

<强>文件:

QWidget.winId()

代码:

import shiboken
from PySide import QtGui, QtCore
import maya.OpenMayaUI as apiUI

import ctypes

def getMayaWindow():
    """
    Get the main Maya window as a QtGui.QMainWindow instance
    @return: QtGui.QMainWindow instance of the top level Maya windows
    """
    ptr = apiUI.MQtUtil.mainWindow()
    if ptr is not None:
        return shiboken.wrapInstance(long(ptr), QtGui.QWidget)

PyCObject_AsVoidPtr = ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.py_object)(
                          ('PyCObject_AsVoidPtr', ctypes.pythonapi))

mayaID = PyCObject_AsVoidPtr(getMayaWindow().winId())
print "This is Maya's main window ID: ", mayaID

# MonitorFromWindow constants 
# https://msdn.microsoft.com/en-us/library/dd145064
MONITOR_DEFAULTTONULL    = 0
MONITOR_DEFAULTTOPRIMARY = 1
MONITOR_DEFAULTTONEAREST = 2
monitorID = windll.user32.MonitorFromWindow(mayaID, MONITOR_DEFAULTTONEAREST)
print "This is Maya's monitor ID: ", monitorID

C:获取哪个显示器有鼠标指针

文件:

MonitorFromPoint

注意:

  

如果显示监视器包含该点,则返回值是该显示监视器的HMONITOR句柄。

     

如果显示监视器未包含该点,则返回值取决于dwFlags的值。

<强>代码:

from ctypes import windll, Structure, c_ulong, byref

class POINT(Structure):
    _fields_ = [("x", c_ulong), ("y", c_ulong)]

def queryMousePosition():
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))
    return pt

mousePos = queryMousePosition()
print "This is your mouse position x: ", mousePos.x, " y:", mousePos.y

# MonitorFromPoint  constants 
# https://msdn.microsoft.com/en-us/library/dd145062(v=vs.85).aspx
MONITOR_DEFAULTTONULL    = 0
MONITOR_DEFAULTTOPRIMARY = 1
MONITOR_DEFAULTTONEAREST = 2
monitorID = windll.user32.MonitorFromPoint(pos, MONITOR_DEFAULTTONEAREST)
print "This is your active monitor ID: ", monitorID

答案 1 :(得分:0)

如果您可以访问PySide或PyQt(您应该从maya中访问),您可以使用这个简单的代码段来获取每个屏幕的ID和位置:

dw = QtGui.QDesktopWidget()
screen_count = dw.numScreens()
for i in range(screen_count):
    rect_screen_i = dw.availableGeometry(i)
    print rect_screen_i