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

📄 ch08.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<P></DL><P>Step 2 sets the hint text (from the Hint property of the Application object) tothe SimpleText property of the StatusBar component. Step 5 takes the method you createdin step 2 and assigns it to the OnHint event of the Application class. Each timean OnHint event occurs, the MyOnHint method is called and the hint text is displayedin the status bar.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> In the preceding example of implementing status bar hints, I took	you the long way around. I wanted to show you how to add a method to your form and	how to assign a method to an event. There is an easier way to implement status bar	text, though. Simply set the status bar's AutoHint property to True. You still have	to specify each component's hint text, but the rest is automatic. The AutoHint property	is new in Delphi 4. <HR></BLOCKQUOTE><P><H3><A NAME="Heading14"></A>Adding a Class Data Field</H3><P>Adding class data fields to a class generated in Delphi works in exactly the sameway. All you have to do is ensure that you add the data field to the private or publicsection of the class declaration as you did earlier when adding a method to the class.You can also place data field declarations in the protected section if you have createdone for your class.</P><P><H3><A NAME="Heading15"></A>Deleting Delphi-Generated Code</H3><P>There might be times when you need to delete code that Delphi generated in yourapplication. For example, you might have a button on a form that, due to design changes,is no longer needed. To delete the button, of course, all you have to do is selectthe button in the Form Designer and press the Delete button on the keyboard. No morebutton. Delphi deletes the button, but the OnClick handler associated with that buttonis still in the code.</P><P>Delphi knows that the button associated with that OnClick handler is gone, butit still doesn't delete the event handler because it is possible that other componentsare using the same event handler. It's up to you to delete the event handler if youwant it removed from your code.</P><P>The actual deletion of the event handler is a trivial task. Simply remove thecode from the event handler and save or compile the project. Delphi will remove anyempty event handlers it finds.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Some might say that if you are unsure about an event handler being	used by other components, just leave it in the code. That's a bad solution, in my	opinion. You need to take the responsibility for knowing what is in your code and	getting rid of any unused methods. Although unused code doesn't hurt anything, it	leads to a larger .exe file. In some cases, unused code can lead to performance degradation.	Be diligent in paring your programs of unused or inefficient code. <HR></BLOCKQUOTE><PRE></PRE><H2><A NAME="Heading16"></A>Creating Component Templates</H2><P><strong>New Term:</strong> A <I>component template</I> is a component or group of componentsthat you modify as desired and then save for later reuse.</P><P>Component templates enable you to create, save, and reuse groups of components.In fact, a component template doesn't have to be a group of components at all--itcan be a single component. A quick example would probably help you see how usefulcomponent templates can be. But first, a quick lesson on the Windows edit control.</P><P>The standard Windows single-line edit control, like all Windows controls, hascertain predefined behaviors. One of those behaviors deals with the way the Enterkey is handled. If the user presses the Enter key when in an edit control, Windowslooks for a default button on the window. If a default button is found, Windows essentiallyclicks the button.</P><P>What does this mean to you? Let's say you have several edit controls on a formand a default button such as an OK button (or any button with the Default propertyset to True). When you press the Enter key when an edit control has focus, the formwill close. If there is no default button on the form, Windows will just beep. Althoughthis is standard Windows behavior, many users find it annoying and confusing. Whatmany users prefer, particularly when working with a form that has several edit fields,is that the Enter key moves focus to the next control rather than closing the form.</P><P>The solution to this problem is really pretty simple. All you have to do is providean event handler for the OnKeyPress event and add code so that it looks like this:</P><P><PRE>procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);begin  if Key = Char(VK_RETURN) then begin    Key := #0;    PostMessage(Handle, WM_NEXTDLGCTL, 0, 0);  end;end;</PRE><P>This code first checks to see whether the key pressed was the Enter key (a virtualkey code of VK_RETURN). If so, it sets the value of Key to #0. This eliminates thebeep that Windows emits when the Enter key is pressed in an edit control. The nextline posts a Windows WM_NEXTDLGCTL message to the form. This message sets focus tothe next control in the tab order. That's all there is to it.</P><P>After you have written the code for your new Edit component, you can save it asa component template. When you do, all the code is saved as well. Any code templatesyou create go into the Templates page of the Component palette. Let's create a componenttemplate so that you can see how it works. Perform these steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Place an Edit component on a blank form. Change its Name property to	EnterAsTab and clear its Text property.	<P>	<DT></DT>	<DD><B>2. </B>Switch to the Events page in the Object Inspector and create an event	handler for the OnKeyPress event. Enter this code in the event handler:	<P></DL><BLOCKQUOTE>	<PRE>if Key = Char(VK_RETURN) then begin  Key := #0;  PostMessage(Handle, WM_NEXTDLGCTL, 0, 0);end;</PRE></BLOCKQUOTE><PRE></PRE><DL>	<DT></DT>	<DD><B>3. </B>Be sure the Edit component is selected and choose Component | Create	Component Template from the main menu. The Component Template Information dialog	box is displayed.	<P>	<DT></DT>	<DD><B>4. </B>Enter TEnterAsTab in the Component Name field. The Component Template	Information dialog box should now look like the one shown in Figure 8.12.	<P></DL><P><A HREF="javascript:popUp('28670812.gif')"><B>FIGURE 8.12.</B></A><B> </B><I>TheComponent Template Information dialog box.</I></P><DL>	<DT><I></I></DT>	<P>	<DT><I></I></DT>	<DD><B>5. </B>Click OK to save the component template.	<P></DL><P>Now your Component palette has a tab called Templates. Switch to the Templatestab (you might have to scroll the Component palette tabs to find it), select yournew component, and place it on the form. You will see that the code for the OnKeyPressevent handler was included when the component was placed on the form.</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> If you have several of these components on a form, the code for the	OnKeyPress event handler would be repeated for every EnterAsTab component on the	form. Rather than duplicating code, you can just place one EnterAsTab component on	the form. Any other components could be standard Edit components that have their	OnKeyPress events hooked up to the OnKeyPress event handler for the EnterAsTab component.	<HR></BLOCKQUOTE><P>One of the biggest advantages of component templates is that the code writtenfor each component's event handlers is saved along with the component. Componenttemplates enable you to have a collection of customized components at your disposal:common dialog boxes with predefined filters and titles, speed buttons with glyphsalready included, list boxes or combo boxes that automatically load items from afile, or any of a number of other possibilities.</P><P>Although the concept of a component template works for a single component, itmakes even more sense when dealing with multiple components. If you have a groupof components that you place on your forms over and over again, you can create acomponent template from those components. After you have created a component template,reusing a group of components is only a click away.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> There are certainly some similarities between component templates	and saving forms in the Object Repository. Use component templates for groups of	components that you typically use as part of a larger form. Use the Object Repository	to save entire forms that you want to reuse. <HR></BLOCKQUOTE><P><H2><A NAME="Heading17"></A>Using Resource Files</H2><P><strong>New Term:</strong> Every Windows program uses resources.<B><I> </I></B><I>Resources</I>are those elements of a program that support the program but are not executable code.</P><P>A typical Windows program's resources include</P><UL>	<LI>Accelerators	<P>	<LI>Bitmaps	<P>	<LI>Cursors	<P>	<LI>Dialog boxes	<P>	<LI>Icons	<P>	<LI>Menus	<P>	<LI>Data tables	<P>	<LI>String tables	<P>	<LI>Version information	<P>	<LI>User-defined specialty resources (sound and video files, for example)</UL><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> Version information can be easily added to your Delphi projects	through the Version Info tab of the Project Options dialog box. The Project Options	dialog box is discussed in detail tomorrow. <HR></BLOCKQUOTE><P>Resources are generally contained in a <I>resource script file</I> (a text filewith an .rc extension). The resource script file is compiled by a resource compilerand then bound to the application's .exe file during the link phase.</P><P>Resources are usually thought of as being bound to the executable file. Some resources,such as bitmaps, string tables, and wave files, can be placed in external files (.bmp,.txt, and .wav) or they can be bound to the .exe and contained within the applicationfile. You can opt to do it either way. Placing resources in the .exe file has twomain advantages:</P><UL>	<LI>The resources can be accessed more quickly because it takes less time to locate	a resource in the executable file than it does to load it from a disk file.	<LI>The program file and resources can be contained in a single unit (the .exe file)	without the need for a lot of supporting files.</UL><P>The downside to this approach is that your .exe will be slightly larger. The programfile won't be any larger than the combined external resource files plus the executable,but the extra size could result in slightly longer load times for the program.</P><P>Your exact needs will determine whether you decide to keep your resources in externalfiles or have your resources bound to the .exe. The important point to remember isthat you can do it either way (or even both ways in the same program).</P><P><H3><A NAME="Heading18"></A>Resources in Delphi</H3><P>A traditional Windows program will almost always contain at least one dialog boxand an icon. A Delphi application, however, is a little different. First of all,there are no true dialog boxes in a Delphi application, so there are no dialog boxresources per se (Delphi forms are stored as resources, but they are RCDATA resourcesand not dialog box resources).</P><P>A Delphi application does have a traditional icon resource, though. Delphi takescare of creating the resource file for the icon for you when you create the application.Similarly, when you choose bitmaps for speed buttons, Image components, or BitBtncomponents, Delphi includes the bitmap file you chose as part of the form's resource.The form and all its resources are then bound to the program file when the applicationis built. It's all more or less handled for you automatically.</P><P>There are times, however, when you will want to implement resources aside fromthe normal Delphi processes. For example, if you want to do animation, you will haveto have a series of bitmaps that can be loaded as resources for the fastest possibleexecution speed. In that kind of situation, you are going to need to know how tobind the resources to your Delphi program file.</P><P>The act of binding the resource file to the executable is trivial, actually. It'smuch more difficult to actually create the resources. Creating basic resources suchas bitmaps, icons, and cursors is not difficult with a good resource editor, butcreating professional quality 3D bitmaps and icons is an art in itself. How manytimes have you seen a fairly decent program with really awful bitmap buttons? I'veseen plenty. (Sorry, I'm getting off-track here.) You can create bitmaps, icons,and cursors with the Delphi Image Editor. (The Image Editor is discussed on Day 11,&quot;Delphi Tools and Options.&quot;)</P><P>If you are going to create string resources, user data resources, wave file resources,or other specialty resources, you will probably need a third-party resource editor.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> If you have an old copy of Borland Pascal lying around, you can use	the Resource Workshop from that product to edit specialty resources. After creating	the resources, you will have an .rc file that you can compile into a .res file using	the Borland Resource Compiler (BRCC32.EXE). The Borland Resource Compiler comes with	Delphi. Technically, you could create the .rc file with any text editor and compile	it with the Resource Compiler, but in reality it is much easier to use a resource	editor. <HR></BLOCKQUOTE><P><H3><A NAME="Heading19"></A>Compiling Resource Files</H3><P>After you create a resource file, you need to compile it with the resource compiler.You can do this in one of two ways:</P><UL>	<LI>Compile the resource file manually from the command line.

⌨️ 快捷键说明

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