如何使用 WinRT 创建经典的 win32 应用程序 window?

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

Win32 API 中的 C++ WinRT 等价物是注册 window class、创建 window 然后通过消息保持活动状态泵回路?

我目前正在查看和阅读 WinRT 的文档,因为我想学习如何以现代 C++ 方式完成我以前在 Win32 中完成的所有工作。

到目前为止,我的经历很糟糕,我只想直截了当地承认我不明白。

我试过了,但因为我没有 运行 在容器中,所以线程的 CoreWindow 似乎还没有创建。

    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);
    }

C++/WinRT 是使用 Windows 运行时 (a.k.a.WinRT) API 的 Modern C++ 方式。这些 API 派生自 IInspectable,后者本身派生自 IUnknown。除了 COM 对象的 winrt::com_ptr 之外,它并没有真正为经典的 Win32 API 提供太多。

您当然可以使用 C++/WinRT 从 Win32 经典应用程序中使用 Windows 运行时 API,但是对于 Win32 经典程序没有 'CoreWindow' 这样的东西。所有 Windows::UI::CoreWindow 都与通用 Windows 平台 (UWP) 应用有关。

Microsoft Docs

C++ /WinRt 旨在支持使用 Windows 运行时类型系统编写的计划 API。其中包括 Windows.UI.XAML 和 Winodws.UI.Composition API。不过,这些 API 是为 UWP-style 应用程序设计的,与经典 Win32 API 交互的能力非常有限。

您可能会找到 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