WinRT新dx截图最小实现如何为?

摘要:转自:https:stackoverflow.coquestions11283015 效果还是很不错的 #include <iostream> #include <W
转自:https://stackoverflow.co/questions/11283015 效果还是很不错的 #include <iostream> #include <Windows.h> #include <dxgi.h> #include <inspectable.h> #include <dxgi1_2.h> #include <d3d11.h> #include <winrt/Windows.Foundation.h> #include <winrt/Windows.System.h> #include <winrt/Windows.Graphics.Capture.h> #include <windows.graphics.capture.interop.h> #include <windows.graphics.directx.direct3d11.interop.h> #include <roerrorapi.h> #include <shlobj_core.h> #include <dwmapi.h> #pragma comment(lib,"Dwmapi.lib") #pragma comment(lib,"windowsapp.lib") void CaptureWindow(HWND hwndTarget) { // Init COM winrt::init_apartment(winrt::apartment_type::multi_threaded); // Create Direct 3D Device winrt::com_ptr<ID3D11Device> d3dDevice; winrt::check_hresult(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, D3D11_CREATE_DEVICE_BGRA_SUPPORT, nullptr, 0,D3D11_SDK_VERSION, d3dDevice.put(), nullptr, nullptr)); winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice device; const auto dxgiDevice = d3dDevice.as<IDXGIDevice>(); { winrt::com_ptr<::IInspectable> inspectable; winrt::check_hresult(CreateDirect3D11DeviceFromDXGIDevice(dxgiDevice.get(), inspectable.put())); device = inspectable.as<winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice>(); } auto idxgiDevice2 = dxgiDevice.as<IDXGIDevice2>(); winrt::com_ptr<IDXGIAdapter> adapter; winrt::check_hresult(idxgiDevice2->GetParent(winrt::guid_of<IDXGIAdapter>(), adapter.put_void())); winrt::com_ptr<IDXGIFactory2> factory; winrt::check_hresult(adapter->GetParent(winrt::guid_of<IDXGIFactory2>(), factory.put_void())); ID3D11DeviceContext* d3dContext = nullptr; d3dDevice->GetImmediateContext(&d3dContext); RECT rect{}; DwmGetWi
阅读全文