C++/WinRT 是 Windows 运行时 (WinRT) API 的完全标准新式 C++17 语言投影,以基于标头文件的库的形式实现,旨在为你提供对新式 Windows API 的一流访问。C++/WinRT is an entirely standard modern C++17 language projection for Windows Runtime (WinRT) APIs, implemented as a header-file-based library, and designed to provide you with first-class access to the modern Windows API. 利用 C++/WinRT,你可以采用任何符合标准的 C++17 编译器创作和使用 Windows 运行时 API。With C++/WinRT, you can author and consume Windows Runtime APIs using any standards-compliant C++17 compiler. Windows SDK 包含 C++/WinRT;它已在版本 10.0.17134.0(Windows 10,版本 1803)中引用。The Windows SDK includes C++/WinRT; it was introduced in version 10.0.17134.0 (Windows 10, version 1803).

C++/WinRT 适合有兴趣编写适用于 Windows 的美观、快速的代码的任何开发人员。C++/WinRT is for any developer interested in writing beautiful and fast code for Windows. 原因如下。Here's why.

针对 C++/WinRT 的案例The case for C++/WinRT


C++ 编程语言适用于企业独立软件供应商 (ISV) 行业中重视高水平的正确性、质量和性能的应用场合。The C++ programming language is used both in the enterprise and independent software vendor (ISV) segments for applications where high levels of correctness, quality, and performance are valued. 例如:系统编程;资源受限的嵌入式和移动系统;游戏和图形;设备驱动程序;以及工业、科学和医疗应用,等等。For example: systems programming; resource-constrained embedded and mobile systems; games and graphics; device drivers; and industrial, scientific, and medical applications, to name but some.

从语言的角度来说,C++ 一直专注于创作和使用类型丰富且轻量的抽象。From a language point of view, C++ has always been about authoring and consuming abstractions that are both type-rich and lightweight. 但是,由于原始的指针、原始的循环、耗时耗力的内存分配以及 C++98 的发布,该语言已经发生了根本性的改变。But the language has changed radically since the raw pointers, raw loops, and painstaking memory allocation and releasing of C++98. 新式 C++(从 C++11 起)可以清楚地表达想法,具有简便性和可读性并且引入 bug 可能性低得多。Modern C++ (from C++11 onward) is about clear expression of ideas, simplicity, readability, and a much lower likelihood of introducing bugs.

对于采用 C++ 编写和使用 Windows 运行时 API,有 C++/WINRT 可用。For authoring and consuming Windows Runtime APIs using C++, there is C++/WinRT. 这是 Microsoft 推荐的用于替代 C++/CX 语言投影和 Windows 运行时 C++ 模板库 (WRL) 的替代品。This is Microsoft's recommended replacement for the C++/CX language projection, and the Windows Runtime C++ Template Library (WRL).

当采用 C++/WinRT 时,你将使用标准 C++ 数据类型、算法和关键字。You use standard C++ data types, algorithms, and keywords when you use C++/WinRT. 投影确实有自己的自定义数据类型,但在大多数情况下,你无需了解它们,因为它们将提供到/自标准类型的相应转换。The projection does have its own custom data types, but in most cases you don't need to learn them because they provide appropriate conversions to and from standard types. 这样,你就可以继续使用已用惯的标准 C++ 语言功能和已拥有的源代码。That way, you can continue to use the standard C++ language features that you're accustomed to using, and the source code that you already have. C++/WinRT 使得在任何 C++ 应用程序(从 Win32 到 UWP)中调用 Windows 运行时 API 变得轻而易举。C++/WinRT makes it extremely easy to call Windows Runtime APIs in any C++ application, from Win32 to UWP.

与适用于 Windows 运行时的任何其他语言选择相比,C++/WinRT 的表现更好,生成的二进制文件更小。C++/WinRT performs better and produces smaller binaries than any other language option for the Windows Runtime. 它的表现甚至超过了直接使用 ABI 接口手动编写的代码。It even outperforms handwritten code using the ABI interfaces directly. 这是因为抽象使用了 Visual C++ 编译器能够优化的新式 C++ 习惯用语。That's because the abstractions use modern C++ idioms that the Visual C++ compiler is designed to optimize. 这包括神奇静态变量、空基类、strlen 删除以及最新版本的 Visual C++ 中的很多专门用于改善 C++/WinRT 的性能的更新的优化。This includes magic statics, empty base classes, strlen elision, as well as many newer optimizations in the latest version of Visual C++ targeted specifically at improving the performance of C++/WinRT.

可以通过多种方式将 C++/WinRT 逐步引入项目。There are ways to gradually introduce C++/WinRT into your projects. 可以使用 Windows 运行时组件,也可以通过 C++/CX 进行互操作。You could use Windows Runtime components, or you could interoperate with C++/CX. 有关详细信息,请参阅实现 C++/WinRT 与 C++/CX 之间的互操作For more info, see Interop between C++/WinRT and C++/CX.

要详细了解如何移动到 C++/WinRT,请参阅以下资源。For info about porting to C++/WinRT, see these resources.

另请参阅可在哪里找到 C++/WinRT 示例应用?Also see Where can I find C++/WinRT sample apps?.

有关 C++/WinRT 的主题Topics about C++/WinRT

主题Topic 说明Description
C++/WinRT 简介Introduction to C++/WinRT 对 C++/WinRT(一种适用于 Windows 运行时 API 的标准 C++ 语言投影)的介绍。An introduction to C++/WinRT—a standard C++ language projection for Windows Runtime APIs.
C++/WinRT 入门Get started with C++/WinRT 为了帮助你更快地开始使用 C++/WinRT,本主题详细介绍了一个简单的代码示例。To get you up to speed with using C++/WinRT, this topic walks through a simple code example.
C++/WinRT 中的新增功能What's new in C++/WinRT C++/WinRT 的新增功能和更改。News and changes to C++/WinRT.
常见问题解答Frequently-asked questions 对你可能存疑的关于通过 C++/WinRT 创作和使用 Windows 运行时 API 的问题的解答。Answers to questions that you're likely to have about authoring and consuming Windows Runtime APIs with C++/WinRT.
故障排除Troubleshooting 无论你是要削减新代码还是要移植现有应用,本主题中的症状排查和补救措施表都可能对你有帮助。The table of troubleshooting symptoms and remedies in this topic may be helpful to you whether you're cutting new code or porting an existing app.
照片编辑器 C++/WinRT 示例应用程序Photo Editor C++/WinRT sample application 照片编辑器是一个 UWP 示例应用程序,其通过 C++/WinRT 语言投影展示开发。Photo Editor is a UWP sample application that showcases development with the C++/WinRT language projection. 此示例应用程序允许你从图片库检索照片,然后使用分类的照片效果编辑选择的图像。The sample application allows you to retrieve photos from the Pictures library, and then edit the selected image with assorted photo effects.
字符串处理String handling 利用 C++/WinRT,你可以使用标准 C++ 宽字符串类型来调用 Windows 运行时 API,也可以使用 winrt::hstring 类型。With C++/WinRT, you can call Windows Runtime APIs using standard C++ wide string types, or you can use the winrt::hstring type.
标准 C++ 数据类型和 C++/WinRTStandard C++ data types and C++/WinRT 利用 C++/WinRT,你可以使用标准 C++ 数据类型调用 Windows 运行时 API。With C++/WinRT, you can call Windows Runtime APIs using Standard C++ data types.
将标量值装箱到 IInspectable 和从 IInspectable 取消标量值装箱Boxing and unboxing scalar values to IInspectable 标量值需要先封装到引用类对象内,然后再传递到需要 IInspectable 的函数。A scalar value needs to be wrapped inside a reference class object before being passed to a function that expects IInspectable. 该封装过程称为对值进行装箱That wrapping process is known as boxing the value.
通过 C++/WinRT 使用 APIConsume APIs with C++/WinRT 本主题介绍了如何使用 C++/WinRT API,无论它们是由 Windows、第三方组件供应商还是由你自己实现的。This topic shows how to consume C++/WinRT APIs, whether they're implemented by Windows, a third-party component vendor, or by yourself.
使用 C++/WinRT 创作 APIAuthor APIs with C++/WinRT 本主题展示了如何直接或间接使用 winrt::implements 基结构来创作 C++/WinRT API。This topic shows how to author C++/WinRT APIs by using the winrt::implements base struct, either directly or indirectly.
C++/WinRT 的错误处理Error handling with C++/WinRT 本主题讨论了处理使用 C++/WinRT 编程时出现的错误的策略。This topic discusses strategies for handling errors when programming with C++/WinRT.
使用代理处理事件Handle events by using delegates 本主题介绍了如何使用 C++/WinRT 注册和撤销事件处理委托。This topic shows how to register and revoke event-handling delegates using C++/WinRT.
创作事件Author events 本主题演示了如何创作包含引发事件的运行时类的 Windows 运行时组件。This topic demonstrates how to author a Windows Runtime component containing a runtime class that raises events. 它还演示了使用该组件并处理事件的应用。It also demonstrates an app that consumes the component and handles the events.
使用 C++/WinRT 的集合Collections with C++/WinRT C++/WinRT 提供了函数和基类,当你希望实现并/或传递集合时,它们可以为你节省大量的时间和精力。C++/WinRT provides functions and base classes that save you a lot of time and effort when you want to implement and/or pass collections.
并发和异步操作Concurrency and asynchronous operations 本主题介绍了你可通过 C++/WinRT 创建和使用 Windows 运行时异步对象的方式。This topic shows the ways in which you can both create and consume Windows Runtime asynchronous objects with C++/WinRT.
更高级的并发和异步More advanced concurrency and asynchrony 使用 C++/WinRT 的具有并发性和异步性的更高级方案。More advanced scenarios with concurrency and asynchrony in C++/WinRT.
XAML 控件; 绑定到 C++/WinRT 属性XAML controls; bind to a C++/WinRT property 可有效地绑定到 XAML 项目控件的属性称为可观测属性。A property that can be effectively bound to a XAML control is known as an observable property. 本主题介绍了如何实现和使用可观测属性以及如何将 XAML 控件绑定到该属性。This topic shows how to implement and consume an observable property, and how to bind a XAML control to it.
XAML 项目控件; 绑定到 C++/WinRT 集合XAML items controls; bind to a C++/WinRT collection 可有效地绑定到 XAML 项目控件的集合称为可观测集合。A collection that can be effectively bound to a XAML items control is known as an observable collection. 本主题介绍了如何实现和使用可观测集合以及如何将 XAML 项目控件绑定到该集合。This topic shows how to implement and consume an observable collection, and how to bind a XAML items control to it.
XAML 自定义(模板化)控件与 C++/WinRTXAML custom (templated) controls with C++/WinRT 本主题引导你完成使用 C++/WinRT 创建简单的自定义控件的步骤。This topic walks you through the steps of creating a simple custom control using C++/WinRT. 你可以基于此处的信息创建你自己的功能丰富且可自定义的 UI 控件。You can build on the info here to create your own feature-rich and customizable UI controls.
将参数传递到 ABI 边界Passing parameters into the ABI boundary C++/ WinRT 通过提供针对常见情况的自动转换,简化了将参数传递到 ABI 边界。C++/WinRT simplifies passing parameters into the ABI boundary by providing automatic conversions for common cases.
通过 C++/WinRT 使用 COM 组件Consume COM components with C++/WinRT 本主题通过一个完整的 Direct2D 代码示例展示了如何通过 C++/WinRT 来使用 COM 类和接口。This topic uses a full Direct2D code example to show how to use C++/WinRT to consume COM classes and interfaces.
通过 C++/WinRT 创作 COM 组件Author COM components with C++/WinRT C++/WinRT 可以帮助你创作经典 COM 组件,就像它可以帮助你创作 Windows 运行时类一样。C++/WinRT can help you to author classic COM components, just as it helps you to author Windows Runtime classes.
从 C++/CX 移动到 C++/WinRTMove to C++/WinRT from C++/CX 本主题介绍将 C++/CX 项目中的源代码移植到 C++/WinRT 项目中的等效项时所涉及的技术细节。This topic describes the technical details involved in porting the source code in a C++/CX project to its equivalent in C++/WinRT.
实现 C++/WinRT 与 C++/CX 之间的互操作Interop between C++/WinRT and C++/CX 本主题介绍了可用于在 C++/CXC++/WinRT 对象之间进行转换的两个帮助程序函数。This topic shows two helper functions that can be used to convert between C++/CX and C++/WinRT objects.
实现 C++/WinRT 与 C++/CX 之间的异步和互操作Asynchrony, and interop between C++/WinRT and C++/CX 这是与从 C++/CX 逐步移动到 C++/WinRT 相关的高级主题。This is an advanced topic related to gradually porting from C++/CX to C++/WinRT. 它显示并行模式库 (PPL) 任务和协同例程如何在同一项目中并行存在。It shows how Parallel Patterns Library (PPL) tasks and coroutines can exist side by side in the same project.
从 WRL 移动到 C++/WinRTMove to C++/WinRT from WRL 本主题介绍了如何将 Windows 运行时 C++ 模板库 (WRL) 代码移植到 C++/WinRT 中的等效项。This topic shows how to port Windows Runtime C++ Template Library (WRL) code to its equivalent in C++/WinRT.
将 Clipboard 示例从 C# 移植到C++—案例研究Porting the Clipboard sample to C++/WinRT from C#—a case study 本主题提供了一个有关将通用 Windows 平台 (UWP) 应用示例之一从 C# 移植到 C++/WinRT 的案例研究。This topic presents a case study of porting one of the Universal Windows Platform (UWP) app samples from C# to C++/WinRT. 可以通过按照本演练并自行移植示例,获取移植实践和体验。You can gain porting practice and experience by following along with the walkthrough and porting the sample for yourself as you go.
从 C# 移动到 C++/WinRTMove to C++/WinRT from C# 本主题对 C# 项目中的源代码移植到 C++/WinRT 项目中的等效项时所涉及的技术细节进行了全面分类。This topic comprehensively catalogs the technical details involved in porting the source code in a C# project to its equivalent in C++/WinRT.
实现 C++/WinRT 与 ABI 之间的互操作Interop between C++/WinRT and the ABI 本主题介绍了如何在应用程序二进制接口 (ABI) 和 C++/WinRT 对象之间进行转换。This topic shows how to convert between application binary interface (ABI) and C++/WinRT objects.
C++/WinRT 中的弱引用Strong and weak references in C++/WinRT Windows 运行时是引用在其中占有重要地位的一个系统;在这样的系统中,了解强引用与弱引用的意义和区别非常重要。The Windows Runtime is a reference-counted system; and in such a system it's important for you to know about the significance of, and distinction between, strong and weak references.
敏捷对象Agile objects 敏捷对象是可从任何线程访问的对象。An agile object is one that can be accessed from any thread. C++/WinRT 类型默认情况下是敏捷对象,但你可以选择退出。Your C++/WinRT types are agile by default, but you can opt out.
诊断直接分配Diagnosing direct allocations 本主题深入探讨了一种 C++/WinRT 2.0 功能,该功能可帮助你诊断在堆栈上创建实现类型对象的错误,而无需使用 WinRT:: make 系列的帮助程序。This topic goes in-depth on a C++/WinRT 2.0 feature that helps you diagnose the mistake of creating an object of implementation type on the stack, rather than using the winrt::make family of helpers, as you should.
用于实现类型的扩展点Extension points for your implementation types 使用 C++/WinRT 2.0 中的这些扩展点可以延迟实现类型的析构、在析构期间安全地查询,以及与投影方法的入口和出口挂钩。These extension points in C++/WinRT 2.0 allow you to defer destruction of your implementation types, to safely query during destruction, and to hook the entry into and exit from your projected methods.
一个简单的 C++WinRT Windows UI 库示例A simple C++/WinRT Windows UI Library example 本主题将指导你完成在 C++/WinRT 项目内添加对 WinUI 的简单支持的过程。This topic walks you through the process of adding simple support for WinUI within a C++/WinRT project.
使用 C++/WinRT 创建 Windows 运行时组件Windows Runtime components with C++/WinRT 本主题展示了如何使用 C++/WinRT 创建和使用 Windows 运行时组件 — 一种可以从使用任何 Windows 运行时语言构建的通用 Windows 应用调用的组件。This topic shows how to use C++/WinRT to create and consume a Windows Runtime component—a component that's callable from a Universal Windows app built using any Windows Runtime language.

有关 C++ 语言的主题Topics about the C++ language

主题Topic 说明Description
值类别以及对它们的引用Value categories, and references to them 本主题介绍了 C++ 中存在的各种值类别。This topic describes the various categories of values that exist in C++. 你肯定听说过左值和右值,但还有其他类型。You will doubtless have heard of lvalues and rvalues, but there are other kinds, too.

重要的 APIImportant APIs