5

I'm porting a programming language to Windows that has commands like "create a window" and "create a pushbutton in that window". The programming language itself is implemented in C++.

I hear the newest, recommended UI API on Windows going forward is WinUI 3, but I couldn't really find any good information on how to define a GUI in code instead of loading it from XAML files.

How does one create a WinUI 3 GUI in code?

uliwitness
  • 8,532
  • 36
  • 58
  • 1
    You instantiate a UI control by instantiating its corresponding Windows Runtime type, e.g. the [`Button`](https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.controls.button) type. – IInspectable Jan 02 '22 at 09:24
  • Thanks, that's a start, but without a window and app to put them in, I can't make much use of that info. Does MS not have sample code for a minimal app or so? The XAML-generated template code seems like a LOT of code just for an app object and empty window, with all those `winrt_get_activation_factory` etc. functions, `AppT` subclass etc. – uliwitness Jan 02 '22 at 22:16
  • There's a [XamlCode](https://github.com/kennykerr/cppwinrt/tree/master/Store/XamlCode) sample in Kenny Kerr's GitHub, in case that's useful. It's not clear to me whether you are creating a compiled language, i.e. you need to generate (C++) code, or whether you are running an interpreter that creates the UI. – IInspectable Jan 03 '22 at 21:51
  • It's an interpreter, so I don't know beforehand how many elements of each type I need and what their individual properties will be set to, and I won't have a C++ compiler to compile generated code (beyond what code can be generated as part of the interpreter). – uliwitness Jan 04 '22 at 16:54
  • Is there a trick to actually making this sample build on current versions of Visual Studio? It uses an older SDK and when I used the menu item to raise the SDK, it fails with errors about the PCH :( – uliwitness Jan 07 '22 at 15:19

1 Answers1

2

This example is in C#, but it should also work in C++.

Perform the following steps:

  • Create a WinUI project
  • Optional step: Remove the "MainWindow.xaml" file (and along its code behind file MainWindow.xaml.cs)
  • Go to the App.xaml.cs file and change the code of the OnLaunched method. See the example below

The example code creates an instance of type Window with a StackPanel as content. The StackPanel contains a TextBlock and a Button. If you click on the Button, the event handling code will write something using Debug.WriteLine.

    /// <summary>
    /// Invoked when the application is launched normally by the end user.  Other entry points
    /// will be used such as when the application is launched to open a specific file.
    /// </summary>
    /// <param name="args">Details about the launch request and process.</param>
    protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
    {
        // The original version of the method just contained these two lines:
        //m_window = new MainWindow();
        //m_window.Activate();

        m_window = new Window();

        StackPanel stackPanel = new StackPanel();

        TextBlock textBlock = new TextBlock();
        textBlock.Text = "Text of the TextBlock";

        Button button = new Button();
        button.Content = "Click Me";
        button.Click += (object sender, RoutedEventArgs e) => { Debug.WriteLine("Button clicked"); };

        stackPanel.Children.Add(textBlock);
        stackPanel.Children.Add(button);

        m_window.Content = stackPanel;
        m_window.Activate();
    }
Martin
  • 5,165
  • 1
  • 37
  • 50