[問題] 透過winapi繪圖

看板Python作者 (悠閒)時間7年前 (2017/01/19 15:18), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/1
目的是:監控最上層視窗,然後畫一個方形在視窗內 問題:有時候方形有畫出來有時候又沒有,不知道這該如何處理? 參考結果圖片: 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://www.ptt.cc/bbs/Python/M.1484810281.A.3AB.html
文章代碼(AID): #1OW6WfEh (Python)