This commit is contained in:
恍兮惚兮 2024-12-01 12:32:35 +08:00
parent 3d1952e229
commit e05c29e205
3 changed files with 41 additions and 92 deletions

View File

@ -1,6 +1,5 @@
 
void GetVirtualDesktopRect(RECT &rect) void GetVirtualDesktopRect(RECT &rect)
{ {
// 获取虚拟桌面的尺寸和位置 // 获取虚拟桌面的尺寸和位置
@ -31,72 +30,6 @@ HBITMAP GetBitmap(RECT &rect, HDC hDC)
return hBitmap; return hBitmap;
} }
int SaveBitmapToFile(HBITMAP hBitmap, LPCWSTR lpFileName)
{
WORD wBitCount; // 位图中每个像素所占字节数
// 定义调色板大小,位图中像素字节大小,位图文件大小,写入文件字节数
DWORD dwPaletteSize = 0, dwBmBitsSize, dwDIBSize, dwWritten;
BITMAP Bitmap; // 位图属性结构
BITMAPFILEHEADER bmfHdr; // 位图文件头结构
BITMAPINFOHEADER bi; // 位图信息头结构
HANDLE fh; // 定义文件,分配内存句柄,调色板句柄
LPSTR lpbk, lpmem;
wBitCount = 32;
// 设置位图信息头结构
GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&Bitmap);
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = Bitmap.bmWidth;
bi.biHeight = Bitmap.bmHeight; // 为负,正向的位图;为正,倒向的位图
bi.biPlanes = 1;
bi.biBitCount = wBitCount;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
dwBmBitsSize = ((Bitmap.bmWidth * wBitCount + 31) / 32) * 4 * Bitmap.bmHeight;
// 创建位图文件
fh = CreateFile(lpFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
if (fh == INVALID_HANDLE_VALUE)
return FALSE;
// 设置位图文件头
bmfHdr.bfType = 0x4D42; // "BM"
dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwBmBitsSize;
bmfHdr.bfSize = dwDIBSize;
bmfHdr.bfReserved1 = 0;
bmfHdr.bfReserved2 = 0;
bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
// 写入位图文件头
WriteFile(fh, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
// 写入位图信息头
WriteFile(fh, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwWritten, NULL);
// 获取位图阵列
lpmem = new char[dwBmBitsSize];
lpbk = (LPSTR) new char[dwBmBitsSize];
GetBitmapBits(hBitmap, dwBmBitsSize, lpmem); // 正向的内存图象数据
// 转化为倒向数据(仅在bmHeight为正时需要)
for (int i = 0; i < Bitmap.bmHeight; i++)
{
memcpy(lpbk + Bitmap.bmWidth * i * 4, lpmem + Bitmap.bmWidth * (Bitmap.bmHeight - i - 1) * 4, Bitmap.bmWidth * 4);
}
// 写位图数据
WriteFile(fh, lpbk, dwBmBitsSize, &dwWritten, NULL);
// 清除
delete[] lpbk;
delete[] lpmem;
CloseHandle(fh);
return TRUE;
}
std::vector<byte> SaveBitmapToBuffer(HBITMAP hBitmap) std::vector<byte> SaveBitmapToBuffer(HBITMAP hBitmap)
{ {
WORD wBitCount; // 位图中每个像素所占字节数 WORD wBitCount; // 位图中每个像素所占字节数
@ -167,8 +100,6 @@ DECLARE_API void gdi_screenshot(HWND hwnd, RECT rect, void (*cb)(byte *, size_t)
if (!hdc) if (!hdc)
return; return;
auto bm = GetBitmap(rect, hdc); auto bm = GetBitmap(rect, hdc);
// SaveBitmapToFile(bm, LR"(.\2.bmp)");
size_t size;
auto bf = std::move(SaveBitmapToBuffer(bm)); auto bf = std::move(SaveBitmapToBuffer(bm));
if (bf.size()) if (bf.size())
cb(bf.data(), bf.size()); cb(bf.data(), bf.size());

View File

@ -23,11 +23,12 @@ def clipboard_set_image(p: QImage):
@threader @threader
def grabwindow(app="PNG", callback_origin=None, tocliponly=False): def grabwindow(app="PNG", callback_origin=None, tocliponly=False):
tmsp = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())
if tocliponly: if tocliponly:
fname = "" fname = ""
uid = None uid = None
elif callback_origin or tocliponly: elif callback_origin or tocliponly:
fname = gobject.gettempdir(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())) fname = gobject.gettempdir(tmsp)
uid = None uid = None
else: else:
@ -36,29 +37,26 @@ def grabwindow(app="PNG", callback_origin=None, tocliponly=False):
hwndx = windows.GetForegroundWindow() hwndx = windows.GetForegroundWindow()
hwndx = windows.GetAncestor(hwndx) hwndx = windows.GetAncestor(hwndx)
gamepath = getpidexe(windows.GetWindowThreadProcessId(hwndx)) gamepath = getpidexe(windows.GetWindowThreadProcessId(hwndx))
exename = os.path.basename(gamepath).replace( exename = os.path.splitext(os.path.basename(gamepath))[0]
"." + os.path.basename(gamepath).split(".")[-1], ""
)
uid = gobject.baseobject.gameuid uid = gobject.baseobject.gameuid
screenshot_savepath: str = globalconfig.get("screenshot_savepath", "") screenshot_savepath: str = globalconfig.get("screenshot_savepath", "")
fnamef = lambda: gobject.getcachedir(
"screenshot/" try:
+ exename if not screenshot_savepath:
+ "/" raise
+ time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()), dirname = screenshot_savepath.format(exename=exename)
abspath=False, os.makedirs(dirname, exist_ok=True)
) fname = os.path.join(dirname, tmsp)
if screenshot_savepath: except:
try: fnamebase = "cache/screenshot"
dirname = screenshot_savepath.format(exename=exename) if not os.path.exists(fnamebase):
os.makedirs(dirname, exist_ok=True) fnamebase = windows.SHGetFolderPathW(windows.CSIDL_MYPICTURES)
fname = os.path.join( if not (fnamebase and os.path.exists(fnamebase)):
dirname, time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()) fnamebase = "cache/screenshot"
) else:
except: fnamebase = os.path.join(fnamebase, "LunaTranslator")
fname = fnamef() fname = os.path.join(fnamebase, exename, tmsp)
else: os.makedirs(os.path.join(fnamebase, exename), exist_ok=True)
fname = fnamef()
def callback_1(callback_origin, uid, tocliponly, p: QPixmap, fn): def callback_1(callback_origin, uid, tocliponly, p: QPixmap, fn):
if p.isNull(): if p.isNull():
@ -80,7 +78,6 @@ def grabwindow(app="PNG", callback_origin=None, tocliponly=False):
hwnd = windows.GetAncestor(hwnd) hwnd = windows.GetAncestor(hwnd)
_ = windows.GetClientRect(hwnd) _ = windows.GetClientRect(hwnd)
p = gdi_screenshot(0, 0, _[2], _[3], hwnd) p = gdi_screenshot(0, 0, _[2], _[3], hwnd)
callback(p, fname + "_gdi." + app) callback(p, fname + "_gdi." + app)
isshit = (not callback_origin) and (not tocliponly) isshit = (not callback_origin) and (not tocliponly)
if p.isNull() or isshit: if p.isNull() or isshit:

View File

@ -53,6 +53,7 @@ from ctypes.wintypes import (
USHORT, USHORT,
) )
HRESULT = LONG
HWINEVENTHOOK = HANDLE HWINEVENTHOOK = HANDLE
LRESULT = LPLONG LRESULT = LPLONG
WAIT_TIMEOUT = 258 WAIT_TIMEOUT = 258
@ -962,3 +963,23 @@ SetWinEventHook.argtypes = DWORD, DWORD, HMODULE, WINEVENTPROC, DWORD, DWORD, DW
PathFileExists = windll.Shlwapi.PathFileExistsW PathFileExists = windll.Shlwapi.PathFileExistsW
PathFileExists.argtypes = (LPCWSTR,) PathFileExists.argtypes = (LPCWSTR,)
PathFileExists.restype = BOOL PathFileExists.restype = BOOL
_SHGetFolderPathW = _shell32.SHGetFolderPathW
_SHGetFolderPathW.argtypes = (
HWND,
c_int,
HANDLE,
DWORD,
LPWSTR,
)
_SHGetFolderPathW.restype = HRESULT
CSIDL_MYPICTURES = 0x27
SHGFP_TYPE_CURRENT = 0
S_OK = 0
def SHGetFolderPathW(csidl):
buff = create_unicode_buffer(MAX_PATH + 100)
if _SHGetFolderPathW(None, csidl, None, SHGFP_TYPE_CURRENT, buff) != S_OK:
return None
return buff.value