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

📄 ch20.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
in the constructor.</P><P>Notice these three lines (comments removed):</P><P><PRE>Timer := TTimer.Create(Self);Timer.Interval := FFlashRate;Timer.OnTimer := OnTimer;</PRE><P>The first line creates an instance of the TTimer object. The second line assignsthe value of the FFlashRate data field to the Interval property of the Timer component,which sets the timer interval that will be used to flash the text on the label. Finally,the last line assigns the OnTimer function to the OnTimer event of the Timer object.This ensures that when a timer event occurs, the component will receive notification.</P><P>The SetFlashEnabled procedure is the write method for the FlashEnabled property.This procedure first assigns the FFlashEnabled data field to the value of AFlashEnabled.Next, the Enabled property of the Timer object is set according to the value of AFlashEnabled.By writing to the FlashEnabled property, the user of the component can turn the flashingon or off. This function also contains a code line that sets the Visible propertyto True when flashing is disabled. This eliminates the situation in which the flashingcould be turned off in mid-flash and, as a result, keep the text hidden.</P><P><H3><A NAME="Heading13"></A>The SetFlashRate Procedure</H3><P>Now turn your attention to the SetFlashRate procedure. This is the write methodfor the FlashRate property. The user can assign a value to the FlashRate propertyto control how fast the component flashes. (A rate of 1200 is slow, and a rate of150 is very fast.) This function contains these lines:<PRE></PRE><PRE>FFlashRate := AFlashRate;Timer.Interval := AFlashRate;</PRE><P>This procedure simply assigns the value of AFlashRate first to the FFlashRatedata field and then to the Interval property of the Timer object. This ensures thatif the user changes the flash rate at runtime, the flash rate will change accordingly.</P><P>The OnTimer method doesn't require much explanation. This method is called inresponse to an OnTimer event. All you do is toggle the component's visible stateeach time a timer event occurs. In other words, if the FlashRate were set to 1000,the timer events would occur nearly every second. When the first timer event occurs,the component is hidden. A second later, the next timer event occurs and the componentis shown. This continues until the FlashEnabled property is set to False or untilthe application closes. Finally, at the end of the unit you see the component registration.</P><P>Enter the code from Listing 20.2 into the FlashingLabel.pas file that Delphi createdfor you earlier (when you used the New Component dialog box). Don't worry about typingthe comment lines if you don't want to. A bit later I'll show you how to test thecomponent to see whether it works.</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> Delphi's class completion makes declaring read and write methods for	properties easy. Let's say, for example, that you typed this property declaration:<HR></BLOCKQUOTE><PRE>property FlashRate : Integer  read FFlashRate write SetFlashRate;</PRE><P>The next step would probably be to declare and define the SetFlashRate method.Guess what? Delphi will do it for you! Just press Ctrl+Shift+C and Delphi will createany read or write methods you haven't yet defined, and it will even add a declarationfor the data field (FFlashRate in this example).</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> If you have Delphi Professional or Client/Server, you can copy the	property redeclarations directly from the VCL source. Just open \Delphi 4\Source\Vcl\	StdCtrls.pas, copy the properties list from the TLabel class declaration, and paste	it into your class declaration. <HR></BLOCKQUOTE><H3><A NAME="Heading14"></A>The ComponentState Property</H3><P>I'm getting a little ahead of myself here, but I want to point out some code thatI didn't discuss in the analysis of Listing 20.2. There is one important line inthe SetFlashEnabled function that requires explanation. Here's that line, and theone that follows:</P><P><PRE>if csDesigning in ComponentState then  Exit;</PRE><P>This code checks whether the component is being used on a form at design time.If the form is being used at design time, you need to prevent the timer from starting.When a component is placed on a form in the Form Designer, it does not necessarilyhave full functionality--the Form Designer can't fully approximate a running program.In this case, you don't want the timer to run while the component is being used indesign mode.</P><P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong>  You could write the TFlashingLabel component so that the label flashes	at design time as well as runtime. It is easier not to deal with that now, plus it	gives me a chance to talk about the ComponentState property.<HR></BLOCKQUOTE><PRE></PRE><P>All components have a property called ComponentState. The ComponentState propertyis a set that indicates, among other things, whether the component is being usedat design time. If the set includes csDesigning, you know that the component is beingused on a form in the Form Designer. In this case, when you determine that the componentis being used at design time, you can return from the OnTimer function without startingthe timer.</P><P>This code, then, checks whether the component is being used on a form in the FormDesigner. If the component is being used in the Form Designer, the timer is turnedoff, and the method exits without performing the remaining code in the method. Thetimer, by default, will be started in the TFlashingLabel constructor, so it needsto be immediately turned off if the component is being used at design time.</P><P><H2><A NAME="Heading15"></A>Testing the Component</H2><P>Ultimately, you will add your newly created component to the Component palette.First, though, you must test your component to be sure that it compiles and thatit functions as you intended. This is a vital step in component development, andone that many component writers overlook. There's no point in being in a hurry toadd a new component to the Component palette. First be sure that the component worksas expected and then worry about adding the component to the Component palette.</P><P>To test your component, write an application that will serve as a testing ground.Because you can't drop the component on a form from the Component palette, you haveto create the component manually. In this case, because your FlashingLabel componenthas two properties, you want to make sure that each property works.</P><P>For that reason, your test program will need to turn the flashing mode on andoff. In addition, the test program should enable you to set several flashing ratesto see whether the FlashRate property performs as designed. Figure 20.2 shows thetest program running. This will give you an idea of what you'll be trying to accomplish.</P><P><A HREF="javascript:popUp('28672002.gif')"><B>FIGURE 20.2.</B></A><B> </B><I>Thetest program running.</I></P><P><I><BR></I>Now that you've had a peek at the test program, let's create it. As always, startwith a blank form. First, add the check box and radio group components as you seethem in Figure 20.2:<PRE></PRE><DL>	<DT></DT>	<DD><B>1. </B>Change the form's Name property to MainForm and the Caption property	to FlashingLabel Test Program.	<P>	<DT></DT>	<DD><B>2. </B>Using Figure 20.2 as a pattern, add a CheckBox component to the form.	Change its Name property to FlashBox, its Caption property to Flash, and its Checked	property to True.	<P>	<DT></DT>	<DD><B>3. </B>Double-click the check box to create an event handler for the OnClick	event. When the Code Editor displays the OnClick handler, type this code at the cursor	(the name of the FlashingLabel component will be Flasher):	<P></DL><BLOCKQUOTE>	<PRE>Flasher.FlashEnabled := FlashBox.Checked;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD>This will enable or disable flashing depending on the state of the check box.	<P>	<DT></DT>	<DD><B>4. </B>Place a RadioGroup component on the form. Change its Name property	to Group and its Caption property to Flash Speed.	<P>	<DT></DT>	<DD><B>5. </B>Double-click on the Value column next to the Items property. When the	String Editor is displayed, type these lines:	<P></DL><BLOCKQUOTE>	<PRE>SlowMediumFastLight Speed</PRE></BLOCKQUOTE><PRE></PRE><BLOCKQUOTE>	<P>Click OK to close the String Editor. The strings you typed will be displayed as	radio buttons in the group box.</BLOCKQUOTE><DL>	<DT></DT>	<DD><B>6. </B>Set the ItemIndex property to 1. The Medium radio button will be selected.	<P>	<DT></DT>	<DD><B>7. </B>Double-click the radio group component. The Code Editor will display	the OnClick event handler for the group box. Type the following code at the cursor:	<P></DL><BLOCKQUOTE>	<PRE>case Group.ItemIndex of  0 :  Flasher.FlashRate := 1200;  1 :  Flasher.FlashRate := 800;  2 :  Flasher.FlashRate := 400;  3 :  Flasher.FlashRate := 150;end;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD>This will set the FlashRate value of the FlashingLabel component based on the	radio button selected.	<P>	<DD><B>8. </B>Save the project in the same directory where your TFlashingLabel component	resides. Save the main form's unit as FlshTstU and the project as FlashTst (remember,	I use short filenames for the book's code, but you can use long filenames if you	want).	<P>Okay, now you add the component itself. Because this component isn't yet visual	(you can't add it from the Component palette), you will have to add it manually.</DL><P><DL>	<DT></DT>	<DD><B>1. </B>Click the Add to Project button (from the toolbar, from the main menu,	or from the Project Manager context menu). When the Add to Project dialog box is	displayed, choose the FlashingLabel.pas file and click OK.	<P>	<DT></DT>	<DD><B>2. </B>Navigate to the top of the unit and add FlashingLabel to the unit's	uses list.	<P>	<DT></DT>	<DD><B>3. </B>Add this declaration in the private section of the TMainForm class:	<P></DL><BLOCKQUOTE>	<PRE>Flasher : TFlashingLabel;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>4. </B>Double-click the form's background to create an OnCreate event handler	for the form. Type the following code in the event handler:	<P></DL><BLOCKQUOTE>	<PRE>Flasher := TFlashingLabel.Create(Self);Flasher.Parent := Self;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DD>Flasher.SetBounds(20, 20, 200, 20);</DL><P><BLOCKQUOTE>	<PRE>Flasher.Font.Size := 16;Flasher.Caption := `This is a test';Flasher.FlashRate := 800;</PRE></BLOCKQUOTE><PRE></PRE><P>Now you are ready to test the component. Click the Run button to compile and runthe test program. If you encounter any compiler errors, carefully check your codeand fix any errors the compiler points out.</P><P>When the program runs, click the Flash check box to turn the flashing on or off.Change the flash rate by choosing one of the radio buttons in the group box. Hey,it works! Congratulations, you've written your first component.</P><P><H2><A NAME="Heading16"></A>Adding the Component to the Component Palette</H2><P>After the component is working properly and you are satisfied with it, you canadd it to the Component palette. To add the FlashingLabel component to the Componentpalette, choose Component|Install Component. The Install Component dialog box willappear. This dialog box enables you to add a component to a package. Figure 20.3shows the Install Component dialog box.</P><P><A HREF="javascript:popUp('28672003.gif')"><B>FIGURE 20.3.</B></A><B> </B><I>TheInstall Component dialog box.</I></P><P>Okay, you're ready to add the FlashingLabel component to the Component palette.Perform the following steps:</P><P>

⌨️ 快捷键说明

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