Skip to content

Commit

Permalink
general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
robmikh committed Jan 15, 2025
1 parent 0c07c6a commit 2a51573
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
47 changes: 33 additions & 14 deletions Win32CaptureSample/App.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ namespace util
using namespace robmikh::common::uwp;
}

App::App(winrt::ContainerVisual root, winrt::GraphicsCapturePicker capturePicker, winrt::FileSavePicker savePicker)
App::App(winrt::ContainerVisual root)
{
m_capturePicker = capturePicker;
m_savePicker = savePicker;
m_mainThread = winrt::DispatcherQueue::GetForCurrentThread();
WINRT_VERIFY(m_mainThread != nullptr);

Expand Down Expand Up @@ -72,7 +70,7 @@ winrt::GraphicsCaptureItem App::TryStartCaptureFromWindowHandle(HWND hwnd)
}
catch (winrt::hresult_error const& error)
{
MessageBoxW(nullptr,
MessageBoxW(m_mainWindow,
error.message().c_str(),
L"Win32CaptureSample",
MB_OK | MB_ICONERROR);
Expand All @@ -90,7 +88,7 @@ winrt::GraphicsCaptureItem App::TryStartCaptureFromMonitorHandle(HMONITOR hmon)
}
catch (winrt::hresult_error const& error)
{
MessageBoxW(nullptr,
MessageBoxW(m_mainWindow,
error.message().c_str(),
L"Win32CaptureSample",
MB_OK | MB_ICONERROR);
Expand All @@ -100,7 +98,9 @@ winrt::GraphicsCaptureItem App::TryStartCaptureFromMonitorHandle(HMONITOR hmon)

winrt::IAsyncOperation<winrt::GraphicsCaptureItem> App::StartCaptureWithPickerAsync()
{
auto item = co_await m_capturePicker.PickSingleItemAsync();
auto capturePicker = winrt::GraphicsCapturePicker();
InitializeObjectWithWindowHandle(capturePicker);
auto item = co_await capturePicker.PickSingleItemAsync();
if (item)
{
// We might resume on a different thread, so let's resume execution on the
Expand All @@ -126,14 +126,16 @@ winrt::IAsyncOperation<winrt::StorageFile> App::TakeSnapshotAsync()
auto item = m_capture->CaptureItem();

// Ask the user where they want to save the snapshot.
m_savePicker.SuggestedStartLocation(winrt::PickerLocationId::PicturesLibrary);
m_savePicker.SuggestedFileName(L"snapshot");
m_savePicker.DefaultFileExtension(L".png");
m_savePicker.FileTypeChoices().Clear();
m_savePicker.FileTypeChoices().Insert(L"PNG image", winrt::single_threaded_vector<winrt::hstring>({ L".png" }));
m_savePicker.FileTypeChoices().Insert(L"JPG image", winrt::single_threaded_vector<winrt::hstring>({ L".jpg" }));
m_savePicker.FileTypeChoices().Insert(L"JXR image", winrt::single_threaded_vector<winrt::hstring>({ L".jxr" }));
auto file = co_await m_savePicker.PickSaveFileAsync();
auto savePicker = winrt::FileSavePicker();
InitializeObjectWithWindowHandle(savePicker);
savePicker.SuggestedStartLocation(winrt::PickerLocationId::PicturesLibrary);
savePicker.SuggestedFileName(L"snapshot");
savePicker.DefaultFileExtension(L".png");
savePicker.FileTypeChoices().Clear();
savePicker.FileTypeChoices().Insert(L"PNG image", winrt::single_threaded_vector<winrt::hstring>({ L".png" }));
savePicker.FileTypeChoices().Insert(L"JPG image", winrt::single_threaded_vector<winrt::hstring>({ L".jpg" }));
savePicker.FileTypeChoices().Insert(L"JXR image", winrt::single_threaded_vector<winrt::hstring>({ L".jxr" }));
auto file = co_await savePicker.PickSaveFileAsync();
if (file == nullptr)
{
co_return nullptr;
Expand Down Expand Up @@ -211,6 +213,18 @@ void App::StartCaptureFromItem(winrt::GraphicsCaptureItem item)
m_capture->StartCapture();
}

void App::InitializeObjectWithWindowHandle(winrt::Windows::Foundation::IUnknown const& object)
{
if (m_mainWindow == nullptr)
{
throw winrt::hresult_error(E_FAIL, L"App hasn't been properly initialized!");
}

// Provide the window handle to the pickers (explicit HWND initialization)
auto initializer = object.as<util::IInitializeWithWindow>();
winrt::check_hresult(initializer->Initialize(m_mainWindow));
}

void App::StopCapture()
{
if (m_capture)
Expand All @@ -221,6 +235,11 @@ void App::StopCapture()
}
}

void App::InitializeWithWindow(HWND window)
{
m_mainWindow = window;
}

bool App::IsCursorEnabled()
{
if (m_capture != nullptr)
Expand Down
9 changes: 4 additions & 5 deletions Win32CaptureSample/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
class App
{
public:
App(winrt::Windows::UI::Composition::ContainerVisual root,
winrt::Windows::Graphics::Capture::GraphicsCapturePicker capturePicker,
winrt::Windows::Storage::Pickers::FileSavePicker savePicker);
App(winrt::Windows::UI::Composition::ContainerVisual root);
~App() {}

winrt::Windows::Graphics::Capture::GraphicsCaptureItem TryStartCaptureFromWindowHandle(HWND hwnd);
Expand All @@ -33,9 +31,11 @@ class App
void MinUpdateInterval(winrt::Windows::Foundation::TimeSpan value);

void StopCapture();
void InitializeWithWindow(HWND window);

private:
void StartCaptureFromItem(winrt::Windows::Graphics::Capture::GraphicsCaptureItem item);
void InitializeObjectWithWindowHandle(winrt::Windows::Foundation::IUnknown const& object);

private:
winrt::Windows::System::DispatcherQueue m_mainThread{ nullptr };
Expand All @@ -44,8 +44,7 @@ class App
winrt::Windows::UI::Composition::SpriteVisual m_content{ nullptr };
winrt::Windows::UI::Composition::CompositionSurfaceBrush m_brush{ nullptr };

winrt::Windows::Graphics::Capture::GraphicsCapturePicker m_capturePicker{ nullptr };
winrt::Windows::Storage::Pickers::FileSavePicker m_savePicker{ nullptr };
HWND m_mainWindow = nullptr;

winrt::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice m_device{ nullptr };
std::unique_ptr<SimpleCapture> m_capture{ nullptr };
Expand Down
9 changes: 9 additions & 0 deletions Win32CaptureSample/SampleWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ SampleWindow::SampleWindow(int width, int height, std::shared_ptr<App> app)
auto isAllDisplaysPresent = winrt::ApiInformation::IsApiContractPresent(WindowsUniversalContract, 9);

m_app = app;
m_app->InitializeWithWindow(m_window);
m_windows = std::make_unique<WindowList>();
m_monitors = std::make_unique<MonitorList>(isAllDisplaysPresent);
m_pixelFormats =
Expand Down Expand Up @@ -114,6 +115,10 @@ LRESULT SampleWindow::MessageHandler(UINT const message, WPARAM const wparam, LP
{
OnCaptureStarted(item, CaptureType::ProgrammaticWindow);
}
else
{
StopCapture();
}
}
else if (hwnd == m_monitorComboBox)
{
Expand All @@ -123,6 +128,10 @@ LRESULT SampleWindow::MessageHandler(UINT const message, WPARAM const wparam, LP
{
OnCaptureStarted(item, CaptureType::ProgrammaticMonitor);
}
else
{
StopCapture();
}
}
else if (hwnd == m_pixelFormatComboBox)
{
Expand Down
11 changes: 1 addition & 10 deletions Win32CaptureSample/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace util

int __stdcall WinMain(HINSTANCE, HINSTANCE, PSTR, int)
{
// SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); // works but everything draws small
// Initialize COM
winrt::init_apartment(winrt::apartment_type::single_threaded);

Expand All @@ -41,19 +40,11 @@ int __stdcall WinMain(HINSTANCE, HINSTANCE, PSTR, int)
root.Size({ -220.0f, 0.0f });
root.Offset({ 220.0f, 0.0f, 0.0f });

// Create the pickers
auto capturePicker = winrt::GraphicsCapturePicker();
auto savePicker = winrt::FileSavePicker();

// Create the app
auto app = std::make_shared<App>(root, capturePicker, savePicker);
auto app = std::make_shared<App>(root);

auto window = SampleWindow(880, 635, app);

// Provide the window handle to the pickers (explicit HWND initialization)
window.InitializeObjectWithWindowHandle(capturePicker);
window.InitializeObjectWithWindowHandle(savePicker);

// Hookup the visual tree to the window
auto target = window.CreateWindowTarget(compositor);
target.Root(root);
Expand Down

0 comments on commit 2a51573

Please sign in to comment.