Why you should learn the API before MFC

<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">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. </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">The standard arguments are: </span></span></p>
<ul style="margin-top: 0cm;" type="disc">
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">API is too hard </span></span></li>
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">MFC is too confusing </span></span></li>
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">API is too much code </span></span></li>
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">MFC is bloated </span></span></li>
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">API doesn't have wizards </span></span></li>
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">MFC is badly designed </span></span></li>
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">API isn't Object Oriented </span></span></li>
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">MFC kicked my dog </span></span></li>
<li class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">API stole my girlfriend </span></span></li>
</ul>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">And so on... </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><strong><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">My Answer</span></span></strong></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">My opinion, although by no means the only one, is that you should use the right framework for the right job. </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">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 <em>Class Library</em>, 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. </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">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. </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">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. 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. </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">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. </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">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. </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><strong><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">So basically...</span></span></strong></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-size: 12.5pt;" lang="EN-US"><span style="font-family: Calibri;">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. </span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: Calibri;"><strong><span style="font-size: 12.5pt;" lang="EN-US">However, and this is important</span></strong><span style="font-size: 12.5pt;" lang="EN-US">... 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. </span></span></p>
<p><span style="" lang="EN-US">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. </span></p>

你可能感兴趣的:(BEFORE)