作者Czero (悠闲)
看板Python
标题[问题] 透过winapi绘图
时间Thu Jan 19 15:17:57 2017
目的是:监控最上层视窗,然後画一个方形在视窗内
问题:有时候方形有画出来有时候又没有,不知道这该如何处理?
参考结果图片:
http://imgur.com/a/TQd5r
片段程式:
def callback(hWinEventHook, event, hwnd, idObject,
idChild, dwEventThread, dwmsEventTime):
print(GetWindowText(GetForegroundWindow()))
hdc = GetDC(hwnd)
left, top, right, bottom = GetClientRect(hwnd)
SetBkMode(hdc, win32con.OPAQUE)
Rectangle(hdc , left+3 , top+3 , right-3 , bottom-3)
ReleaseDC(hdc,hwnd)
WinEventProc = WinEventProcType(callback)
user32.SetWinEventHook.restype = ctypes.wintypes.HANDLE
hook = user32.SetWinEventHook(
EVENT_SYSTEM_FOREGROUND,
EVENT_SYSTEM_FOREGROUND,
0,
WinEventProc,
0,
0,
WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS
)
if hook == 0:
print('SetWinEventHook failed')
sys.exit(1)
msg = ctypes.wintypes.MSG()
while user32.GetMessageW(ctypes.byref(msg), 0, 0, 0) != 0:
user32.TranslateMessageW(msg)
user32.DispatchMessageW(msg)
user32.UnhookWinEvent(hook)
ole32.CoUninitialize()
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 118.168.86.21
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1484810281.A.3AB.html