学习 MFC 编程的先决条件

2022-01-12 00:00:00 windows mfc

我知道一点 C++ 和 C,而我现在正在处理的项目是大量的 MFC 编程.有经验的能告诉我学习MFC的先决条件吗?

I know a bit of C++ and C and the project I am working with now is a whole lot of MFC programming. Can someone experienced tell me the prerequisites for learning MFC.

另外,什么是最好的学习资源?

Also, What the are best sources to learn from ?

任何特定的书籍或视频系列?

Any particular book or video series?

我知道这个问题太笼统了,但答案可能对我(或任何正在研究 MFC 的人)有很大帮助

I know the question is too general but answers might help me(or anyone else who is digging into MFC) a lot

谢谢!

推荐答案

+1 好问题!

tl;dr:按顺序学习 Win32.

tl;dr: Learn Win32 - in that order.

到目前为止,MFC 最重要的先决条件是对 Windows API(也有时称为 Win32 API).您还需要熟悉 C++ 的一些关键概念并熟悉这些工具.我建议的先决条件列表是:

By far the most important prerequisite to MFC is a solid understanding of the Windows API (also sometimes referred to as the Win32 API). You will also need to be familiar with a few key concepts of C++ and get intimate with the tools. My proposed list of prerequisites would be:

  1. 具备扎实的 Windows API 开发背景.
  2. 熟悉 C++ 的相关概念.
  3. 了解工具.

以下是这些步骤的概要,以及指向其他信息的链接.

The following is a rundown of these steps, with links to additional information.

1.Windows API:

Windows API 通过 C 接口公开所有服务.因此,资源管理通常是乏味的样板代码.并且在源代码中可见(有时在阅读代码时是一个令人难以置信的奖励).MFC 在很大程度上是一个围绕 Windows API 的自动化资源管理包装器(和实用程序库),隐藏了大部分资源管理和类型转换.要精通 MFC,您必须了解它隐藏的内容.

The Windows API exposes all services through a C interface. As a consequence resource management is often tedious boiler plate code. And visible in source code (sometimes an incredible bonus when reading code). MFC is - to a large extent - an automated resource management wrapper (and utility library) around the Windows API, hiding most of the resource management and type conversions. To be fluent in MFC you have to understand what it hides.

要开始使用 Windows API,您只需要快速了解主要组件,我会推荐 theForger 的 Win32 API教程 (by forgey of #winprog 成名).它涵盖了消息处理、窗口化、控件和GDI,并奠定了坚实的基础.如果您想投入(花费)时间更详细地探索 Windows API,那么迄今为止最好的资源可能仍然是 Charles Petzold 的 编程 Windows(不要与 托管 Petzold).MSDN 也是获取特定领域的概述和详细文档的好资源,例如:

To get started with the Windows API and all you need is a quick rundown of the major components I would recommend theForger's Win32 API Tutorial (by forgey of #winprog fame). It covers Message Handling, Windowing, Controls, and the GDI, and builds a solid foundation. If you feel like investing (well spent) time into exploring the Windows API in more detail, the best resource to date is probably still Charles Petzold's Programming Windows (not the be confused with the managed Petzold). The MSDN is also a good resource to get both an overview as well as detailed documentation for specific areas like:

  • 消息处理
  • 窗口化
  • 用户交互
  • Windows 控件

<强>2.关键 C++ 概念:

MFC 是用 C++ 实现的.虽然它早于官方 C++ 标准,但您不会在其中找到太多时髦的业务.非常少的模板代码,当然也没有新奇的 C++11 功能.熟悉基本的 C++ 概念会让你走得更远.

MFC is implemented in terms of C++. While it predates the official C++ standard you won't find too much funky business in there. Very little template code and certainly none of the new-fangled C++11 features. Intimacy with basic C++ concepts will get you a long way.

如前所述,MFC 在很大程度上是围绕 Windows API 的资源管理包装器.Windows API 资源和 MFC 对象之间通常存在直接映射(例如 HWND : CWnd,或 HDC : CDC).如果您了解构造函数、析构函数和对象生命周期,那么您几乎就已经掌握了该部分.

As mentioned previously, MFC is to a large extent a resource management wrapper around Windows API. Often there is a direct mapping between Windows API resources and MFC objects (like HWND : CWnd, or HDC : CDC). If you understand constructors, destructors and object lifetime you're pretty much all set in that department.

使用 MFC 的基于模板的容器(如 CMap) 你自然会接触到模板.那里没有太多涉及,只是非常基本的类型参数化以重用容器代码并启用类型安全的元素访问.MFC 容器的介绍可以在这里找到:Collections.

When using MFC's template-based containers (like CMap) you will be exposed to templates, naturally. Nothing too involved there, just very basic type parameterization to reuse the container code and enable type-safe element access. An introduction to MFC containers can be found here: Collections.

异常在 MFC 中很少使用,主要是在访问文件或序列化数据时.您仍然应该知道如何编写异常安全代码;您将在非平凡的应用程序中看到异常.可以在 MFC 中的异常处理中找到概述.

Exceptions are used rarely in MFC, mostly when accessing files or when serializing data. You should still know how to write exception safe code; you will see exceptions in non-trivial applications. An overview can be found at Exception Handling in MFC.

预处理器不是真正属于 C++ 的一部分,但在整个 MFC 中广泛使用的一个领域是预处理器.无论您的 MFC 应用程序多么微不足道,都会有宏.您必须非常了解预处理器的语法以及预处理器的操作方式.

One area which is not really part of C++ but used extensively throughout the MFC is the preprocessor. Regardless of how trivial your MFC application is, there will be macros. You have to have a very good understanding of both the preprocessor syntax as well as how the preprocessor operates.

3.工具:

虽然可以单独使用记事本编写 MFC 应用程序,但这样做肯定不是很有效.纯文本编辑器可能是学习该平台的好主意,但当需要满足里程碑和截止日期时,您肯定会想要使用功能强大的 IDE.

While it is possible to write MFC applications with Notepad alone it is certainly not very efficient to do so. A plain text editor might be a good idea for learning the platform, but when it comes time to meet milestones and deadlines you certainly will want to use a powerful IDE.

MFC 几乎暗示了 Visual Studio 的使用,我假设这就是您正在使用的.如果您使用的是 VS6 或 VS2010(或更高版本),您将可以访问 MFC 类向导.这是您将经常使用的工具.为了安全地操作它,您必须熟悉它为您生成的代码.玩弄它,检查生成的代码,并尝试建立它的心理模型.如果还没有意义,你应该稍后再回来.

MFC pretty much implies the use of Visual Studio and I will assume that's what you're using. If you are using VS6 or VS2010 (or later) you will have access to the MFC Class Wizard. This is a tool you will be using frequently. To safely operate it you must get familiar with the code it generates for you. Play around with it, inspect the generated code, and try to build a mental model of it. If it doesn't make sense yet you should return later.

MFC 类向导几乎肯定会生成预处理器代码.它隐藏了大量的复杂性,您需要了解这些复杂性才能安全使用.使用 IDE 浏览预处理器宏,尝试在您的脑海中扩展它们,看看您是否理解生成的代码.如果您在解开宏调用时遇到困难,请让编译器使用 /P(预处理到文件) 编译器选项.

The MFC class wizard will almost certainly generate preprocessor code. It hides a tremendous amount of complexity which you need to understand to use safely. Use the IDE to navigate through the preprocessor macros, try to expand them in your mind, and see if you understand the resulting code. If you are having difficulty unmangling the macro invocations have the compiler output the preprocessed code for you using the /P (Preprocess to a File) compiler option.

有时您必须查看或调试 MFC 源代码.要使源代码可用于源代码浏览和调试器,您必须设置 VC++ 目录 以包括以下内容(如果尚不存在):

Occasionally you have to look or debug into the MFC source code. To make the source code available to source browsing and the debugger you have to set up the VC++ Directories to include the following (if not already present):

$(VCInstallDir)atlmfcsrcmfc
$(VCInstallDir)atlmfcsrcmfcm
$(VCInstallDir)atlmfcsrcatl

根据 IDE,这可以通过工具 -> 选项:项目和解决方案 -> VC++ 目录 或项目设置的属性表来完成.

Depending on the IDE this is either done through Tools -> Options: Projects and Solutions -> VC++ Directories or the property sheet of your project settings.

寻求帮助:虽然 MSDN 是最好的文档资源,但 MFC 部分感觉像是处于维护模式,不像 Windows API 文档那样受到关注.如果您发现 MFC 文档缺少查找相应的 Windows API 文档,例如 <代码>CWnd::OnNcDestroy 与 WM_NCDESTROY.后者包含有关窗口接收此消息的顺序的有价值信息.

Finding help: While the MSDN is the best resource for documentation, the MFC section feels like it is in maintenance mode and doesn't get as much attention as the Windows API documentation. If you find the MFC documentation lacking look up the respective Windows API documentation instead, for example CWnd::OnNcDestroy vs. WM_NCDESTROY. The latter contains valuable information on the order in which windows receive this message.

学习 MFC

学习 MFC 的综合资源是 Jeff Prosise 的 Programming Windows使用 MFC.虽然它已经过时(1999 年发布),但这些概念在今天仍然有效.它概述了 MFC 的概念并深入探讨了实现细节.如果您发现自己在生成的(预处理器)代码方面遇到困难,这本书就是为您准备的.

A comprehensive resource for learning MFC is Jeff Prosise' Programming Windows with MFC. While it is dated (released in 1999) the concepts are still valid today. It provides an overview of the concepts of MFC and goes deep into the implementation details. If you find yourself struggling with the generated (preprocessor) code this book is for you.

作为一个有价值的在线资源,MSDN 提供了有关 MFC 开发的几乎所有方面的信息.主要概念包括:

As a valuable online resource the MSDN offers information on just about any aspect of MFC development. The major concepts include:

  • 消息处理和映射
  • 用户界面元素
    • Windows
    • 对话框

    完整列表可在概念中找到.

相关文章