GithubHelp home page GithubHelp logo

flowerlove / win32capturesample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from robmikh/win32capturesample

0.0 0.0 0.0 129 KB

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

License: MIT License

C++ 100.00%

win32capturesample's Introduction

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!

win32capturesample's People

Contributors

robmikh avatar chrisguzak avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.