`
buliedian
  • 浏览: 1234620 次
  • 性别: Icon_minigender_2
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

为什么学习MFC前先学习API?(略译)

阅读更多

Why you should learn the API before MFC
为什么学习MFC前先学习API?


原文:http://www.winprog.org/tutorial/apivsmfc.html
刘建文略译 (http://blog.csdn.net/keminlau

The Controversy争论

Too many people come on to IRC and ask "What is better, MFC or API?" and too many people are willing to say "MFC sucks" or "API sucks" either because of traumatic events involving one or the other in early childhood, or because everyone else is saying it.

有很多人都问一个问题:MFC和API都底哪个更好?而有更多的回复是“MFC太烂了”或者“API太烂了”。偏袒的原因多半是因为他们使用二者有过不好的第一印象,心理上“受伤”所留下了阴影;或者只是听到别人的评价,人云亦云。

The standard arguments are:典型的争论点有如下一些:
* API is too hard
* MFC is too confusing
* API is too much code
* MFC is bloated
* API doesn't have wizards
* MFC is badly designed
* API isn't Object Oriented
* MFC kicked my dog
* API stole my girlfriend

And so on...


My Answer我的答案

My opinion, although by no means the only one, is that you should use the right framework for the right job.

我的观点只有一个:那就是为你的工作需要选择合适的框架(即API或MFC)。

First of all a clarification on what the API and MFC are. API is a generic term meaning Application Programming Interface, however in the context of Windows programming, it means specifically the Windows API, which is the lowest level of interaction between applications and the windows operating system. Drivers of course have even lower levels, and different sets of function calls to work with, but for the vast majority of windows development this is not an issue. MFC is a Class Library, it's a bunch of C++ classes that have been written to reduce the amount of work it takes to do certain things with the API. It also introduces an (arguably) Object Oriented framework into the application that you can either take advantage of or ignore, which is what most beginners do since the framework isn't really aimed at writing MP3 players, IRC clients or games.

首先,我们澄清一下什么是 API和MFC。API是“应用编程接口”,它是应用程序与操作系统交互的边界,是相当的“底层”的东西。比API更“底层”当然数驱动程序了,它直接与硬件交互。MFC是一个C++类库,目的是为了降低用API写特定功能的代码量。MFC同时也为应用开发带来了一种面向对象框架,你可以决定是否使用 OO。很多初学者使用OO,因为框架的初衷并不是写MP3播放器、IRC客户端或游戏。

Every program, whether it is written with MFC, Delphi, Visual Basic, perl, or any other wacked out language or framework you can think of, is eventually built upon the API. In many cases this interaction is hidden, so you don't deal directly with the API, the runtime and support libraries do it for you. Some people ask, "MFC can do Blah Blah Blah, can the API?" The answer is that MFC can only do what the API can do, because it's built on top of it. However doing things yourself with the API may take considerably more code than using the pre-written MFC classes.

无论使用什么样的语言或框架写的程序,最终于还是建立在API之上。很多时候,与API交互是被隐藏的,所以你不必直接与API打交道,运行时或代码库会为你效劳。由于MFC是建立在API之上的,所以MFC能做的API也能,反之则不一定了。当然API的灵活性的代价是代码数量巨增。

So what is the right framework? For starters, for people that are just learning to program, I strongly believe that you should work with the API untill you are comfortable with the way windows applications work and you understand all of the basic mechanics behind things like the message loop, GDI, controls, and maybe even multithreading and sockets. This way you will understand the fundamental building blocks of all windows applications, and can apply this common knowledge to MFC, Visual Basic, or whatever other framework you choose to work with later.

因此什么叫合适的框架呢?对于初学者,他们只是学习编程的技能,我强烈建议你一开始学习API的机理,直到对window应用程序的工作原理了如指掌,包括消息循环、GDI、控件、多线程和套接字的基本机理。这种方式能够让你理解windows应用程序的基本构造块的工作原理,从而把这些通用的知识应用到 MFC、VB等种不同的框架代码中去。(kemin:基本构造块(the fundamental building blocks)是应用框架的有机元素,不同的[应用框架]使用不同的基本构造块。要做GUI开发,那么消息循环、GDI、控件、多线程等FBB是必须掌握的,无论你学什么GUI应用框架。)
(kemin:还有,从这里可以看出来,API是通用的,而MFC是特殊的实现;GUI开发的原理,或者GUI开发框架的理论是通用的;MFC、WCL等框架是特殊的实现,我们要学通用的东西。)

It's also important because these other frameworks don't support everything that the API does, simply because it does a whole lot and they can't necessarily support all of the arcane little things that most people won't use. So when you finally do need to use them you need to add it yourself, you can't rely on the framework to do it for you and if you don't understand the API this could be quite the chore.

学习API的另一个重要的原因是框架不支持所有API所支持的功能。原因很简单,框架只封装那些应用需要的常用的功能。封装的代价就是灵活性的下降。

But isn't MFC easier? In a certain sense it's easier in that many common tasks are done for you, thus reducing the amount of code that you need to actually type. However, less code does not mean "easier" when you don't understand the code you DO need to write, or how all of the code that is there to support you actually works. Generally beginners who use the wizards to start there applications have no idea what most of the generated code does, and spend a great deal of time trying to figure out where to add things, or what changes to make to acheive a certain result. If you start your programs from scratch, either in the API or with MFC, then you know where everything is because you put it there, and you will only use features that you understand.

但是,MFC不是更简单吗?从某种意思上说,是的。MFC为你的常见开发工作提供了方便,降低了开发的所需的代码量。不过,代码少了并不意味着“简单”了。你不明白黑箱内部的具体机理,你的能力很受限制的。

Another important factor is that most people that are learing the Win32 API for the first time don't already have a strong base in C++. To try and comprehend windows programming with MFC and learn C++ at the same time can be a monumental task. Although it's not impossible, it will take you considerably longer to become productive than if you already knew either C++ or the API.

第三个从win32 api开始学习编程的重要原因是,它有助于学习C++。公认学习C++是相当困难的,但是如果带有应用性的学习则效果很不错,而且C++与API一并学习是一箭双雕。

So basically...
What it comes down to is that I think you should learn the API untill you feel comfortable with it, and then try out MFC. If it seems like it's making sense to you and saving you time, then by all means use it.

结论是,先学习API直到相当的熟悉然后再学MFC。

However, and this is important... if you work with MFC without understanding the API and then ask for help with something, and the answer you get is stated using the api (such as "Use the HDC provided in the WM_CTLCOLORSTATIC message") and you say "huh?" because you don't know how to translate an API subject into MFC on your own, then you are in trouble and people will get frustrated with you for not learning what you need to know before you try and use MFC.

I personally prefer to work with the API, it just suits me better, but if I were to write a database frontend, or a host for a set of ActiveX controls I would seriously consider using MFC, as it would eliminate a lot of code that I would need to reinvent otherwise.

我个人更倾向于使用API,因为它更适合我。不过,如果我要写一个数据前端或者一个ActiveX控件的宿主程序,我毫不犹豫地选择使用MFC。

分享到:
评论

相关推荐

    MFC中文API手册(一怀去意修正).zip

    MFC是微软为Windows平台开发的C++类库,它封装了Windows API,使得开发者可以使用面向对象的方式来编写Windows应用程序。这份手册的出现,为学习和使用MFC的程序员提供了方便,特别是对于中文阅读习惯的开发者,它...

    学习MFC的一些很不错的资料

    MFC将Windows API封装为易于使用的C++类,如CWinApp、CWnd、CDialog等,提供了面向对象的编程接口。这份资料会详细解释这些类的功能、用法和相互关系,帮助你构建MFC应用的框架。比如,CWinApp类代表应用程序本身,...

    MFC实现API侦测

    在编程领域,API(应用程序接口)是软件系统之间交互的核心工具。API侦测,也称为API钩子或API监控,是一种技术,...通过这个项目,开发者可以学习到如何在MFC环境下集成API监控功能,提升对系统行为的理解和控制能力。

    MFC_官方API文档.zip

    学习MFC官方API文档时,开发者应重点关注各个类的功能、成员函数的使用、以及如何通过消息映射和事件处理来构建应用程序。同时,结合实际项目实践,不断熟悉并掌握MFC的各种特性和技巧,才能真正提升Windows应用程序...

    C++MFC学习资料整合大全

    1. **MFC基础**:MFC是微软为Windows应用程序设计的面向对象框架,它封装了Windows API,使得开发者可以通过面向对象的方式进行编程。MFC的核心是CWinApp类,它是每个MFC应用程序的基础,负责初始化和管理应用程序的...

    【解惑】API.SDK和MFC的对照学习

    通过这样的对照学习,可以更深入地理解Windows编程的内部机制,从MFC的封装抽象回归到更底层的API调用,从而在遇到需要精细控制的场景时能够更自如地应用。不过,一般来说,除非有特殊需求,否则在大多数情况下,...

    MFC中文API下载

    这个“MFC中文API下载”很可能是一个包含MFC类库详细解释的CHM(Compiled Help Manual)文件,便于开发者查找和理解MFC的相关API。 MFC是基于面向对象编程的,它封装了Windows API,提供了丰富的类来处理窗口、菜单...

    MFC中学习API函数的秘籍

    API函数大全,对学习MFC非常有帮助,还在MFC中挣扎的朋友快快来下载

    MFC API中文函数

    **正文** MFC(Microsoft Foundation Class)库是微软提供的一套C++类库,它基于Windows ...ts005080.pdf文件很可能是MFC API函数的中文参考手册,其中应包含详细的函数解释和实例,是学习和查阅MFC API的宝贵资源。

    MFC API方式串口程序

    本篇文章将深入探讨如何利用MFC API方式来实现串口程序,并以VC++6.0为开发环境,介绍一个串口调试助手的源代码。 首先,我们需要理解MFC中的CSerialPort类。这是MFC对串口通信的一种封装,它提供了打开、关闭串口...

    MFC 官方文档API

    MSDN(Microsoft Developer Network)是微软提供的开发人员资源库,包含了详细的API文档和技术支持,是学习和使用MFC的重要参考。 MFC官方文档API是MSDN中针对MFC框架的部分,提供了详尽的函数、类、宏和全局函数的...

    MFC类和API

    MFC类和API之间的关系是核心概念,理解和掌握它们对于Windows平台的软件开发至关重要。 MFC的核心在于它的类库,这些类是对Windows API进行封装的结果。通过使用MFC,开发者可以避免直接与底层的API函数打交道,而...

    mfc类库详解 Win32API大全(全中文版)

    学习MFC与Win32 API的结合使用,可以让你: - 掌握Windows应用程序开发的基本原理和技巧。 - 学会如何利用面向对象编程思想提高代码的可读性和可维护性。 - 熟悉Windows系统内部的工作机制,如消息队列、窗口过程等...

    MFC win32 api中文助手

    **MFC Win32 API中文助手**是一款专为C++开发者设计的辅助工具,它集成了C/C++标准库、MFC(Microsoft Foundation Classes)框架和Win32 API的中文文档,极大地方便了中文环境下的编程工作。MFC是微软为Windows平台...

    VC MFC调用百度地图API

    本篇文章将深入探讨如何在MFC项目中集成并使用百度地图API。 首先,了解百度地图API的基本概念是至关重要的。百度地图API是一组基于HTTP协议的RESTful接口,开发者可以通过发送HTTP请求来获取地图相关的数据和服务...

    快速学习MFC教程!

    ### 快速学习MFC教程的关键知识点 #### MFC与Visual C++ - **MFC简介**:Microsoft Foundation Classes(MFC)是微软提供的一套基于C++的类库,用于简化Windows应用程序的开发过程。MFC封装了Windows API,使得...

    MFC.rar_mfc api

    文档"mfc类库.doc"可能包含了关于MFC类的详细说明和使用示例,而"API函数大全.txt"则提供了Windows API的详细参考,两者结合学习,对于理解和使用MFC以及底层Windows编程非常有帮助。 通过深入学习MFC,开发者可以...

    API-PORT.rar_MFC API 串口_mfc串口 API_串口 mfc api

    在Windows编程环境中,MFC(Microsoft Foundation Classes)是C++的一个库,为开发者提供了封装Windows API的类。本文将深入探讨MFC API在串口通信中的应用。 标题“API-PORT.rar_MFC API 串口_mfc串口 API_串口 ...

    MFC API socket通讯源码

    总结来说,这个“MFC API socket通讯源码”实例提供了学习和理解网络编程基础的好机会,包括如何使用MFC封装的socket API创建服务器和客户端,以及如何进行数据的发送和接收。通过分析和理解这些源码,开发者可以更...

Global site tag (gtag.js) - Google Analytics