从另一个桌面捕获屏幕截图

Capture Screenshot from another desktop

本文关键字:屏幕截图 桌面 另一个      更新时间:2023-10-16

我已经使用 CreateDesktop创建了第二个桌面,而我不切换到它。另外,我在其中创建了一些流程,例如explorer.exe和winrar.exe。接下来,我有一个代码,该代码将当前桌面的屏幕截图带到剪贴板。CreateSktop和屏幕截图都有效,但是该新桌面或窗口的屏幕截图返回黑色位图

这是返回当前桌面的窗口中窗口的屏幕截图:

// hwnd is handle to winrar or ... created in a new desktop retrieved by EnumDesktopWindow
RECT rc;
GetClientRect(hwnd, &rc);
const HDC hScreenDC = GetDC(nullptr);
const HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
const int width = GetDeviceCaps(hScreenDC, HORZRES);
const int height = GetDeviceCaps(hScreenDC, VERTRES);
const HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HBITMAP(SelectObject(hMemoryDC, hBitmap));
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
OpenClipboard(nullptr);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);

我已经在C#中实现了这两种方法,但是同一件事发生了。

有很好的资源,例如:

捕获隐藏桌面的屏幕截图

使用CreateSktop API

创建的桌面屏幕截图

c# - 使用Vista DWM(共享Direct3D Surface)的屏幕捕获

使用WM_PRINT消息捕获的窗口内容

如何从另一个桌面捕获屏幕?(CreateSktop)

这也像是一个死的话题,没有新的文章,解释或解决方案。

我读了其中的大多数,但没有运气,这是我想的最接近的尝试。语言对我也不重要:c#,c ,python或...

我找到了解决方案,这很有趣,但没有完美,只需解决我的需求。

CreateDesktop之后,通过调用OpenDesktop然后SetThreadDesktop然后使用屏幕截图代码,您可以在CreateSktop内部创建的窗口的屏幕截图,也不需要在其中创建Explorer.exe,如果您只需要窗口:

>
CreateDesktopW(L"NewDesktop"); // CreateDesktop code here. This is my function
const HDESK Handle = OpenDesktopW(L"NewDesktop", 0, 0, GENERIC_ALL);
SetThreadDesktop(Handle);
// Above ScreenShot code here ...

屏幕快照代码需要PrintWindow

RECT rc;
GetClientRect(hwnd, &rc);
const HDC hScreenDC = GetDC(nullptr);
const HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
const int width = GetDeviceCaps(hScreenDC, HORZRES);
const int height = GetDeviceCaps(hScreenDC, VERTRES);
const HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HBITMAP(SelectObject(hMemoryDC, hBitmap));
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
/// ADDED CODE
PrintWindow(hWnd, hMemoryDC, PW_CLIENTONLY);
///
OpenClipboard(nullptr);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);

我在不活动台式机内与winrar.exe窗口一起工作。您可以尝试一下,然后将其粘贴以查看结果。

只有一件事,屏幕快照位图的整个区域都是黑色的,除了我想要的窗口句柄,这对我来说很好。我想我应该按顺序从下到顶部的每个窗口,然后将它们混合。

对此的所有添加都表示赞赏。