📄 ch05.htm
字号:
All of the controls (the toolbar button and the menu items) can be enabled or disabledat one time using the TActionList component.</P><P><B>Components on the Additional Tab  </B>Delphi has another group ofcomponents that I'll throw in with the standard controls. These controls can be foundunder the Additional tab on the Component palette. The classes representing thesecomponents include TBitBtn, TSpeedButton, TMaskEdit, TStringGrid, TDrawGrid, TImage,TShape, TBevel, TScrollBox, TCheckListBox, TSplitter, TStaticText, and TChart. TheTBitBtn class represents a button that has an image on it.</P><P><P>TSpeedButton is also a button with an image, but this component is not a truebutton. Instead, it's a graphical depiction of a button. This enables you to havea large number of speed buttons and not consume Windows resources for each button.</P><PRE></PRE><P>The TImage component enables you to place an image on a form that can then beselected from a file on disk. You can use the TBevel component to create boxes andlines that are raised (bumps) or lowered (dips). Bevels can be used to divide a forminto visual regions and to provide an aesthetically pleasing form. The TStringGridand TDrawGrid classes give you a means to present information in a grid format.</P><P><H4>Win32 Custom Control Classes</H4><P>VCL has component classes that encapsulate many of the Windows 32-bit custom controls.These classes include TListView, TTreeView, TTrackBar, TProgressBar, TTabControl,TPageControl, TRichEdit, TImageList, TStatusBar, TAnimate, TDateTimePicker, TToolBar,TCoolBar, and a few others. Some of these controls are, by nature, complicated, andthe VCL classes that represent them are complicated as well. Trust me when I saythat VCL does much to ease the burden of working with these common controls. Youhave to spend some time with these classes before you fully understand them. I coverthe TToolBar, TCoolBar, TImageList, and TStatusBar components on Day 13, "Beyondthe Basics."</P><P><H4>Database Component Classes</H4><P>VCL has a host of database components that include both visual and nonvisual classes.Nonvisual database components include TDataSource, TDatabase, TTable, and TQuery.These classes encapsulate behind-the-scenes database operations.</P><P>Visual database component classes are the part of the VCL database operationsthat users see and interact with. For instance, a TDBGrid component is used to displaya database table in grid format. In this way, the TDBGrid acts as the interface betweenthe user and the database. Through the TDBGrid, the user can view and edit the databasetable on disk.</P><P>The TDBNavigator component provides buttons that enable the user to move througha database table. This class includes buttons for next record, previous record, firstrecord, last record, cancel edit, accept edit, and undo edit.</P><P>Other data-aware component classes hook standard Windows controls to databasefields. These classes include TDBText, TDBEdit, TDBListBox, and TDBImage, among others.</P><P>A group of components usually associated with database programming is the QuickReportcomponents. This group of components makes report writing easy, especially if yourdata source is a database. The database QuickReport components are discussed on Days16, 17, and 18.</P><P><H4>Common Dialog Classes</H4><P>As you are no doubt aware, Windows has common dialog boxes for things like openingfiles, saving files, choosing fonts, and choosing colors. VCL encapsulates thesecommon dialog boxes in classes representing each type. The classes are TOpenDialog,TSaveDialog, TOpenPictureDialog, TSavePictureDialog, TFontDialog, TColorDialog, TPrintDialog,and TPrinterSetupDialog. VCL also adds the TFindDialog and TReplaceDialog classesto this component group. All the components in this group are nonvisual in that theydon't have a design-time visual interface. The dialog boxes are visible when displayedat runtime, of course.</P><P><H4>System Component Classes</H4><PRE>The System tab on the Component palette contains a mixture of visual and nonvisual components. The TTimer class is used to represent a Windows system timer. Its single event is OnTimer, which is called each time the timer fires. The timer interval is set through the Interval property. TTimer is a nonvisual component.</PRE><P>Tucked into this group of classes is the TMediaPlayer class. This class enablesyou to play media files like wave audio, AVI video, and MIDI audio. The media canbe played, stopped, paused, or positioned at a particular point in the file, as wellas many other operations. This class has many properties and events that greatlysimplify the complex world of the Windows Media Control Interface (MCI).</P><P>The TPaintBox component gives you an empty canvas on which you can draw anythingyou like. This component has many potential uses. The System group includes OLE anddynamic data exchange (DDE) classes as well.</P><P><H4>The Win 3.1 Group of Components</H4><P>Don't make the mistake of automatically discarding this component group just becauseof the name of the tab on which it resides. This group contains some great components.(The Win 3.1 tab has its roots in Delphi 1.) In particular, I like the TTabSet andTNotebook components. This group also includes several component classes that enableyou to build your own custom File Open or File Save dialog box. The classes are TFileListBox,TDirectoryListBox, TDriveComboBox, and TFilterComboBox.</P><P><H4>Internet Components</H4><P>Depending on which version of Delphi you have (Standard, Professional, or Client/Server),you might have an Internet tab. This tab contains components used in Internet programming.The components include HTML, FTP, SMTP, POP3, and HTTP components. It also containscomponents that are for general network programming via the Winsock API. Most ofthese components are native VCL components, although at least one, the THTML component,is an ActiveX control.</P><P><H4>Sample Components</H4><P>The Samples tab contains components that can be used to gain an understandingof how to write components. The source for these components is provided so that youcan see how they work. The sample components include TGauge, TColorButton, TSpinButton,TSpinEdit, TDirectoryOutline, and TCalendar.</P><P><H4>ActiveX Controls</H4><PRE>The ActiveX tab contains ActiveX controls that you can use in your applications. These controls include Chart FX by Software FX, Inc., Visual Speller by Visual Components, Inc., Formula One Spreadsheet, Formula One VtChart, and a Graph control by Bits Per Second, Ltd.</PRE><H4>GDI Classes</H4><P>The GDI (graphics device interface) classes get a lot of work in Windows GUI applications.These classes encapsulate the use of bitmaps, fonts, device contexts (DCs), brushes,and pens. It is through these GDI objects that graphics and text are displayed ona window. The GDI classes are not associated with a specific component, but manycomponents have instances of these classes as properties. For example, an edit controlhas a property called Font that is an instance of the TFont class.</P><P>The term <I>device context</I> is well known by traditional Windows programmers.In VCL, though, the term is not widely used. This is because VCL encapsulates WindowsDCs in the TCanvas class. VCL uses the term <I>canvas</I> to refer to a Windows devicecontext. A canvas provides a surface that you can draw on using methods like MoveTo,LineTo, and TextOut. Bitmaps can be displayed on the canvas using the Draw or StretchDrawmethods. The concept of a canvas that you draw on makes more sense than the archaicterm <I>device</I> <I>context</I>, don't you think? Graphics operations are discussedon Day 12, "Graphics and Multimedia Programming." The following is a listof the more common GDI classes used:</P><UL> <LI>The TCanvas class contains instances of the other GDI classes. For example, when you do a MoveTo/LineTo sequence, a line is drawn with the current pen color. The Pen property is used to determine the current pen color and is an instance of the TPen class. TPen has properties that determine what type of line to draw: the line width, the line style (solid, dashed, dotted, and so on), and the mode with which to draw the line. <P> <LI>The TBrush class represents a brush used as the fill pattern for canvas operations like FillRect, Polygon, and Ellipse. TBrush properties include Color, Style, and Bitmap. The Style property enables you to set a hatch pattern for the brush. The Bitmap property enables you to specify a bitmap for the fill pattern. <P> <LI>TBitmap encapsulates bitmap operations in VCL. Properties include Palette, Height, Width, and TransparentColor. Methods include LoadFromFile, LoadFromResourceID, and SaveToFile. TBitmap is used by other component classes such as TImage, TBitBtn, and TSpeedButton in addition to TCanvas. An instance of the TBitmap class can also be used as an offscreen bitmap. Offscreen bitmaps are commonly used in graphics-intensive applications to reduce flicker and improve graphics performance. <P> <LI>The TFont class handles font operations. Properties include Color, Height, and Style (bold, italic, normal, and so on). The TFont class is used by all component classes that display text.</UL><P>In addition to the GDI classes listed here, there are others that either workas helper classes or extend a base class to provide extra functionality. As you workwith Delphi, you will learn more about these classes and how to use them. Figure5.6 shows the hierarchy of the VCL classes that encapsulate GDI operations.</P><P><A HREF="javascript:popUp('28670506.gif')"><B>FIGURE 5.6.</B></A><B> </B><I>VCLGDI class hierarchy.</I></P><P><H4>Utility Classes</H4><P>VCL contains many utility classes that you can use in your applications. A <I>utility</I><I>class</I> simplifies some task in Windows programming. For instance, the TIniFileclass eases the use of writing and reading Windows configuration files (.INI files).Conventional wisdom has it that the use of .INI files is out and the Registry isin. To aid in Registry operations, VCL has the TRegistry and TRegkeyInfo classes.</P><P>The TStringList class enables arrays of strings. TStringList is used by many ofthe component classes to store strings. For instance, the TMemo class uses a TStringListobject for its Lines property. TStringList has the capability to save its list ofstrings to file or load strings from a file using the LoadFromFile and SaveToFilemethods. TStringList can also be used to read and write text files.</P><P>Another useful VCL utility class is the TList class. This class enables you tocreate arrays of any type of object you want. The TList class simply stores a listof pointers. The main advantage of the TList class is that it provides you with anarray that will dynamically grow or shrink as new objects are added or removed.</P><P>VCL also includes a set of classes to enable reading and writing of streams (astream is really just a block of data). The TStream, TFileStream, TMemoryStream,and TResourceStream classes all enable you to read or write data to streams. TStreamis the base class for all stream classes. The TFileStream class is used when dealingwith files on disk, TMemoryStream is used to manipulate data in memory, and TResourceStreamis used to load binary resources from EXEs and DLLs. These classes are for more advanceduses, but they are invaluable when you need the particular functionality they provide.For more information on these classes, see the Delphi VCL Help.</P><P><H3><A NAME="Heading11"></A>And That's Not All . . .</H3><P>By no means did I cover all the VCL classes here. I did, however, touch on thoseclasses that you are most likely to use in your applications.</P><P>Flip back a few pages and take another look at Listing 5.1. I showed you how easyit was to load and display a bitmap with Delphi. Let's do an exercise that provesthe point. First, begin a new application. You will be looking at a blank form. Performthe following steps:</P><DL> <DD><B>1. </B>Change the Caption property of the form to Bitmap Test Program. <DT></DT> <DD><B>2. </B>Click on the Additional tab on the Component palette, choose the Image component, and place the component on the form. <P> <DT></DT> <DD><B>3. </B>Locate the Align property and change it to alClient. The picture component fills the client area of the form. <P> <DT></DT> <DD><B>4. </B>Locate the Stretch property and change it to True. <P> <DT></DT> <DD><B>5. </B>Locate the Picture property and double-click the Value column. <P> <DT></DT> <DD><B>6. </B>The Picture Editor dialog box is displayed. Click the Load button. The File Open dialog box is displayed. <P> <DT></DT> <DD><B>7. </B>Navigate to the \Program Files\Common Files\Borland Shared Files\Images\Splash\256Color directory and choose an image from those presented (I like HANDSHAKE.BMP). Click OK. <P> <DT></DT> <DD><B>8. </B>You are now back to the Image Editor dialog box, and the bitmap you chose is displayed in the preview window. Click OK. (If you want to choose a different bitmap, click the Load button again.) The bitmap now fills the client area of the form. <P> <DT></DT> <DD><B>9. </B>Click the Run button. When the application runs, you can size the window and the bitmap will always fill the client area of the window. <P></DL><P>See how easy it is? It would have been even easier if you hadn't bothered to makethe image fill the form's client area. Figure 5.7 shows the bitmap test program running.</P><P><A HREF="javascript:popUp('28670507.gif')"><B>FIGURE 5.7.</B></A><B> </B><I>Thebitmap test program running.</I><H2><A NAME="Heading12"></A>Summary</H2><P>Today you learned about frameworks and how VCL fits into the framework scene.I discussed properties, methods, and events and gave you some hands-on experiencein the process. You finished today with an overview of the VCL classes that you arelikely to encounter when programming in Delphi. I didn't cover them all, but I gaveyou a brief look at the most commonly used classes.</P><P>So where is this industry going? The wave of the future appears to be components.The great thing about VCL is that if you decide (or are forced) to switch to C++,you can still use VCL. You might be surprised to learn that the very same VCL thatis used in Delphi is also used in Borland C++Builder. Any time you spend learningVCL can be immediately applied to C++ if the time ever comes. It's the same VCL,so there's nothing to relearn.</P><P><H2><A NAME="Heading13"></A>Workshop</H2><P>The Workshop contains quiz questions to help you solidify your understanding ofthe material covered and exercises to provide you with experience in using what youhave learned. You can find the answers to quiz questions in Appendix A, "Answersto the Quiz Questions."</P><P><H3><A NAME="Heading14"></A>Q&A</H3><DL> <DT></DT> <DD><B>Q What is a framework?</B> <P> <DT></DT> <DD><B>A</B> A framework, also called a class library, is a set of classes that simplifies Windows programming. A good framework implements object-oriented design and object-oriented programming to apply an object-oriented approach to writing Windows applications. <P> <DT></DT> <DD><B>Q Is VCL a framework?</B> <P> <DD><B>A</B> Yes, VCL is a framework. VCL is written in Object Pascal and works with both Delphi and C++Builder. <DT></DT> <DD><B>Q It seems as though the component way of doing t
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -