📄 ch08.htm
字号:
<BLOCKQUOTE> <P><HR><strong>TIP:</strong> After you create an Application Wizard project, you can choose Project | Add to Repository to save the project for later use. This will save you the trouble of going through the Application Wizard to create your basic application. You might want to add an About box before saving the project to the Repository. <HR></BLOCKQUOTE><P>Using the wizards is fast and easy. You will still need to write the program,of course, but Delphi gives you a head start by saving you from the tedium of creatingthe basic application elements. As RAD-friendly as Delphi is overall, the wizardssimplify this process even more. The Delphi wizards are sort of like RAD on RAD!</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Delphi provides wizards other than the Dialog Wizard and the Application Wizard. For example, the Database Form Wizard (discussed on Day 17, "Building Database Forms") is used to create database forms, and the ActiveX Control Wizard (discussed on Day 15, "COM and ActiveX") aids in the creation of ActiveX controls. These are specialized wizards, so I didn't cover them in this chapter. <HR></BLOCKQUOTE><H2></H2><H2><A NAME="Heading11"></A>Adding Methods and Data Fields to Code</H2><P>As you know by now, Delphi is a great tool for quickly creating the UI (user interface)portion of a Windows application. It creates event handlers for you so that you canbegin entering code to drive your application. It won't be long, however, beforeyou find the need to start adding more complicated code to your applications.</P><P>Part of that means adding your own data fields and methods to the code that Delphigenerates. For example, a simple application might contain two dozen event handlersof various types. Delphi creates all these event handlers for you; you simply fillin the blanks with working code. To make the application a viable, working application,however, you might have to write another two-dozen methods of your own.</P><P>Adding your own methods and data fields to code generated by Delphi is not a difficulttask, but you need to know the rules or you can get into trouble.</P><P><H2><A NAME="Heading12"></A>How Delphi Manages Class Declarations</H2><P>As you know, when you create a new form in the Form Designer, Delphi creates theunit's source file automatically. When Delphi creates the class declaration, it essentiallycreates two sections. The first section is the part of the class declaration thatDelphi manages. The second section is the part that you manage.</P><P>On Day 6, "Working with the Form Designer and the Menu Designer," youcreated the ScratchPad program. If you did the exercises at the end of that chapter,you also created an About box for the program and added a few more buttons. Listing8.1 contains the main form's class declaration as it appears after adding these enhancements.</P><P>Keep in mind that the individual component declarations appear in the order thecomponents were placed on the form. Your class declaration should have the same componentsas shown in Listing 8.1, but they might not be in the same order.</P><P><H4>LISTING 8.1. THE class DECLARATION FOR ScratchPad'S MAIN FORM.</H4><PRE>TMainForm = class(TForm) StatusBar1: TStatusBar; ToolBar1: TToolBar; ToolButton1: TToolButton; ToolButton2: TToolButton; Memo: TMemo; MainMenu: TMainMenu; FileMenu: TMenuItem; FileNew: TMenuItem; FileOpen: TMenuItem; FileSave: TMenuItem; FileSaveAs: TMenuItem; N1: TMenuItem; FilePrint: TMenuItem; FilePrintSetup: TMenuItem; N2: TMenuItem; FileExit: TMenuItem; Edit1: TMenuItem; EditReplace: TMenuItem; EditFind: TMenuItem; N4: TMenuItem; EditPaste: TMenuItem; EditCopy: TMenuItem; EditCut: TMenuItem; N5: TMenuItem; EditUndo: TMenuItem; Help1: TMenuItem; HelpAbout: TMenuItem; HelpContents: TMenuItem; EditSelectAll: TMenuItem; N3: TMenuItem; EditWordWrap: TMenuItem; OpenDialog: TOpenDialog; SaveDialog: TSaveDialog; MemoPopup: TPopupMenu; PopupCut: TMenuItem; PopupCopy: TMenuItem; PopupPaste: TMenuItem; procedure FileExitClick(Sender: TObject); procedure EditCutClick(Sender: TObject); procedure EditCopyClick(Sender: TObject); procedure EditPasteClick(Sender: TObject); procedure FileNewClick(Sender: TObject); procedure FileSaveClick(Sender: TObject); procedure FileOpenClick(Sender: TObject); procedure FileSaveAsClick(Sender: TObject); procedure EditUndoClick(Sender: TObject); procedure EditSelectAllClick(Sender: TObject); procedure EditWordWrapClick(Sender: TObject); procedure HelpAboutClick(Sender: TObject); private { Private declarations } public { Public declarations } end;</PRE><P>It is important to understand that the section between the first line of the classdeclaration and the private keyword should be considered off-limits. As they say,"Don't go there." Leave this section to Delphi to manage.</P><BLOCKQUOTE> <P><HR><strong>CAUTION:</strong> Placing any code in the Delphi-managed section of a form's class declaration can cause problems with your program. In some cases, you might just get compiler errors. In other cases, your program might be beyond repair (unusual but possible). Get in the habit of avoiding this section of the class declaration like the plague. <HR></BLOCKQUOTE><P>You can safely place any of your own class data fields or method declarationsin either the private or the public section of the class declaration. You could adda protected section and place data fields or methods there too, of course.</P><BLOCKQUOTE> <P> <P><HR><B>A WORD ABOUT STATUS BARS AND HINTS</B></P> <P>In a moment you're going to add support for hint text displayed in the status bar of the ScratchPad program. Before you do, though, you need a brief primer on how hint text is handled.</P> <P>When the Application object's ShowHint property is set to True (the default) and the mouse cursor is placed over a component that also has its ShowHint property set to True, a hint event is triggered. The Application object has an event called OnHint that occurs whenever a hint event is triggered. The Application's Hint property will contain the hint text for the control that generated the hint event. An application can use the OnHint event to display the hint on a status bar. </P> <P>The problem is that you can't directly access the OnHint event of the Application object. What you can do, however, is reassign the value of OnHint to point to one of your own methods. Then, when the hint event occurs, the event gets rerouted to your own OnHint handler. To do that, you have to write your own event handler for the OnHint event. Let's do that next. <HR></BLOCKQUOTE><P><H2><A NAME="Heading13"></A>Adding a Method to Your Code</H2><P>To illustrate adding a method to an application, let's implement hint text forthe ScratchPad program you wrote earlier. First, reopen the ScratchPad program.</P><P>What you do in this series of steps is assign hint text to each of the toolbarbuttons and prepare the status bar to receive the hints. Remember, the toolbar buttonsyou placed on the toolbar on Day 6 are just for show right now, but that doesn'tprevent you from adding hints to them. Do the following:</P><DL> <DT></DT> <DD><B>1. </B>Ensure that the ScratchPad main form is visible. Click the first button on the main form's toolbar. <P> <DT></DT> <DD><B>2. </B>Locate the Hint property in the Object Inspector and type the following for the hint text: <P></DL><BLOCKQUOTE> <PRE>Open|Open an Existing File</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD><B>3. </B>Change the ShowHint property to True. <P> <DT></DT> <DD><B>4. </B>Repeat steps 2 and 3 for any other buttons on the toolbar, adding whatever hint text you like for each button. <P> <DT></DT> <DD><B>5. </B>Click the status bar component along the bottom of the main form. Change the SimplePanel property to True. This enables the full status bar to display a text string through the SimpleText property. <P></DL><P>Okay, now everything is ready to go, so it's time you did what you came here for.You're going to create your own OnHint handler and then name the method MyOnHint.Let's take this one step at a time. First, add the method declaration to the classdeclaration. Here goes:</P><DL> <DT></DT> <DD><B>1. </B>Switch to the Code Editor and be sure the SPMain.pas file is visible. <P> <DT></DT> <DD><B>2. </B>Scroll down through the class declaration for the TScratchPad class until you locate the private section. Add this line of code after the private keyword: <P></DL><BLOCKQUOTE> <PRE>procedure MyOnHint(Sender : TObject);</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD>To give you perspective, the last few lines of the class declaration should now look like this: <P></DL><BLOCKQUOTE> <PRE>private { Private declarations } procedure MyOnHint(Sender : TObject);public { Public declarations }end;</PRE></BLOCKQUOTE><PRE></PRE><P>Okay, so far, so good. Now you've added the method declaration for your new method.Two more steps and you'll be done. First, you need to add the actual method to theimplementation section. After that, you need to assign your new method to the Applicationobject's OnHint event. Follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Scroll to the bottom of the implementation section. <P> <DT></DT> <DD><B>2. </B>Enter the following code (just above the unit's final end keyword): <P></DL><BLOCKQUOTE> <PRE>procedure TMainForm.MyOnHint(Sender: TObject);begin StatusBar.SimpleText := Application.Hint;end;</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD><B>3. </B>Go to the Object Inspector. Select the main form, ScratchPad, from the Object Selector. <P> <DT></DT> <DD><B>4. </B>Switch to the Events page in the Object Inspector and double-click in the Value column next to the OnCreate event. The Code Editor is displayed and is ready for you to type code. <P> <DT></DT> <DD><B>5. </B>Enter a line of code so that the FormCreate method looks like this: <P></DL><BLOCKQUOTE> <PRE>procedure TMainForm.FormCreate(Sender: TObject);begin Application.OnHint := MyOnHint;end;</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD><B>6. </B>Compile and run the program. The long hint text you entered will appear in the status bar when you place the mouse cursor over a toolbar button. The short hint text will be displayed in a tooltip when you pause over the button.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -