Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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);
    }
like image 200
vlind Avatar asked Jul 04 '19 18:07

vlind


People also ask

Is WinRT based on Win32?

Technology. WinRT is implemented in the programming language C++ and is object-oriented by design. Its underlying technology, the Windows API (Win32 API), is written mostly in the language C.

Does WinRT replace Win32?

What is WinRT(Windows Runtime)? In simple words, Windows Runtime Library (WinRT) is the default application programming interface (API) used by Windows 8. It does not replace the Win32 API that has been running underneath all Windows applications, but rather augments it.

How do I create a Win32 application in C++?

On the File menu, choose New and then choose Project. In the New Project dialog box, in the left pane, expand Installed > Templates > Visual C++, and then select Win32. In the middle pane, select Win32 Project. In the Name box, type a name for the project, for example, DesktopApp.


2 Answers

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

like image 103
Chuck Walbourn Avatar answered Sep 22 '22 19:09

Chuck Walbourn


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.

like image 42
Ben Kuhn Avatar answered Sep 25 '22 19:09

Ben Kuhn