⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch05.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//-->                                                                </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css">	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">	<TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 5 -- The Visual Component Model</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch04/ch04.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch06/ch06.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1>- 5 -</H1></CENTER><CENTER><H1>The Visual Component Model</H1></CENTER><UL>	<LI><A HREF="#Heading1">Frameworks Fundamentals</A>	<LI><A HREF="#Heading2">So Why Should I Care About Frameworks?</A>	<UL>		<LI><A HREF="#Heading3">So What's the Catch?</A>	</UL>	<LI><A HREF="#Heading4">The Visual Component Library</A>	<UL>		<LI><A HREF="#Heading5">Components</A>		<LI><A HREF="#Heading6">Properties, Methods, and Events</A>		<LI><A HREF="#Heading7">Properties</A>	</UL>	<LI><A HREF="#Heading8">VCL Explored</A>	<UL>		<LI><A HREF="#Heading9">Form and Application Classes</A>		<LI><A HREF="#Heading10">Component Classes</A>		<LI><A HREF="#Heading11">And That's Not All . . .</A>	</UL>	<LI><A HREF="#Heading12">Summary</A>	<LI><A HREF="#Heading13">Workshop</A>	<UL>		<LI><A HREF="#Heading14">Q&amp;A</A>		<LI><A HREF="#Heading15">Quiz</A>		<LI><A HREF="#Heading16">Exercises</A>	</UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>Today I am going to talk about the visual component model and Borland's VisualComponent Library (VCL). Before getting into that, though, I'll talk a little aboutclass frameworks. In this chapter you will find</P><UL>	<LI>The fundamentals of frameworks	<P>	<LI>The Visual Component Library overview	<P>	<LI>VCL classes, including the form, application, and component classes</UL><H2><A NAME="Heading1"></A>Frameworks Fundamentals</H2><P>&quot;In the beginning there was C. . . .&quot; Well, not quite. As far as Windowsprogramming is concerned, though, that statement is accurate. In the beginning, thevast majority of Windows programs were written in C. In fact, the Windows ApplicationProgramming Interface (API) is just a huge collection of C functions--hundreds ofthem. There are still undoubtedly thousands of programmers out there writing Windowsprograms in C.</P><P>Somewhere along the line, the folks at Borland decided, &quot;There has got tobe an easier way.&quot; (Actually, the framework revolution might have started onseveral different fronts, but Borland was certainly a leader.) It was apparent thatWindows programming was very well suited to object-oriented programming. By creatingclasses that encapsulate common Windows programming tasks, a programmer could bemuch more productive. After a class was created to encapsulate a window's variousduties, for instance, that class could be used over and over again. The frameworkrevolution began.</P><P>But I haven't yet told you what a framework is.</P><P><strong>New Term:</strong> A <I>framework</I> is a collection of classes that simplifiesprogramming in Windows by encapsulating often-used programming techniques. Frameworksare also called <I>class libraries</I>. <I>Encapsulation</I> means taking a complexprogramming task and making it easier by providing a simplified interface.</P><P>Popular frameworks have classes that encapsulate windows, edit controls, listboxes, graphics operations, bitmaps, scrollbars, dialog boxes, and so on.</P><P><H2><A NAME="Heading2"></A>So Why Should I Care About Frameworks?</H2><P>That's a good question. The bottom line is that frameworks make Windows programmingmuch easier than it would be in straight C, in assembler, or in the original Pascallanguage (the Pascal that came prior to Object Pascal). Let me give you an example.Listing 5.1 contains a portion of a Windows program written in C++. This sectionof code loads a bitmap file from disk and displays the bitmap in the center of thescreen. None of this will make sense to you right now, but be patient.</P><P><H4>LISTING 5.1. C++ CODE TO LOAD AND DISPLAY A BITMAP.</H4><PRE>HPALETTE hPal;BITMAPFILEHEADER bfh;BITMAPINFOHEADER bih;LPBITMAPINFO lpbi = 0;HFILE hFile;DWORD nClrUsed, nSize;HDC hDC;HBITMAP hBitmap;void *bits;do {  if ((hFile = _lopen(data.FileName, OF_READ)) == HFILE_ERROR) break;  if (_hread(hFile, &amp;bfh, sizeof(bfh)) != sizeof(bfh)) break;  if (bfh.bfType != `BM') break;  if (_hread(hFile, &amp;bih, sizeof(bih)) != sizeof(bih)) break;  nClrUsed =    (bih.biClrUsed) ? bih.biClrUsed : 1 &lt;&lt; bih.biBitCount;  nSize =    sizeof(BITMAPINFOHEADER) + nClrUsed * sizeof(RGBQUAD);  lpbi = (LPBITMAPINFO) GlobalAllocPtr(GHND, nSize);  if (!lpbi) break;  MoveMemory(lpbi, &amp;bih, sizeof(bih));  nSize = nClrUsed * sizeof(RGBQUAD);  if (_hread(hFile, &amp;lpbi-&gt;bmiColors, nSize) != nSize) break;  if (_llseek(hFile, bfh.bfOffBits, 0) == HFILE_ERROR) break;  nSize = bfh.bfSize-bfh.bfOffBits;  if ((bits = GlobalAllocPtr(GHND, nSize)) == NULL) break;  if (_hread(hFile, bits, nSize) != nSize) break;  hDC = GetDC(hWnd);  hBitmap = CreateDIBitmap(hDC, &amp;(lpbi-&gt;bmiHeader), CBM_INIT,                          bits, lpbi, DIB_RGB_COLORS);  if (hBitmap) {    LPLOGPALETTE lppal;    DWORD nsize = sizeof(LOGPALETTE)      + (nClrUsed-1) * sizeof(PALETTEENTRY);    lppal = (LPLOGPALETTE)  GlobalAllocPtr(GHND, nSize);    if (lppal) {      lppal-&gt;palVersion = 0x0300;      lppal-&gt;palNumEntries = (WORD) nClrUsed;      MoveMemory(lppal-&gt;palPalEntry, lpbi-&gt;bmiColors,      nClrUsed * sizeof(PALETTEENTRY));      hPal = CreatePalette(lppal);      (void) GlobalFreePtr(lppal);    }   } }  while(FALSE);if (hFile != HFILE_ERROR) _lclose(hFile);HPALETTE oldPal = SelectPalette(hDC, hPal, FALSE);RealizePalette(hDC);HDC hMemDC = CreateCompatibleDC(hDC);HBITMAP oldBitmap =(HBITMAP)SelectObject(hMemDC, hBitmap);BitBlt(hDC, 0, 0, (WORD)bih.biWidth, (WORD)bih.biHeight,  hMemDC, 0, 0, SRCCOPY);SelectObject(hMemDC, oldBitmap);DeleteDC(hMemDC);SelectPalette(hDC, oldPal, FALSE);ReleaseDC(hWnd, hDC);if (bits) (void) GlobalFreePtr(bits);</PRE><PRE>if (lpbi) (void) GlobalFreePtr(lpbi);</PRE><P>That looks just a little intimidating, doesn't it? Now look at the equivalentusing Borland's VCL:</P><P><PRE>Image.LoadFromFile(`winnt.bmp');</PRE><P>So which would you rather use? You don't even have to know what these code snippetsdo to make that decision. It's easy to see that the VCL version is shorter (justa bit!) and more readable.</P><P>These examples sum up what frameworks are all about. Frameworks hide details fromyou that you don't need to know. Everything that is contained in Listing 5.1 is performedbehind the scenes in the VCL code (albeit in Pascal rather than in C++). You don'tneed to know every detail about what goes on behind the scenes when VCL does itsjob, and you probably don't want to know. All you want is to take the objects thatmake up a framework and put them to use in your programs.</P><P>A good framework takes full advantage of OOP, and some do that better than others.Borland's Object Windows Library (which came in both C++ and Pascal versions) andVisual Component Library are excellent examples of object-oriented programming. Theyprovide the proper abstraction needed for you to rise above the clutter and get downto the serious business of programming.</P><P><H3><A NAME="Heading3"></A>So What's the Catch?</H3><P>A little skeptical, are you? Good. You're bright enough to figure out that ifyou have all that ease of use, you must be giving up something. Truth is, you areright. You might think that a program written with a framework would be larger andslower than its counterpart written in a low-level language. That's partially correct.Applications written with frameworks don't necessarily have to be slower than thoseother programs, though. There is some additional overhead inherent in an object-orientedlanguage, certainly, but for the most part, it is not noticeable in a typical Windowsprogram.</P><P>The primary trade-off is that Windows programs written in Delphi tend to be largerthan programs written in languages such as C. For example, let's say you had a simpleWindows program written in C that was 75KB. The equivalent program written with Delphimight be 250KB. That might seem like a significant difference, but this example demonstratesthe worst-case scenario. The difference in final program size between a C applicationand a Delphi application written with a framework is most noticeable in very smallprograms. As your programs increase in size and sophistication, the size differenceis much less noticeable.</P><P>One reason for the size difference is simply the difference between a procedurallanguage and an object-oriented language. Object-oriented languages (C++ and ObjectPascal, for example) carry additional overhead for features such as exception handling,runtime type information (RTTI), and other OOP goodies. In my opinion, the differencein code size is an acceptable trade-off for the features that Object Pascal provides.</P><P>Now, before you label me as a code-bloat proponent, let me say that I am as conscientiousas the next person when it comes to code bloat. I believe that we should all writethe tightest code we can given the tools we use. I am also a realist, and I understandthat time-to-market is a driving force in the software industry today. I am willingto trade some code size for the power that Object Pascal and VCL give me. Let meput it another way. I'm not interested in spending a month to write a Windows programthat compiles to a 100KB executable when I can accomplish the same thing in Delphiin two days and end up with a 400KB executable. The size of the resulting executablesis insignificant when compared to the development time saved.</P><BLOCKQUOTE>	<P><HR><B>FRAMEWORKS TEACH OBJECT-ORIENTED PROGRAMMING AND DESIGN</B></P>	<P>If you end up getting serious about this crazy game called Windows programming,	you will eventually end up peeking into the source code of your favorite framework.	Sooner or later you'll want to know how the pros do things. The VCL source code is	a great place to go for that kind of information.</BLOCKQUOTE>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -