はんぎょねこの憂鬱

耳から変な汁が出てきた

Pythonでウインドウハンドル取得してGetClientRectしてみる

WIN32から離れられない貴方へ

import sys
from PySide import QtGui
import ctypes


class RECT(ctypes.Structure):
    _fields_ = [('left', ctypes.c_long),
                ('top', ctypes.c_long),
                ('right', ctypes.c_long),
                ('bottom', ctypes.c_long)]


class TestWidget(QtGui.QWidget):
    def __init__(self):
        super().__init__()

    def resizeEvent(self, event):
        super().resizeEvent(event)
        
        # ここから!
        ctypes.pythonapi.PyCapsule_GetPointer.restype = ctypes.c_void_p
        ctypes.pythonapi.PyCapsule_GetPointer.argtypes = [ctypes.py_object, ctypes.c_char_p]
        hwnd = ctypes.pythonapi.PyCapsule_GetPointer(self.winId(), None)
        rect = RECT()
        ctypes.windll.user32.GetClientRect(hwnd, ctypes.byref(rect))
        print(rect.left, rect.top, rect.right, rect.bottom)

if __name__ == '__main__':
    a = QtGui.QApplication(sys.argv)
    w = TestWidget()
    w.show()
    sys.exit(a.exec_())

参考記事

stackoverflow.com