Guides for using WinUI with C++ [closed]
Asked Answered
F

0

6

I'll try to summarize my issue as concisely as I can:

  • I have a legacy C++ project that uses WinAPI to create and manage UI elements. I want to replace this with a more modern framework that supports OOP and has some kind of visual designer. I intend to achieve this by gradually replacing the old WinAPI-based code with the new framework (e.g I could introduce a new popup window that uses the new framework, while the rest of the UI would still use the old window procedures and whatnot)
  • My initial plan was to use Qt, since it allows me to conveniently wrap UI code in objects, which I can easily instantiate and manage. It also has a Designer that lets me create custom UI elements (including automatic layout management), a Visual Studio extension, and in general, past experience has shown me that Qt would suit my purposes quite well. However, users here have warned me that it does not cooperate well with WinAPI, and I would basically have to rebuild the app from scratch.
  • It was suggested that I use WinUI instead, i.e XAML coupled with C++/WinRT, as these supposedly offer the same flexibility as what I described above. I managed to successfully replicate the results of the "host UWP control in a Win32 app" guide, but there seems to be little useful material beyond that.
  • The Microsoft documentation is rather difficult to navigate in order to get a clear answer. At the moment, the guide above is leading me to believe that I'd still be stuck with using WinAPI, as there doesn't seem to be an obvious way to wrap UI code in objects, preferably those that I can derive from existing base classes (e.g some kind of basic Window object)

I have the following environment to work with:

  • Latest C++ feature level supported by MSVC
  • Visual Studio 2019
  • Windows 10 (i.e I don't mind if I'm no longer compatible with Windows operating systems)

What I need:

  • An explicit answer on whether it is possible to work with XAML and C++/WinRT in a similar fashion to how one designs UI code in Qt with C++. Exact parity is not necessary, I just want to eliminate the hassle of managing UI using WinAPI, where I practically have to do everything "by hand"

  • If the above is true, then where can I find guides for the various steps of the workflow? How do I make a "window object", how can I design a window with layout management, how do I handle UI events, etc.?

  • Are the above equally possible if it needs to be done in the context of a legacy WinAPI application that I am trying to upgrade?

Fasciate answered 19/6, 2020 at 16:0 Comment(13)
There's also for example wxWidgets, which still is a USER32 GUI but now it's object-oriented and the repetitive parts are moved into the library. But since all the same Win32 API concepts still exist, it should be possible to port piecemeal instead of throwing away the existing stuff and doing a total replacement.Mady
@BenVoigt are you sure a mix/match paradigm is possible? I think a working message loop would be difficult, for example.Midden
@MarkRansom: Why? The usual GetMessage+TranslateMessage+DispatchMessage loop works brilliantly with any number of windows and any number of window classes whether those are builtin ("TEXT", "BUTTON", etc), common controls ("LISTVIEW" etc), third-party library, implemented in the main program using direct WinAPI calls, implemented in the main program with the help of wxWidgets object-oriented wrapper classes.... If your windowing library needs to inject code into the main message loop, that library is broken.Mady
C++/WinRT is your entry point to C++/WinRT. It has lots of information, including a simple C++/WinRT Windows UI Library example. While you linked to a walkthrough that creates XAML controls in code, Host a custom UWP control in a C++ Win32 app explains how to do it with XAML markup.Conventionality
@Conventionality I tried setting it all up, and while it sort of worked, it was way too much hassle. I decided to go with Ben Voigt's suggestion to use wxWidgets instead.Fasciate
wxWidgets is certainly a lot less invasive than Qt could ever be. Still, keep in mind that Project Reunion is Microsoft's vision of the future development platform for UIs. If that is successful, that's where you'll find Windows' UI platform with long-term support.Conventionality
You now have a new option - WinUI 3 WinUI 3 offers XAML and 'Modern App' UI from C++, except without the disruption of moving to the WinRT platform (it can work fine with plain old Win32). So it's ideal for a legacy C++ project on Windows. learn.microsoft.com/en-us/windows/apps/winui/winui3Nikaniki
Unfortunately it is not useable and wont be ready until end of 2021, production ready maybe mid 2022 (I'm just adding 6 month to the official roadmap dates out of personal experience with the MS team). Preview3 is still in a bad shape and lots of features including XAML Island and Ink Control which were main advertising topics at the beginning are pushed to unspecified "later" milestones in the updated roadmap (thats why i say mid/end of 2022). So sad because i want to use it too for my new app.Germen
You wont be happy going with WxWidgets in the long run. The development of WxWidgets is almost dead for the last 10 years. So don't expect them to move to WinUI3 when it becomes ready (or GTK4 which is showtime ready next April). I recommend to work on other parts of your app and wait with the GUI transition until Preview4 or first Beta of WinUI3 is available and then consider again.Germen
@Germen The project I'm using it on is very old, and I would take whichever library would do the job easiest. I would be happy to use WinUI3 if there was coherent documentation for the specific task, namely transitioning an old Win32 app without having to rebuild the entire thing.Fasciate
WinUI3 seems very exciting, and I'm still tempted to experiment with it (wxWidgets has been working decently so far, though it does have a few quirks), but I still can't find a coherent guide that explains, from start to finish, how one should take an existing Win32 app, update its configurations, and then start injecting WinUI3 controls into it. Instead it keeps directing me to XAML Islands, which AFAIK was written before WinUI3 was a thing.Fasciate
Yes there is none. The API is also not finalized. They for example still trying to figure out the thing with multiple windows (UWP apps must have one thread per window). All we can do is wait. Unfortunately the chief architect of the C++/WinRT Kenny Kerr said in his blogs that nobody at MS is willing to pay for technecial writers anymore as books are seen as outdated to fast (such a terrible assumption). So i fear we will never get a new Petzold Bible for WinUI3. So we have to put our trust in 3rd party bloggers.Germen
@Fasciate WinUI 3 does indeed look promising. This could become the goto UI toolkit for Windows. Beta 3 is currently out, which unfortunately means that not much sample code is out yet. I tried to figure out how to port the basics of a Win32 app to WinUI 3 but many things are non-obvious. This is why I asked a question on GitHub (github.com/microsoft/microsoft-ui-xaml/issues/3812) how this is best done. There aren't any answers yet but you could post a comment to that question to increase its visibilty and prompt the WinUI 3 team to respond.Colchester

© 2022 - 2024 — McMap. All rights reserved.