diff --git a/LunaTranslator/LunaTranslator/myutils/hwnd.py b/LunaTranslator/LunaTranslator/myutils/hwnd.py index 94aea60e..48d508ba 100644 --- a/LunaTranslator/LunaTranslator/myutils/hwnd.py +++ b/LunaTranslator/LunaTranslator/myutils/hwnd.py @@ -51,6 +51,8 @@ def grabwindow(app, callback=None): _() try: hwnd = gobject.baseobject.textsource.hwnd + if not hwnd: + raise except: hwnd = windows.GetForegroundWindow() @@ -77,7 +79,6 @@ def grabwindow(app, callback=None): _() - def getprocesslist(): pids = windows.EnumProcesses() @@ -233,5 +234,6 @@ def mouseselectwindow(callback): def screenshot(x1, y1, x2, y2, hwnd=None): bs = winsharedutils.gdi_screenshot(x1, y1, x2, y2, hwnd) pixmap = QPixmap() - pixmap.loadFromData(bs) + if bs: + pixmap.loadFromData(bs) return pixmap diff --git a/LunaTranslator/LunaTranslator/winsharedutils.py b/LunaTranslator/LunaTranslator/winsharedutils.py index 72b4b7e3..ade38d05 100644 --- a/LunaTranslator/LunaTranslator/winsharedutils.py +++ b/LunaTranslator/LunaTranslator/winsharedutils.py @@ -375,6 +375,8 @@ def gdi_screenshot(x1, y1, x2, y2, hwnd=None): rect.right = x2 rect.bottom = y2 bf = _gdi_screenshot(hwnd, rect, pointer(sz)) + if not (sz.value and bf): + return None data = cast(bf, POINTER(c_char))[: sz.value] c_free(bf) return data diff --git a/plugins/winsharedutils/screenshot.cpp b/plugins/winsharedutils/screenshot.cpp index e5555d4b..afae32b8 100644 --- a/plugins/winsharedutils/screenshot.cpp +++ b/plugins/winsharedutils/screenshot.cpp @@ -158,6 +158,9 @@ BYTE *SaveBitmapToBuffer(HBITMAP hBitmap, size_t *size) } DECLARE BYTE *gdi_screenshot(HWND hwnd, RECT rect, size_t *size) { + *size = 0; + if (rect.bottom == rect.top || rect.left == rect.right) + return nullptr; if (!hwnd) hwnd = GetDesktopWindow(); auto hdc = GetDC(hwnd);