How do I open a new Window in WinUI3 with WinRT/C++?

3.2k Views Asked by At

How do I open a new window in WinRT / WinUI3? I want to click a button and open up another floating window on top of the current / main window.

I have tried code from several samples with zero luck:

The majority of the C++ WinUI3 documentation still only has C# code samples in it and is largely useless to me for that reason. I just want to open a new window and nothing else.

2

There are 2 best solutions below

0
rileyd On

You must upgrade to Project Reunion 1.0.0 EXPERIMENTAL or higher to use <winrt/Microsoft.UI.Windowing.h>. You can then create an AppWindow as follows:

auto appwind = winrt::Microsoft::UI::Windowing::AppWindow::Create();
appwind.Title(L"New Window Title");
appwind.Show();

This does NOT work in project reunion 0.8.2 or 0.8.3 stable releases.

7
Simon Mourier On

You can use Windows App SDK Samples and for example modify this method: void DemoPage::TitleBtn_Click

like this:

void DemoPage::TitleBtn_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Microsoft::UI::Xaml::RoutedEventArgs const& e)
{
    auto window = Window();
    auto tb = TextBlock();
    tb.Text(L"Hello");
    window.Content(tb);
    window.Activate();
}

Which looks exactly like the C# sample here: Create a new Window

var window = new Window();
window.Content = new TextBlock() { Text = "Hello" };
window.Activate();