桌面应用程序 (Visual C++)Desktop Applications (Visual C++)

C + + 中的 桌面应用程序 是一个本机应用程序,它可以访问完整的 Windows api 集,并可以在窗口或系统控制台中运行。A desktop application in C++ is a native application that can access the full set of Windows APIs and either runs in a window or in the system console. C + + 中的桌面应用程序可以通过 Windows 10 (在 Windows XP 上运行,尽管 Windows XP 不再正式受支持,并且自那时) 起,已经引入了许多 Windows Api。Desktop applications in C++ can run on Windows XP through Windows 10 (although Windows XP is no longer officially supported and there are many Windows APIs that have been introduced since then).

桌面应用程序不同于通用 Windows 平台 (UWP) 应用程序,该应用程序可以在运行 Windows 10 的电脑上运行,也可以在 XBox、Windows Phone、Surface Hub 和其他设备上运行。A desktop application is distinct from a Universal Windows Platform (UWP) app, which can run on PCs running Windows 10, and also on XBox, Windows Phone, Surface Hub, and other devices. 有关桌面和 UWP 应用程序的详细信息,请参阅 选择技术For more information about desktop vs. UWP applications, see Choose your technology.

桌面桥Desktop Bridge

在 Windows 10 中,你可以将现有的桌面应用程序或 COM 对象打包为 UWP 应用,并添加 UWP 功能(如触控),或从新式 Windows API 集中调用 Api。In Windows 10 you can package your existing desktop application or COM object as a UWP app and add UWP features such as touch, or call APIs from the modern Windows API set. 你还可以在 Visual Studio 中向桌面解决方案添加 UWP 应用,并将其打包在单个包中,并使用 Windows Api 在它们之间进行通信。You can also add a UWP app to a desktop solution in Visual Studio, and package them together in a single package and use Windows APIs to communicate between them.

在 Visual Studio 2017 版本15.4 及更高版本中,你可以创建一个 Windows 应用程序包项目,以大幅简化你的现有桌面应用程序的打包工作。In Visual Studio 2017 version 15.4 and later, you can create a Windows Application Package Project to greatly simplify the work of packaging your existing desktop application. 对于桌面应用程序使用的注册表调用或 Api,有几个限制适用,但在许多情况下,你可以在应用程序包中运行时创建备用代码路径以实现类似的功能。A few restrictions apply with respect to what registry calls or APIs your desktop application uses, but in many cases you can create alternate code paths to achieve similar functionality while running in an app package. 有关详细信息,请参阅桌面桥For more information, see Desktop Bridge.

术语Terminology

  • Win32 应用程序是一个 c + + 中的 Windows 桌面应用程序,可利用本机 Windows C api 和/或 COM Api CRT 和标准库 api 以及第三方库。A Win32 application is a Windows desktop application in C++ that can make use of native Windows C APIs and/or COM APIs CRT and Standard Library APIs, and 3rd party libraries. 在窗口中运行的 Win32 应用程序要求开发人员在 Windows 过程函数内显式处理 Windows 消息。A Win32 application that runs in a window requires the developer to work explicitly with Windows messages inside a Windows procedure function. 不管名称如何,Win32 应用程序都可以编译为32位 (x86) 或 64 (x64) 二进制文件。Despite the name, a Win32 application can be compiled as a 32-bit (x86) or 64-bit (x64) binary. 在 Visual Studio IDE 中,x86 和 Win32 术语是同义词。In the Visual Studio IDE, the terms x86 and Win32 are synonymous.

  • 组件对象模型 (COM) 是一种规范,用不同语言编写的程序可以相互通信。The Component Object Model (COM) is a specification that enables programs written in different languages to communicate with one another. 许多 Windows 组件是作为 COM 对象实现的,并遵循用于对象创建、接口发现和对象析构的标准 COM 规则。Many Windows components are implemented as COM objects and follow standard COM rules for object creation, interface discovery and object destruction. 使用 c + + 桌面应用程序中的 COM 对象相对简单,但编写自己的 COM 对象更高级。Using COM objects from C++ desktop applications is relatively straightforward, but writing your own COM object is more advanced. 活动模板库 (ATL) 提供可简化 COM 开发的宏和帮助程序函数。The Active Template Library (ATL) provides macros and helper functions that simplify COM development.

  • MFC 应用程序是一个 Windows 桌面应用程序,它使用 Microsoft 基础类 来创建用户界面。An MFC application is a Windows desktop application that use the Microsoft Foundation Classes to create the user interface. MFC 应用程序还可以使用 COM 组件以及 CRT 和标准库 Api。An MFC application can also use COM components as well as CRT and Standard Library APIs. MFC 为窗口消息循环和 Windows Api 提供了一个面向瘦 c + + 对象的包装器。MFC provides a thin C++ object-oriented wrapper over the window message loop and Windows APIs. MFC 是具有许多用户界面控件或自定义用户控件的应用程序(尤其是企业类型应用程序)的默认选择。MFC is the default choice for applications—especially enterprise-type applications—that have lots of user interface controls or custom user controls. MFC 为窗口管理、序列化、文本操作、打印和新式用户界面元素(如功能区)提供了便利的帮助器类。MFC provides convenient helper classes for window management, serialization, text manipulation, printing, and modern user interface elements such as the ribbon. 为了有效地处理 MFC,你应该熟悉 Win32。To be effective with MFC you should be familiar with Win32.

  • C + +/CLI 应用程序或组件使用 c + + 语法) (的扩展来实现 .NET 和本机 C + + 代码之间的交互。A C++/CLI application or component uses extensions to C++ syntax (as allowed by the C++ Standard) to enable interaction between .NET and native C++code. C + +/CLI 应用程序可以具有以本机方式运行的部分,以及在 .NET Framework 上运行的、可访问 .NET 基类库的部件。A C++/CLI application can have parts that run natively and parts that run on the .NET Framework with access to the .NET Base Class Library. 如果你的本机 c + + 代码需要使用以 c # 或 Visual Basic 编写的代码,则使用 c + +/CLI 是首选选项。C++/CLI is the preferred option when you have native C++ code that needs to work with code written in C# or Visual Basic. 它旨在在 .NET Dll 中使用,而不是在用户界面代码中使用。It is intended for use in .NET DLLs rather than in user interface code. 有关详细信息,请参阅使用 C++/CLI 进行 .NET 编程 (Visual C++)For more information, see .NET Programming with C++/CLI (Visual C++).

C + + 中的任何桌面应用程序都可以使用 C 运行时 (CRT) 和标准库类和函数、COM 对象和公共 Windows 函数,它们统称为 Windows API。Any desktop application in C++ can use C Runtime (CRT) and Standard Library classes and functions, COM objects, and the public Windows functions, which collectively are known as the Windows API. 有关 c + + 中的 Windows 桌面应用程序的简介,请参阅 Win32 和 c + + 入门For an introduction to Windows desktop applications in C++, see Get Started with Win32 and C++.

本节内容In this section

TitleTitle 描述Description
C++ 中的 Windows 控制台应用程序Windows Console Applications in C++ 包含有关控制台应用的信息。Contains information about console apps. Win32(或 Win64)控制台应用程序本身不具有窗口,且不使用消息循环。A Win32 (or Win64) console application has no window of its own and no message loop. 它在控制台窗口中运行,并通过命令行处理输入和输出。It runs in the console window, and input and output are handled through the command line.
演练:创建 Windows 桌面应用程序 (C++)Walkthrough: Creating Windows Desktop Applications (C++) 创建一个简单的 Windows 桌面应用程序。Create a simple Windows desktop application.
创建空的 Windows 桌面应用程序Creating an Empty Windows Desktop Application 如何创建没有默认文件的 Windows 桌面项目。How to create a Windows desktop project that has no default files.
向空的 Win32 应用程序添加文件Adding Files to an Empty Win32 Applications 如何将文件添加到空项目。How to add files to an empty project.
使用资源文件Working with Resource Files 如何向桌面应用程序添加图像、图标、字符串表以及其他资源。How to add images, icons, string tables, and other resources to a desktop application.
有关使用 DirectX 创建游戏的资源 (C++)Resources for Creating a Game Using DirectX (C++) 用 c + + 创建游戏的内容链接。Links to content for creating games in C++.
演练:创建和使用静态库Walkthrough: Creating and Using a Static Library 如何创建 .lib 二进制文件。How to create a .lib binary file.
如何:在 Windows 桌面应用程序中使用 Windows 10 SDKHow to: Use the Windows 10 SDK in a Windows Desktop Application 包含使用 Windows 10 SDK 设置项目以便生成的步骤。Contains steps for setting up your project to build using the Windows 10 SDK.
TitleTitle 描述Description
Windows 开发Windows Development 包含有关 Windows API 和 COM 的信息。Contains information about the Windows API and COM. (某些 Windows API 和第三方 DLL 是作为 COM 对象实现的。)(Some Windows APIs and third-party DLLs are implemented as COM objects.)
Hilo:开发适用于 Windows 7 的 C++ 应用程序Hilo: Developing C++ Applications for Windows 7 描述如何创建使用 Windows Animation 和 Direct2D 创建基于传送的用户界面的、客户端丰富的 Windows 桌面应用程序。Describes how to create a rich-client Windows desktop application that uses Windows Animation and Direct2D to create a carousel-based user interface. 此教程自 Windows 7 以来尚未更新,但它仍提供了 Win32 编程的全面介绍。This tutorial has not been updated since Windows 7 but it still provides a thorough introduction to Win32 programming.
C + + 中的 Windows 编程概述Overview of Windows Programming in C++ 介绍 c + + 中 Windows 桌面编程的主要功能。Describes key features of Windows desktop programming in C++.

请参阅See also

Visual Studio 中的 C++C++ in Visual Studio