gpt4 book ai didi

c++ - 如何使用 DirectX 捕获分层窗口?

转载 作者:行者123 更新时间:2023-11-28 07:00:29 29 4
gpt4 key购买 nike

我正在使用 DirectX 开发屏幕捕获应用程序,该应用程序适用于大多数情况,除非屏幕具有分层窗口,DirectX 捕获会忽略这种情况。

我知道使用 GDI 我可以使用 CAPTUREBLT 标志来捕获这些窗口,但我不知道如何使用 DirectX 来实现,所以我的问题是:

如何使用 DirectX 捕获分层窗口?

我的代码:

LPDIRECT3D9 d3d9;
LPDIRECT3DDEVICE9 d3ddev;
IDirect3DSurface9* dest;

void Init()
{
CoInitialize(NULL);

d3d9 = Direct3DCreate9(D3D_SDK_VERSION);

ww = GetSystemMetrics(SM_CXSCREEN);
wh = GetSystemMetrics(SM_CYSCREEN);

HWND hwnd = GetDesktopWindow();
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferWidth = ww;
d3dpp.BackBufferHeight = wh;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);

d3ddev->CreateOffscreenPlainSurface(ww, wh, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &dest, NULL);
}


void Screenshot()
{
d3ddev->GetFrontBufferData(0, dest);

D3DLOCKED_RECT rect;
ZeroMemory(&rect, sizeof(D3DLOCKED_RECT));
dest->LockRect(&rect, NULL, D3DLOCK_READONLY);
uchar *bits = (uchar*)rect.pBits;
dest->UnlockRect();

//Use bits
}

void End()
{
dest->Release();
d3ddev->Release();
d3d9->Release();

CoUninitialize();
}

最佳答案

我使用以下方法在 Direct-X 中捕获屏幕:

HRESULT dxReadPixels(IDirect3DDevice9* Device, void* Buffer, HDC& DC, int& Width, int& Height, D3DFORMAT Format)
{
IDirect3DSurface9* RenderTarget = nullptr;
IDirect3DSurface9* DestTarget = nullptr;
HRESULT result = Device->GetRenderTarget(0, &RenderTarget);

if (result == S_OK)
{
if (Width == 0 || Height == 0 || Format == D3DFMT_UNKNOWN)
{
D3DSURFACE_DESC descriptor = {};
RenderTarget->GetDesc(&descriptor);
Width = descriptor.Width;
Height = descriptor.Height;
Format = descriptor.Format;
}

if (Buffer)
{
RenderTarget->GetDC(&DC);
result = Device->CreateOffscreenPlainSurface(Width, Height, Format, D3DPOOL_SYSTEMMEM, &DestTarget, nullptr);
result = Device->GetRenderTargetData(RenderTarget, DestTarget);

D3DLOCKED_RECT rect;
DestTarget->LockRect(&rect, 0, D3DLOCK_READONLY);
memcpy(Buffer, rect.pBits, Width * Height * 4);
DestTarget->UnlockRect();
}
}

SafeRelease(RenderTarget);
SafeRelease(DestTarget);
return result;
}

不确定有什么方法可以让它更快,但它运行得相当好..

关于c++ - 如何使用 DirectX 捕获分层窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22569589/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com