简体   繁体   中英

Windows GUI: Can I access the "modern ui" from C++?

I am trying to find an answer to a simple question: Can I use the modern UI - that you see in Windows 10 a lot, which is affected by the dark theme too - from C/C++?

I know that the Win32 API windows are not affected by the dark theme, at least for the most part. I have also not found a definitive clue if .NET Windows Forms is affected and UWP uses this odd C++ subset called CX? I am a little confused.

Basically, I just want to pick the right API to design my GUI with the modern UI elements that are commonly used in Windows 10. One of the reasons is that these controls are properly made accessible already - something which other GUI frameworks might actually lack. (I am visually impaired myself and thus do rough testing with NVDA since most of my friends are reliant on these tools.)

The "modern" UI framework in Windows (which never got a name) was initially only available to applications targeting the Universal Windows Platform (UWP). Later it was made available to classic Desktop applications by way of XAML islands . Later still it was decided to decouple the UI framework from the OS, so that changes can ship independent of OS updates. This is what became WinUI 2 . With WinUI 3 it is planned to be fully decoupled, making the UI immediately available to classic Desktop applications.

The entire API surface is exposed as Windows Runtime types. While you can interact with those types at the ABI level (which is what the WRL does) it is more convenient to use what is coined a language projection . Initially, that was C++/CX, a non-standard language extension implem.neted by Microsoft's compiler, which was later superseded by C++/WinRT , a language projection implemented in Standard C++.

So yes, it is possible to use the new UI framework in a classic Desktop application from C++. Though it has to be noted, that C++/WinRT is still not on par with C++/CX when it comes to IDE integration, and - especially - XAML authoring and binding. Still possible, but it is occasionally more tedious.

Use the WinUI API: https://learn.microsoft.com/en-us/windows/apps/winui/winui3/ Note: you can use that with c++, not with c.

I guess all you need is to enable visual styles. By default Win32 app will look like Windows 98/2000 GUI. To switch window borders to WinXP/7/10 style, you need to add manifest file with simple contents and initialize Common Controls library (comctl32.dll) with InitCommonControls() or InitCommonControlsEx() . See the following articles:

https://learn.microsoft.com/en-us/windows/win32/controls/cookbook-overview https://www.codeproject.com/Questions/58606/How-do-you-get-XP-visual-styles-manifest-to-work-o

In short you need to do 3 steps:

  1. Add comctl32.lib to your project;
  2. Call InitCommonControls(); (or InitCommonControlsEx ) in WinMain ;
  3. Embed manifest file or include a file with .manifest extension and same name like your .exe . See Manifest Tool entry in Visual Studio project properties (Additional Manifest Files).

Sample manifest file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
    version="1.0.0.0"
    processorArchitecture="*"
    name="CompanyName.ProductName.YourApplication"
    type="win32"
/>
<description>Application Description</description>
<dependency>
<dependentAssembly>
<assemblyIdentity
    type="win32"
    name="Microsoft.Windows.Common-Controls"
    version="6.0.0.0"
    processorArchitecture="*"
    publicKeyToken="6595b64144ccf1df"
    language="*"
/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM