📄 ch05.htm
字号:
<UL> <LI>Properties can include <P> <LI>Simple data types <P> <LI>Strings <P> <LI>Arrays <P> <LI>Sets <P> <LI>Enumerations <P> <LI>VCL class objects</UL><H4>Methods</H4><PRE>I discussed methods on Day 3, "Classes and Object-Oriented Programming," so I don't need to cover them again here in any great detail. <I>Methods</I> in VCL components are functions and procedures that can be called to make the component perform certain actions. For example, all visual components have a method called Show, which displays the component, and a method called Hide, which hides the component. For example:</PRE><PRE>MyWindow.Show;{ do some stuff, then later... } MyWindow.Hide;</PRE><P>Methods in VCL can be declared as public, protected, or private. Public methodscan be accessed by the component's users. In this example, both the Show and Hidemethods are public. Protected methods cannot be accessed by the component users butcan be accessed by classes (components) derived from a component. Of course, privatemethods can be accessed only within a class itself.</P><P>Some methods take parameters and return values, others don't. It depends entirelyon how the method was written by the component writer. For example, the GetTextBufmethod retrieves the text of a TEdit component. This method can be used to get thetext from an edit control as follows:</P><P><PRE>var Buff : array [0..255] of Char; NumChars : Integer;begin NumChars := EditControl.GetTextBuf(Buff, SizeOf(Buff));end;</PRE><P>As you can see, this particular method takes two parameters and returns an integer.When this method is called, the edit control contents are placed in the variableBuff and the return value is the number of characters retrieved from the edit control.</P><P>For now, that's all you need to know in order to use methods. I'll discuss themin more detail on Day 20, "Creating Components."</P><P>House Rules: Methods</P><UL> <LI>Methods can be private, protected, or public. <P> <LI>Methods are called using the dot operator. <P> <LI>Methods can take parameters and can return values. <P> <LI>Some methods take no parameters and return no values. <P> <LI>Only public methods can be called by component users.</UL><H4>Events</H4><P><strong>New Term:</strong> Windows is said to be an <I>event-driven</I> environment.Event-driven means that a program is driven by events that occur within the Windowsenvironment. Events include mouse movements, mouse clicks, and key presses.</P><P>Programmers moving from DOS or mainframe programming environments might have somedifficulty with the concept of something being event-driven. A Windows program continuallypolls Windows for events. Events in Windows include a menu being activated, a buttonbeing clicked, a window being moved, a window needing repainting, a window beingactivated, and so forth.</P><P>Windows notifies a program of an event by sending a Windows message. There areover 200 possible messages that Windows can send to an application. That's a lotof messages. Fortunately, you don't have to know about each and every one of themto program in Delphi; there are only a couple dozen that are used frequently.</P><P><B>VCL Events  </B>In VCL, an event is anything that occurs in the componentthat the user might need to know about. Each component is designed to respond tocertain events. Usually this means a Windows event, but it can mean other thingsas well. For example, a button component is designed to respond to a mouse click,as you would expect. But a nonvisual control, such as a database component, can respondto non-Windows events, such as the user reaching the end of the table.</P><P><B><strong>New Term:</strong> Handling Events  </B>When you respond to a componentevent, you are said to <I>handle</I> the event.</P><P><PRE>Events are handled through methods called <I>event handlers</I>. You used event handlers extensively as you worked through the first three days of the book.</PRE><P>A typical Windows program spends most of its time idle, waiting for some eventto occur. VCL makes it incredibly easy to handle events. The events that a componenthas been designed to handle are listed under the Events tab in the Object Inspectorwindow. Event names are descriptive of the event to which they respond. For instance,the event to handle a mouse click is called OnClick.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> You don't have to handle every event that a component defines. In fact, you rarely do. If you don't respond to a particular event, the event message is either discarded or handled in a default manner as described by either VCL or the component itself. You can handle any events you have an interest in and ignore the rest. <HR></BLOCKQUOTE><P>These concepts will make more sense if you put them into practice. To begin, let'sstart a new application. Choose File|New Application from the main menu. If you areprompted to save the current project, click No. Now you will again have a blank form.First, set up the main form:</P><DL> <DT></DT> <DD><B>1. </B>Change the Name property to PMEForm (PME for properties, methods, and events). <P> <DT></DT> <DD><B>2. </B>Change the Caption property to PME Test Program. <P></DL><BLOCKQUOTE> <PRE>Next, you need to add a memo component to the form:</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD><B>1. </B>Choose the Standard tab on the Component palette and click the Memo button. <P> <DT></DT> <DD><B>2. </B>Click on the form to place a memo component on the form. <P> <DT></DT> <DD><B>3. </B>Change the Name property to Memo. Be sure the memo component is selected so that you don't accidentally change the form's name instead of the memo component. <P> <DT></DT> <DD><B>4. </B>Double-click on the Lines property in the Value column. The String list editor will be displayed. <P> <DT></DT> <DD><B>5. </B>Delete the word Memo and type A test program using properties, methods, and events. Click OK to close the String list editor. <P> <DT></DT> <DD><B>6. </B>Resize the memo component so that it occupies most of the form. Leave room for a button at the bottom. <P></DL><P>Your form will now look like the form shown in Figure 5.2.</P><P><BR>Now you can place a button on the form:</P><UL> <LI><B>1. </B>Choose the Standard tab on the Component palette and click the Button component.</UL><DL> <DT></DT> <DD><B>2. </B>Click on the form below the memo component to place the button on the form. <P> <DT></DT> <DD><B>3. </B>Change the Name property for the button to Button. <P> <DT></DT> <DD><B>4. </B>Change the Caption property to Show/Hide. <P></DL><P><A HREF="javascript:popUp('28670502.gif')"><B>FIGURE 5.2.</B></A><B> </B><I>Theform with a memo component added.</I></P><P><DL> <DT><I></I></DT> <DD><B>5. </B>Center the button horizontally on the form. <P></DL><BLOCKQUOTE> <P><HR><strong>TIP:</strong> You can center components visually, but for a more exact method use the Alignment palette. Choose View|Alignment Palette from the main menu and then click the Center horizontally in window button on the Alignment palette to center a component horizontally on the form. <HR></BLOCKQUOTE><P>You will use this button to alternately show and hide the memo component. Nowyou need to write some code so that the button does something when it is clicked.Be sure that the button component is selected, and then click on the Events tab inthe Object Inspector.</P><PRE></PRE><P>A list of the events that a button component is designed to handle is presented.The top event should be the OnClick event. Double-click on the Value column of theOnClick event. What happens next is one of the great things about visual programming.The Code Editor comes to the top and displays the OnClick procedure ready for youto type code. Figure 5.3 shows the Code Editor with the OnClick handler displayed.</P><P><A HREF="javascript:popUp('tyd0503.gif')"><B>FIGURE 5.3.</B></A><B> </B><I>TheDelphi Code Editor with the OnClick handler displayed.</I></P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Your Code Editor might not look exactly like Figure 5.3. I removed the Module Explorer from the Code Editor window to show more of the code. One of the great things about the Delphi IDE is that it is fully customizable. If you don't like the default layout, you can always change it. <HR></BLOCKQUOTE><P><B>Adding Event Handler Code  </B>Notice that the event handler is alreadyset up for you, all you have to do is type the code. If you take a good look at theevent handler, you will see that it is a procedure, that it is called ButtonClick,that it is a member of the TPMEForm class, and that it takes a pointer to a TObjectcalled Sender as a parameter. (I'll talk about the Sender parameter in just a bit.)All that is left to do now is type code that shows and hides the button each timethe button is clicked. We'll borrow a little code from our earlier discussion ofmethods. Edit the ButtonClick method until it looks like this:</P><P><PRE>procedure TPMEForm.ButtonClick(Sender: TObject);const IsVisible : Boolean = False;begin IsVisible := not IsVisible; if IsVisible then Memo.Hide else Memo.Show;end;</PRE><PRE>This code first declares a typed constant named IsVisible.</PRE><P><strong>New Term:</strong> A <I>typed constant</I>, when used in this way, is a variablethat retains its value between calls to the method.</P><P>The first line of code in this method flips the Boolean variable between Trueand False by applying a logical NOT to the variable's present value. It works likeso:</P><DL> <DT></DT> <DD><B>1. </B>Initially the static variable is set to False. The first time the event handler is called, the variable is assigned NOT False, which is, of course, True. <P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -