Skip to content

Latest commit

 

History

History
81 lines (63 loc) · 5.06 KB

README.md

File metadata and controls

81 lines (63 loc) · 5.06 KB

Win32CaptureSample

A simple sample using the Windows.Graphics.Capture APIs in a Win32 application.

Table of Contents

Requirements

This sample requires the Windows 11 SDK (10.0.22000.194) and Visual Studio 2022 to compile. Neither are required to run the sample once you have a binary. The minimum verison of Windows 10 required to run the sample is build 17134.

Win32 vs UWP

This sample demonstrates using the Windows.Graphics.Capture APIs in a Win32 application. For the most part, the usage is the same, except for a few tweaks:

HWND or HMONITOR based capture

Win32 applications have access to the IGraphicsCaptureItemInterop interface. This interface can be found by QIing for it on the GraphicsCaptureItem factory:

#include <winrt/Windows.Graphics.Capture.h>
#include <windows.graphics.capture.interop.h>
#include <windows.graphics.capture.h>

namespace winrt
{
    using namespace Windows::Graphics::Capture;
}

// Obtaining the factory
auto interopFactory = winrt::get_activation_factory<
    winrt::GraphicsCaptureItem, 
    IGraphicsCaptureItemInterop>();

winrt::GraphicsCaptureItem item{ nullptr };

// Creating a GraphicsCaptureItem from a HWND
winrt::check_hresult(interopFactory->CreateForWindow(
    hwnd, 
    winrt::guid_of<winrt::GraphicsCaptureItem>(), 
    winrt::put_abi(item)));

// Creating a GraphicsCaptureItem from a HMONITOR
winrt::check_hresult(interopFactory->CreateForMonitor(
    hmon, 
    winrt::guid_of<winrt::GraphicsCaptureItem>(), 
    winrt::put_abi(item)));

This sample makes uses a collection of common header files which contains helpers for GraphicsCaptureItem creation.

Using the GraphicsCapturePicker

Win32 applications may also use the GraphicsCapturePicker to obtain a GraphicsCaptureItem, which asks the user to do the selection. Like other pickers, the GraphicsCapturePicker won't be able to infer your window in a Win32 application. You'll need to QI for the IInitializeWithWindow interface and provide your window's HWND.

#include <winrt/Windows.Graphics.Capture.h>
#include <shobjidl_core.h>

namespace winrt
{
    using namespace Windows::Graphics::Capture;
}

auto picker = winrt::GraphicsCapturePicker();
auto initializeWithWindow = picker.as<IInitializeWithWindow>();
winrt::check_hresult(initializeWithWindow->Initialize(hwnd));
// The picker is now ready for use!

Create vs CreateFreeThreaded

When a Direct3D11CaptureFramePool is created using Create, you're required to have a DispatcherQueue for the current thread and for that thread to be pumping messages. When created with Create, the FrameArrived event will fire on the same thread that created the frame pool.

When created with CreateFreeThreaded, there is no DispatcherQueue requirement. However, the FrameArrived event will fire on the frame pool's internal thread. This means that the callback you provide must be agile (this is usually handled by the language projection).

Points of interest

The code is organized into a couple of classes:

  • App.h/cpp handles the basic logic of the sample, as well as setting up the visual tree to present the capture preview.
  • SampleWindow.h/cpp handles the main window and the controls.
  • SimpleCapture.h/cpp handles the basics of using the Windows.Graphics.Capture API given a GraphicsCaptureItem. It starts the capture and copies each frame to a swap chain that is shown on the main window.
  • CaptureSnapshot.h/cpp shows how to take a snapshot with the Windows.Graphics.Capture API. The current version uses coroutines, but you could synchronously wait as well using the same events. Just remember to create your frame pool with CreateFreeThreaded so you don't deadlock!