简体   繁体   English

Python win32gui应用程序窗口闪烁

[英]Python win32gui app window flickers

I have been having a problem with win32gui while taking screenshot of certain application window. 拍摄某些应用程序窗口的屏幕快照时,win32gui出现了问题。

My script (code below) take a screenshot of a window to analyze it. 我的脚本(下面的代码)对窗口进行了截图以对其进行分析。 For certain windows it works perfectly, but I have just encountered one application that, while I have my script running the window that it takes screenshot from make the window flickers. 对于某些窗口,它可以完美运行,但是我刚遇到一个应用程序,而我的脚本正在运行该窗口,该脚本需要从屏幕截图中捕获屏幕闪烁。 (I keep seeing white flash on the whole window) (我一直在整个窗口上看到白色闪烁)

Anyone ever experienced it and had a solution for it? 任何人都经历过并且有解决方案吗?

def getImage(self,hwnd = None):
    if hwnd == None:
        hwnd = self.hwnd
    self.whereIsWindow(hwnd)

    left, top, right, bot = win32gui.GetWindowRect(hwnd)

    w = right - left
    h = bot - top
    self.width = w
    self.height = h
    hwndDC = win32gui.GetWindowDC(hwnd)
    mfcDC = win32ui.CreateDCFromHandle(hwndDC)
    saveDC = mfcDC.CreateCompatibleDC()
    saveBitMap = win32ui.CreateBitmap()
    saveBitMap.CreateCompatibleBitmap(mfcDC, w, h)
    saveDC.SelectObject(saveBitMap)

    result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0)
    bmpinfo = saveBitMap.GetInfo()
    bmpstr = saveBitMap.GetBitmapBits(True)

    im = Image.frombuffer(
        'RGB',
        (bmpinfo['bmWidth'], bmpinfo['bmHeight']),
        bmpstr, 'raw', 'BGRX', 0, 1)

    win32gui.DeleteObject(saveBitMap.GetHandle())
    saveDC.DeleteDC()
    mfcDC.DeleteDC()
    win32gui.ReleaseDC(hwnd, hwndDC)

I know the culprit are the last 5 lines, if i comment them out it stops flickering but it takes all the memory which is not an option. 我知道罪魁祸首是最后5行,如果我将它们注释掉,它会停止闪烁,但是会占用所有内存,这不是一个选择。

我最终在循环中添加了更多的延迟,并且根本没有闪烁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM