0

I am trying to use the UWP capture api Windows.Graphics.Capture to capture a window. For that I have to use the GraphicsCapturePicker to select a screen/window. But I get the error message:

Error { code: HRESULT(0x8000000E), message: "Could not create a new view because the main window has not yet been created" }

From this it is clear that in have to create the main window, but I don't know how to do that. The code example (C#) given by the windows docs (https://learn.microsoft.com/en-us/windows/uwp/audio-video-camera/screen-capture) just does it like this:

CoreWindow window = CoreApplication.MainView.CoreWindow;

await window.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
    await StartCaptureAsync();
});

However I cant find CoreApplication.MainView.CoreWindow anywhere in the windows-rs docs. The code that I currently have looks like this:

use windows;

fn window_dispatch() -> windows::core::Result<()> {
    let picker = windows::Graphics::Capture::GraphicsCapturePicker::new().unwrap();
    picker.PickSingleItemAsync().unwrap().get();
    let return_value: windows::core::Result<()> = Ok(());
    return return_value;
}

fn main() {
    let support = windows::Graphics::Capture::GraphicsCaptureSession::IsSupported().unwrap();
    println!("Support Capture: {}", support);

    let handler = windows::UI::Core::DispatchedHandler::new(window_dispatch);
    let prio =  windows::UI::Core::CoreDispatcherPriority::Normal;
    let window = windows::ApplicationModel::Core::CoreApplication::MainView().unwrap().CoreWindow().unwrap().Dispatch

Does anyone have information on how to create the main window or where this is documented in UWP?

MEisebitt
  • 122
  • 7
  • 1
    *"From this it is clear that in have to create the main window"* - No, you don't. The window is created for you. You'll just have to wait until it is created. Your [`IFrameworkView.SetWindow`](https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.core.iframeworkview.setwindow) implementation will see a properly initialized [`CoreWindow`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.core.corewindow) instance. See the [core app](https://github.com/microsoft/windows-rs/tree/master/crates/samples/core_app) sample for an example. – IInspectable Nov 05 '22 at 12:29
  • 1
    That is, if you're actually trying to use the API from a UWP application. If you are writing a classic Win32 application, things are a lot easier (even if harder to [discover](https://github.com/robmikh/Win32CaptureSample)). – IInspectable Nov 05 '22 at 15:35
  • @IInspectable Thanks for the reply. I have to say I am very new to UWP so this is all still very confusing to me. Can you specify how I wait for the window to be created, because currently I don't have an `IFrameworkView.SetWindow` implementation. Does your message then imply that I need one? I have checked the core app sample but as it has no comments it confuses me. Maybe it would be smarter to use the classic Win32 API, especially as UWP is more or less deprecated. – MEisebitt Nov 07 '22 at 13:04
  • *"UWP [...] is all still very confusing to me"* - The whole UWP/WinRT business is confusing *everyone*. MS have done a phenomenal job at obscuring what those technologies are, and how they relate. It's a hot mess, and if you're confused, then you're already ahead of the masses that aren't. *"Maybe it would be smarter to use the classic Win32 API"* - Unless you're targeting Windows 10 Mobile, XBox, or the HoloLens, then UWP is something you can mostly ignore. – IInspectable Nov 07 '22 at 13:44
  • If your ultimate goal is to simply have a program that can capture any given `HWND` I'm about to amend an [old answer](https://stackoverflow.com/a/30965831/1889329) of mine that illustrates how to do that. That'll be C++, though I'm sure I can translate that to Rust just fine. *"I have checked the core app sample but as it has no comments it confuses me."* - That can be [changed](https://github.com/microsoft/windows-rs/issues). It'll be a bit until I can post an issue for what I thought to be lacking as well. – IInspectable Nov 07 '22 at 13:47
  • @IInspectable It is good to hear I'm not alone with my problems. Yeah I just want to make a desktop aplication (and thought UWP was the way) so I'll go to Win32. Thank you very much for your help, I will create an issue regarding the comments. – MEisebitt Nov 07 '22 at 14:08

0 Answers0