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 後續版本) 是關於清楚運算式的想法、簡單、可讀性和較低引入錯誤的可能性。Modern C++ (from C++11 onward) is about clear expression of ideas, simplicity, readability, and a much lower likelihood of introducing bugs.

使用 C++,還有 C++/WinRT,適合撰寫與使用 Windows 執行階段 API。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++ 應用程式中很輕鬆呼叫 Windows 執行階段 API,從 Win32 到 UWP。C++/WinRT makes it extremely easy to call Windows Runtime APIs in any C++ application, from Win32 to UWP.

C++/WinRT 執行得更好,且比適用於 Windows 執行階段的任何其他語言選項所產生的二進位檔要小。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. 這是因為抽象概念使用現代化 C++ 慣用語法,Visual C++ 編譯器設計用來最佳化。That's because the abstractions use modern C++ idioms that the Visual C++ compiler is designed to optimize. 最新版的 Visual C++ 中包括神奇靜態、空白的基底類別、strlen elision,以及許多較新的最佳化,最新版特別針對 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++/WinRTGet 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.
Photo Editor C++/WinRT 範例應用程式Photo Editor C++/WinRT sample application Photo Editor 是 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.
將純量數值 Boxing 和 unboxing 到 IInspectableBoxing 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. 該包裝程序稱為「boxing」** 值。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.
使用 C++/WinRT 的 XAML 自訂 (範本化) 控制項XAML 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.
將剪貼簿範例從 C# 移植到 C++/WinRT—案例研究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