How do you create a classic win32 application window with WinRT?
Asked Answered
S

2

6

What's the C++ WinRT equivalent for what in the Win32 API would be registering a window class, creating a window and then keeping it alive via a message pump loop?

I'm currently looking at and reading the documentation for WinRT because I wanted to learn how to do all of the stuff I used to do in Win32 the modern C++ way.

My experience has been awful so far and I'm just gonna admit straight out that I'm not getting it.

I tried this but since I'm not running in a container it seems like the CoreWindow for the thread hasn't been created yet.

    int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
    {
        winrt::init_apartment(winrt::apartment_type::single_threaded);
        winrt::Windows::UI::Core::CoreWindow window = winrt::Windows::UI::Core::CoreWindow::GetForCurrentThread();

        window.Activate();

        auto dispatcher = window.Dispatcher();

        using DispatcherOptions = winrt::Windows::UI::Core::CoreProcessEventsOption;
        const DispatcherOptions options = DispatcherOptions::ProcessUntilQuit;

        dispatcher.ProcessEvents(options);
    }
Suffragan answered 4/7, 2019 at 18:36 Comment(3)
It's common for winrt to be initialized to multithreaded. I don't know if single threaded has any purpose. About your first line.Risotto
If you are looking for a C++ library that helps you in writing a classic Win32 desktop application, have a look into the Windows Implementation Libraries (WIL). Although C++/WinRT offers some resource wrappers for Win32 objects in its winrt/base.h header, that header is now generated. You may be able to have it generated for a classic Win32 desktop app, too, but I'm not sure how that would work. Given the limited facilities it offers for Win32 programming, I'd probably just go with the WIL instead.Glycoside
While most "Win32" and "UWP" features and APIs have become easier to mix and match over time, the windowing systems are still an exception. Win32 CreateWindow, HWNDs, etc. can only be used directly from Win32/desktop/full trust apps, while UWP CoreWindow/AppWindow can only be used from UWP apps. While you can use most WinRT APIs from Win32 apps, there are no WinRT APIs for directly manipulating Win32 HWNDs, you would just use the same flat C APIs as always (or some wrapper such as WIL that's been suggested)Oilcan
S
4

C++/WinRT is the Modern C++ way of using Windows Runtime (a.k.a. WinRT) APIs. These APIs are derived from IInspectable, which itself derives from IUnknown. Other than the winrt::com_ptr for COM objects, it doesn't really offer much for classic Win32 APIs.

You can certainly use C++/WinRT to consume Windows Runtime APIs from a Win32 classic application, but there's no such thing as a 'CoreWindow' for Win32 classic programs. All of Windows::UI::CoreWindow is related to Universal Windows Platform (UWP) apps.

See Microsoft Docs

Slimsy answered 5/7, 2019 at 8:2 Comment(6)
Here's an example: gist.github.com/kennykerr/62923cdacaba28fedc4f3dab6e0c12ecRepresent
So that sample is setting up a XAML UI window in a Win32 desktop loop?Slimsy
That's possible too, but this one uses the Windows.UI.Composition API to render a Win32-style desktop app with an HWND and message loop.Represent
@ChuckWalbourn Does it really make sense to include <base.h> just to use winrt::com_ptr to consume COM stuff like DX, WASAPI and no other WinRT APIs. What is the current state (2020) of Win32 applications and C++/WinRT? Could they use WinRT APIs safely and could such applications be supported on the classic Desktop without restrictions and also packaged and distributed as Windows Store applications. I have completely lost track about what is going on currently.Cyrillus
@Cyrillus Personally for my COM smart-pointer usage, I've stuck with Microsoft::WRL::ComPtr because it's in all the Microsoft platforms back to the Windows 8.0 SDK including the Xbox One XDK and Microsoft GDK.Slimsy
@Cyrillus Win32 "classic" desktop applications can be published in the Windows Store via Windows Desktop Bridge. This is independent to the use of Windows Runtime APIs. Some of them support Win32 via single apartment, some work in multi-threaded, and others don't work at all. See Microsoft Docs.Slimsy
E
2

C++ /WinRt was designed to support the projected APIs written using the Windows Runtime type system. These include the Windows.UI.XAML and Winodws.UI.Composition APIs. These APIs are designed for UWP-style applications though, and have very limited ability to interface with the classic Win32 APIs.

You might find the WIL header libraries useful. It doesn't have the complete modern wrappers for C++ that you get with C++ /WinRT, but you will find the libraries have smart wrappers and various convenience helpers for many Win32 constructs. You can find documentation here.

Enos answered 6/7, 2019 at 7:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.