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

📄 ch05.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<DT></DT>	<DD><B>2. </B>The next time the event handler is called, the variable is assigned	NOT True, and so on.	<P>	<DT></DT>	<DD><B>3. </B>Each time the method is called, IsVisible contains the opposite value	it had on the previous call. After that, the if/else pair calls either Show or Hide	depending on the value of IsVisible.	<P></DL><P>That's all there is to it. But does it work? Let's find out. Click the Run buttonon the toolbar. After being compiled, the program runs and is displayed. It's themoment of truth. Click the button, and the memo component is hidden. Click the buttonagain, and the memo component is again displayed. It works!</P><P>After playing with that for a minute, close the program (use the Close Programbutton in the upper-right corner of the title bar) and you are back to the Code Editor.</P><P>Before you go on, save the project. Choose File|Save All from the main menu. Thefirst thing you are prompted for is the name of the unit (source file). Type PMEMainand click OK. Next, you are prompted for a filename for the project. Type PMETestand press Enter or click OK.</P><P><B>Using the Visible Property&#160;&#160;</B>All that work with a Boolean variableis a bit cumbersome. Think back to the discussion about properties. Wouldn't it benice if the memo component had a property that could tell us whether the componentwas currently visible? Is there such a beast? Of course there is. It's called, predictably,Visible. Let's make use of it. Again, edit the event handler until it looks likethis:</P><P><PRE>procedure TPMEForm.ButtonClick(Sender: TObject);</PRE><P><B>begin</B></P><P><PRE>  if Memo.Visible then    Memo.Hide  else     Memo.Show;end;</PRE><P>Again click the Run button. The program is displayed and, lo and behold, the buttondoes what it's supposed to. How about that? You managed to use properties, methods,and events in the same example.</P><P>Are you getting the fever yet? Hold on, because there's much more to come. Oh,and wipe that silly grin off your face. . .your boss thinks you're working!</P><P><B>The Sender Parameter&#160;&#160;</B>As you can see, the ButtonClick methodtakes a pointer to a TObject called Sender (whether you know it or not, all classvariables in Delphi are pointers). Every event handler will have at least a Senderparameter. Depending on the event being handled, the event handler might have oneor more additional parameters. For instance, the OnMouseDown event handler lookslike this:</P><P><PRE>procedure TPMEForm.FormMouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);beginend;</PRE><P>Here you are getting information on the button that was pressed, which keyboardkeys were pressed at the time the mouse was clicked, and the x, y coordinate of thecursor when the mouse button was clicked. The event handler contains all the informationyou need to deal with the particular event the event handler is designed to handle.</P><P>So what exactly is Sender? Sender is a pointer to the component that is sendingthe message to the message handler. In this example, the Sender parameter is extrabaggage because you know that the Show/Hide button is the sender. Sender exists toenable you to have more than one component use the same event handler.</P><P>To illustrate, let's create a new button and make one of our buttons the Showbutton and the other the Hide button:</P><DL>	<DD><B>1. </B>If the Code Editor is on top, press F12 to switch back to the Form	Editor.	<DT></DT>	<DD><B>2. </B>Click on the Show/Hide button to select it. Change both the Name and	Caption properties to Show.	<P>	<DT></DT>	<DD><B>3. </B>Add a new button to the form to the right of the Show button. Arrange	the buttons if you want to give an even look to the form.	<P>	<DT></DT>	<DD><B>4. </B>Change the Name property for the new button to Hide. The Caption property	will also change to Hide (you'll have to press Enter before the Caption property	will change).	<P>	<DT></DT>	<DD><B>5. </B>Click the Show button and then click on the Events tab in the Object	Inspector. Notice that the OnClick event now says ShowClick. Edit it to say ButtonClick	again. (The initial event-handler name is a default name. You can change it to any	name you like.)	<P>	<DT></DT>	<DD><B>6. </B>Click the Hide button and find the OnClick event in the Object Inspector	(it will be selected already). Next to the value is a drop-down arrow button. Click	the arrow button and choose ButtonClick from the list that drops down (there will	be only one name in the list at this point).	<P>	<DT></DT>	<DD><B>7. </B>Double-click on the value ButtonClick. You are presented with the Code	Editor with the cursor in the ButtonClick method. Modify the code so that it reads	like this:	<P></DL><PRE>procedure TPMEForm.ButtonClick(Sender: TObject)begin  if Sender = Hide    then Memo.Hide  else     Memo.Show;end;</PRE><DL>	<DT></DT>	<DD><B>8. </B>Bake at 425 degrees for one hour or until golden brown. (Just checking.)	<P></DL><PRE>Your form will look similar to Figure 5.4. Compile and run the program. Click each button to be sure that it functions as advertised. </PRE><P><A HREF="javascript:popUp('28670504.gif')"><B>FIGURE 5.4.</B></A><B> </B><I>Theform with all components added.</I></P><P><P>What you have done here is create a single event-handling method that handlesthe OnClick event of both buttons. You use the Sender parameter to determine whichbutton sent the OnClick event and then either hide or show the memo component asneeded. You could have created a separate OnClick handler for each button, but withthis method the code is more compact. Besides, it's a good illustration of how Sendercan be used.</P><P>Note that I am comparing the Sender variable to a component's Name property. Becauseboth are pointers, a comparison is being made to see whether both variables containthe same address.</P><P>Step 6 in the previous exercise illustrates an important point: After you createan OnClick event handler for a particular component, you can attach that same handlerto the OnClick event of any component on the form. This enables you to use the sameevent handler for multiple components. I'll discuss events in more detail as youprogress through the book.</P><BLOCKQUOTE>	<P><HR><B>HOUSE RULES: EVENTS</B></BLOCKQUOTE><UL>	<LI>You can respond to any of a component's events as needed.	<P>	<LI>You are not required to respond to all events a component defines.	<P>	<LI>Events are handled by event-handling methods called event<I> </I>handlers.<BR>	<BR>		<LI>Several components can share a common event handler.	<LI>Event-handler names produced by Delphi are default names and can be changed by	the programmer.	<P>	<LI>Be sure to change an event handler's name only in the Object Inspector.	<P>	<LI>An event handler's Sender parameter can be used to determine which component	generated the event.	<P>	<LI>Double-clicking the event handler's name in the Object Inspector displays the	Code Editor and takes you to the section of code containing the event handler.	<P>	<LI>Each event handler contains the parameters needed to properly handle that event.	<HR></UL><H2><A NAME="Heading8"></A>VCL Explored</H2><P>The Visual Component Library is a well-designed framework. As with most good frameworks,VCL makes maximum use of inheritance. The bulk of the VCL framework is composed ofclasses that represent components. Other VCL classes are not related to components.These classes perform housekeeping chores, act as helper classes, and provide someutility services.</P><P>The VCL class hierarchy dealing with components is complex. Fortunately, you don'thave to know every detail of VCL to begin programming in Delphi. At the top of theVCL chain, you will find TObject. Figure 5.5 shows some of the main base classesand the classes derived from them.</P><P><BR><A HREF="javascript:popUp('28670505.gif')"><B>FIGURE 5.5.</B></A><B> </B><I>The VCLclass hierarchy.</I></P><P>TObject is the granddaddy of all VCL component classes. (Remember that all classesin Object Pascal are derived from TObject.) Below TObject you see TPersistent. Thisclass deals with a component's capability to save itself to files and to memory aswell as other messy details you don't need to know about. I'm thankful (and you shouldbe, too) that you don't need to know much about TPersistent to program most applicationsin Delphi.</P><P>The TComponent class serves as a more direct base class for components. This classprovides all the functionality that a basic component requires. Nonvisual componentsare derived from TComponent itself. Visual components are derived from TControl,which, as you can see in Figure 5.5, is derived from TComponent. TControl providesadditional functionality that visual components require. The individual components,then, are derived from either TGraphicControl or TWinControl.</P><P>When you drop a component on a form, Delphi creates a pointer to that componentin the form's class declaration so that you can access the component in your code.Delphi uses the component's Name property for the pointer variable's name. When youcreated the sample application earlier, you placed a memo component on the form.At that point Delphi created a TMemo variable and gave it the name Memo.</P><P>Similarly, when you created a button on the form, Delphi created a TButton variableto represent the button. Before any of that took place, Delphi had already deriveda new class from TForm and, of course, created an instance of that class to representthe form.</P><P>Some understanding of the VCL classes and components is obviously necessary beforeworking with VCL. Although I cannot review each and every VCL class, I can hit thehigh points. Let's take a look at some of the classes that you will use most frequently.</P><P><H3><A NAME="Heading9"></A>Form and Application Classes</H3><P>Form and application classes represent forms and the Application object in VCL.These classes are derived from TComponent and indeed are components themselves. Theyare listed separately to distinguish them from the controls you drop on a form.</P><P><H4>The TApplication Class</H4><P>The TApplication class encapsulates the basic operations of a Windows program.TApplication takes care of responsibilities such as managing the application's icon,providing context help, and doing basic message handling. Every Delphi applicationhas a pointer to the TApplication object called Application. You use the TApplicationclass primarily to execute message boxes, manage context help, and set hint textfor buttons and status bars. TApplication is a bit of an oddity in VCL in that someof its properties (Icon, HelpFile, and Title) can be set via the Application pageof the Project Options dialog box.</P><P><H4>The TForm Class</H4><P>The TForm class encapsulates forms in VCL. Forms are used for main windows, dialogboxes, secondary windows, and just about any other window type you can imagine. TFormis a workhorse class in VCL. It has nearly 60 properties, 45 methods, and 20 events.I discussed forms in detail in Day 4.</P><PRE></PRE><H3><A NAME="Heading10"></A>Component Classes</H3><P>This group of classes encompasses a wide range of classes and can be further dividedinto separate categories, which I've done in the following sections.</P><P><H4>Standard Component Classes</H4><P>The standard components are those that encapsulate the most common Windows controls.The standard component classes include TButton, TEdit, TListBox, TMemo, TMainMenu,TScrollBar, TPopupMenu, TCheckBox, TRadioButton, TRadioGroup, TGroupBox, TPanel,and TActionList.</P><P>Most of these classes encapsulate a Windows control, so I won't discuss all ofthem right now. The TMainMenu class encapsulates an application's main menu. At designtime, double-clicking the MainMenu component's icon brings up the Menu Designer.TMainMenu has properties that control whether the menu item is grayed out, whetherit is checked, the help context ID, the item's hint text, and others. Each menu itemhas a single event, OnClick, so that you can attach an event handler to a menu itembeing selected. I'll discuss menus and the Menu Designer in more detail tomorrow.</P><P><B>The TPanel Component&#160;&#160;</B>Another standard component of interestis TPanel.</P><P><strong>New Term:</strong> A <I>panel</I> represents a rectangular region on a form,usually with its own components, that can be treated as a single unit.</P><P>The Panel component is a container component. As such it can contain other components.Panels have properties that control what type of edge the panel should have; whetherthe panel is raised, sunken, or flat; and the width of the border. Combinations ofthese properties can be used to create a variety of 3D panels.</P><P><B>The TActionList Component&#160;&#160;</B>The TActionList component is new toDelphi 4. This component can be used to easily add command enabling to a componentor a group of components. For example, an application that uses the Clipboard mighthave cut, copy, and paste items on a menu, on a toolbar, and on a context menu. Ifthere is data in the Clipboard, the paste button and menu items should be enabled.If there is no data in the Clipboard, the button and menu items should be disabled.

⌨️ 快捷键说明

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