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

📄 ch05.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<PRE></PRE><BLOCKQUOTE>	<P>Some weekend when the leaves are raked, the house trim is painted, the laundry	is done, the kids are at Grandma's, and you think you have a good handle on Delphi,	spend some time browsing the VCL source code. (Delphi Professional and Client/Server	ship with the VCL source code.) It can be intimidating at first, but after a while	you'll see what the designers were doing. Don't strain yourself. Attempt to understand	the things that bump up against the limits of your knowledge regarding Object Pascal.	Leave the complicated stuff for later.<BR>	But notice how the VCL designers use private, protected, and public access in classes.	Notice how things that should be kept hidden from the user aren't in public view.	Studying the VCL source can teach you a great deal about Object Pascal and object-oriented	design. <HR></BLOCKQUOTE><H2><A NAME="Heading4"></A>The Visual Component Library</H2><P>You have probably noticed that this book contains &quot;Delphi 4&quot; in itstitle. Obviously Delphi has been around a while. When Delphi 1 was introduced in1995, it was an instant hit. Delphi offered rapid application development (RAD) usingsomething called <I>components</I>. Components are objects that can be dropped ona form and manipulated via properties, methods, and events. It's visual programming,if you will.</P><P>The concept of form-based programming was first popularized by Microsoft's VisualBasic. Unlike Visual Basic, though, Delphi used a derivative of Pascal as its programminglanguage. This new language, called Object Pascal, introduced OOP to the Pascal language.Delphi and Object Pascal represented the marriage of object-oriented programmingand form-based programming. In addition, Delphi could produce standalone executables.Real programs. Programs that did not require a runtime DLL to run; programs thatwere compiled, not interpreted; programs that ran tens of times faster than VisualBasic programs. The programming world was impressed.</P><P>Delphi didn't just throw Object Pascal at you and let you flounder. It also introducedthe Visual Component Library. As I have said, VCL is an application framework forWindows programming in Object Pascal. The most noticeable feature of VCL is thatit was designed around the concept of properties, methods, and events--the visualcomponent model. Let's look at the visual component model in more detail.</P><P><H3><A NAME="Heading5"></A>Components</H3><P>As I talked about on Day 1, &quot;Getting Started with Delphi,&quot; VCL componentsare objects that perform a specific programming task. VCL components are wrappedup in Object Pascal classes. From now on in this book, you will be encountering componentson a daily basis. I won't spend a lot of time explaining every detail of componentsright now because you will see by example how they work throughout the rest of thebook. I'll explain components in more detail on Day 7, &quot;VCL Components.&quot;</P><P><H3><A NAME="Heading6"></A>Properties, Methods, and Events</H3><P>On Day 1, I gave you a brief introduction to the properties, methods, and eventsmodel. These three ingredients make up the public interface of components in VCL(the part of the component the user will see). Let's take a look at these elementsone at a time.</P><P><H3><A NAME="Heading7"></A>Properties</H3><P><I>Properties</I> are elements of a component that control how the component operates.Many components have common properties. All visual components, for example, havea Top and a Left property. These two properties control where the component willbe positioned on a form both at design time and at runtime. All components have anOwner property, which VCL uses to keep track of the child components a particularparent form or component owns.</P><P><B>Properties and the Object Inspector&#160;&#160;</B>A picture is always wortha thousand words, so let's start up Delphi again and see properties in action. Whenyou start Delphi, you are greeted with a blank form and the Object Inspector.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> If you have the Delphi options configured to save the desktop when	you close Delphi, you might see the last project you were working on when you start	Delphi. If that's the case, choose File|New Application from the main menu to get	a blank form. <HR></BLOCKQUOTE><P>The Object Inspector will look something like Figure 5.1. (When Delphi starts,it sizes the Object Inspector based on your current screen resolution, so your ObjectInspector might be taller or shorter than the one shown in Figure 5.1.) If necessary,click on the Properties tab of the Object Inspector window so that the form's propertiesare displayed. The component's properties are arranged in alphabetical order.</P><P><A HREF="javascript:popUp('28670501.gif')"><B>FIGURE 5.1.</B></A><B> </B><I>TheObject Inspector.</I></P><P>If more properties exist than can be displayed at one time, the Object Inspectordisplays a scrollbar so that you can view additional properties. The Object Inspectorwindow can be moved and sized. I like my Object Inspector as tall as my screen permitsso that I can see the maximum number of properties at one time. Scroll through theproperties until you locate the Left property and then click on it. Change the valuefor the Left property (any number between 0 and 600 will do) and press Enter on thekeyboard. Notice how the form moves as you change the value.</P><P>This illustrates an important aspect of properties: they are more than simplefields of a class. Each property has an underlying data field associated with it,but the property itself is not a class data field. Changing a property often leadsto code executed behind the scenes.</P><P><BR><strong>New Term:</strong> Properties are often tied to <I>access methods</I> that executewhen the property is modified.</P><PRE></PRE><P><B>Changing a Property's Value&#160;&#160;</B>Properties can be changed at <I>designtime</I> (when you are designing your form) and at <I>runtime</I> (when the programis running through code you write). In either case, if the property has an accessmethod, that access method will be called and executed when the property is modified.You already saw an example of changing a property at design time when you changedthe Left property and watched the form move on the screen.</P><P>That is one of the strengths of VCL and how it is used in Delphi: You can instantlysee on the screen the result of your design change. Not all properties show a visiblechange on the form at design time, however, so this doesn't happen in every case.Still, when possible, the results of the new property value are immediately displayedon the form.</P><P>To change a property at runtime, you simply make an assignment to the property.When you make an assignment, VCL works behind the scenes to call the access methodfor that property. To change the Left property at runtime, you use code like this:</P><P><PRE>Left := 200;</PRE><P>In the case of the Left property (as well as the Top property), VCL moves andrepaints the form. (For you Windows API programmers, you can figure out that thiseventually translates into calls to the Windows API functions SetWindowPos and InvalidateRect.)</P><P><B><strong>New Term:</strong> Property Access Specifiers&#160;&#160;</B>Properties havetwo <I>access specifiers</I>, which are used when properties are read or modified.There is a <I>read specifier</I> and a <I>write specifier</I>.</P><P>Suffice it to say that access specifiers associate read and write methods withthe property. When the property is read or written to, the methods associated withthe property are automatically called. When you make an assignment as in the previousexample, you are accessing the write specifier. In effect, VCL checks to see whetheran access method exists for the write specifier. If it does, the access method iscalled. If no access method exists, VCL assigns the new value to the data field associatedwith the property.</P><P>When you reference a property (use the property as the right side of an equation),you are accessing the read specifier:</P><P><PRE>X := Left;</PRE><P>In this case, VCL calls the read specifier to read the value of the Left property.In many cases the read specifier does very little more than return a property's currentvalue.</P><P><PRE><B>Property Attributes&#160;&#160;</B>The properties of the property (sorry, I couldn't resist) are determined by the writer of the component. A property can be read-only. A read-only property can be read--its value can be retrieved--but not written to. In other words, you can fetch the property's value, but you can't change it. In rare cases, a property can be made write-only (a property that can be written to but not read isn't very useful in most cases). This is obviously the opposite of a read-only property. </PRE><P>Finally, some properties can be specified runtime-only. A runtime-only propertycan be accessed only at runtime, not design time. Because a runtime-only propertydoesn't apply at design time, it is not displayed in the Object Inspector. A runtime-onlyproperty can be declared as read-only, too, which means that it can be accessed onlyat runtime and can only be read (not written to).</P><P><B>Property Types&#160;&#160;</B>Some properties use an instance of a VCL classas the underlying data field. To illustrate, let's put a memo component on our blankform:</P><DL>	<DT></DT>	<DD><B>1. </B>Go to the Delphi Component palette, choose the Standard tab, and click	the Memo button. (The tooltip will tell when you are over the Memo button.)	<P>	<DT></DT>	<DD><B>2. </B>Now move to the form and click on it where you want the memo's top-left	corner to appear. As soon as you place the memo component on the form, the Object	Inspector switches to show you the properties of the component just placed on the	form, in this case a TMemo.	<P>	<DT></DT>	<DD><B>3. </B>Locate the Lines property and click on it. Notice that the property	value contains the text (TStrings) and that there is a little button with an ellipsis	(. . .) to the right of the property value.	<P></DL><BLOCKQUOTE>	<P><HR>The ellipsis button tells you that this property can be edited by using a property	editor. For an array of strings, for instance, a dialog box will be displayed in	which you can type the strings. In the case of the Font property, clicking the ellipsis	button will invoke the Choose Font dialog box. The exact type of the property editor	is property-specific, although certain properties can share a common editor. You	can bring up the property editor by clicking the ellipsis button or by double-clicking	the property value. <HR></BLOCKQUOTE><P>The Lines property for a memo component is an instance of the TStrings class.When you double-click the Value column, the string editor is displayed and you canthen type the strings you want displayed in the memo component when the applicationruns. If you don't want any strings displayed in the memo component, you need toclear the property editor of any strings.</P><PRE></PRE><P>The Font property is another example of a property that is an instance of a VCLclass. A <I>font</I> includes things like the typeface, the color, the font size,and so on. Locate the Font property in the Object Inspector. (It doesn't matter whetheryou have selected the memo component or the form.)</P><P>Notice that there is a plus sign before the word <I>Font</I>. This tells you thatthere are individual properties within this property that can be set. If you double-clickon the property name, you will see that the Object Inspector expands the propertyto reveal its individual elements. You can now individually edit the Font property'selements. In the case of the Font property, these same settings can be edited byinvoking the property editor. You can use either method with the same results.</P><P>Some properties are sets (I talked about sets on Day 3, &quot;Classes and Object-OrientedProgramming&quot;). The Style property within the Font object is a good example ofa set. Notice that Style has a plus sign in front of it. If you double-click on theStyle property, you will see that the Style node expands to reveal the set's contents.In this case, the set consists of the various styles available for fonts: bold, italic,underline, and strikeout. By double-clicking a style, you can turn that style onor off. A set can be empty or can contain one or more of the allowed values.</P><P><strong>New Term:</strong> Some properties can be <I>enumerations</I>, a list of possiblechoices.</P><P>An <I>enumeration</I> is a list of possible choices for a property. When you clickon an enumeration property, a drop-down arrow button appears to the right of thevalue. To see the choices in the enumeration, click the drop-down button to displaythe list of choices. Alternatively, you can double-click the Value column for theproperty. As you double-click on the property's value, the Object Inspector willcycle through (or enumerate) the choices. The Cursor property gives a good exampleof an enumerated property. Locate the Cursor property and click the arrow buttonto expose the list of possible cursors to choose from.</P><P>Enumerations and sets differ in that with an enumeration property, only one ofthe presented choices can be selected (only one cursor can be in effect at any time).The set can contain none or any number of the choices (a font style can contain bold,underline, italic, or none of these).</P><P>As long as you have Delphi running and a blank form displayed, you might as wellspend some time examining the various components and their properties. Go ahead,I'll wait.</P><BLOCKQUOTE>	<P><HR><B>HOUSE RULES: PROPERTIES</B></BLOCKQUOTE><UL>	<LI>Properties appear to be class fields and are accessed like class fields.	<P>	<LI>Properties are <I>not</I> class fields. They are a special category of class	member.	<P>	<LI>Properties often invoke an access method when they are written to (assigned a	value), but not always. It depends on how the particular component is written.	<P>	<LI>Published properties usually have default values. The default value is the value	that initially shows up in the Object Inspector when a component is first utilized	and is the value that will be used if no specific value is assigned.	<P>	<LI>Properties can be designed as read/write, read-only, or write-only.	<P>	<LI>Runtime-only properties don't show up in the Object Inspector and can be modified	only at runtime.</UL><PRE></PRE>

⌨️ 快捷键说明

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