ch04.htm
来自「delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具」· HTM 代码 · 共 1,371 行 · 第 1/5 页
HTM
1,371 行
</DL><BLOCKQUOTE> <P><HR><strong>TIP:</strong> It's fastest to place all components of a particular type on your form at one time using this technique. Components can always be rearranged and resized at a later time. <HR></BLOCKQUOTE><DL> <DT></DT></DL><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> When placing multiple copies of a particular component, it's easy to forget to click the arrow button when you're done. If you accidentally place more components than you intend, you can simply delete any extras. <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading8"></A>Placing and Centering a Component on the Form</H3><P>Delphi provides a shortcut method of placing a component on a form. Simply double-clickthe component's button in the Component palette and the component will be placedon the form. The component will be centered on the form both horizontally and vertically.Components placed with this method can be moved to another location on the form justlike components placed in the usual method.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Each time you double-click a button on the Component palette, a component will be placed on the center of the form in the component's default size. If you repeatedly double-click the component button, multiple copies of the component will be placed on the form. Each component will be placed in the center of the form and will be stacked on top of the previous one. It will appear as if you have a single component, so you might not realize that you have several components occupying the same space. If you accidentally place multiple components, just click the extra components and delete them from the form. <HR></BLOCKQUOTE><P><H3><A NAME="Heading9"></A>The Component Palette Context Menu</H3><P>When you place the mouse cursor over the Component palette and right-click, youwill see a menu specific to the Component palette (see Figure 4.5).</P><P><A HREF="javascript:popUp('28670405.gif')"><B>FIGURE 4.5.</B></A><B> </B><I>TheComponent palette context menu.</I></P><P>The Show Hints item toggles the tooltips on and off for the component buttons.Unless you really dislike tooltips, this should be left on. The Hide item on thecontext menu hides the Component palette. In order to show the Component paletteagain, you have to choose Component Palette from the toolbar context menu.</P><P>The Help item on the context menu brings up Delphi help with the Component Palettepage displayed. The Properties item brings up the Palette page of the EnvironmentOptions dialog box, where you can customize the Component palette. Here you can addand remove pages of the Component palette. You can also add, remove, or rearrangethe order of components on the individual pages. I'll discuss this in more detailon Day 11, "Delphi Tools and Options," when you learn about setting theenvironment options.</P><P><H3><A NAME="Heading10"></A>Navigating the Component Palette</H3><P>When the Component palette is sized small enough so that it cannot display allits tabs, you will see scroll buttons in the upper-right corner of the Componentpalette. Click these scroll buttons to display tabs not currently in view. Likewise,if a particular page of the Component palette contains more buttons than will fitthe width of the display window, scroll buttons will be enabled to allow you to scrollthrough the available buttons. Figure 4.6 shows the Component palette with both typesof scroll buttons enabled.</P><P><A HREF="javascript:popUp('tyd0406.gif')"><B>FIGURE 4.6.</B></A><B> </B><I>TheComponent palette scroll buttons.</I></P><P>The Component palette is not terribly complicated, but a basic understanding ofits use is vital for programming with Delphi. Now that you've finished with thisquick overview of the Delphi main window, you can return to the main topic again.</P><P><H2><A NAME="Heading11"></A>A Multiple-Form Application</H2><P>To illustrate how Delphi uses units, you can create an application with multipleforms. You'll create a simple application that displays a second form when you clicka button:</P><DL> <DT></DT> <DD><B>1. </B>Create a new project by choosing File | New Application from the main menu. <P> <DT></DT> <DD><B>2. </B>Change the Name property to MainForm and the Caption property to Multiple Forms Test Program. <P> <DD><B>3. </B>Save the project. Save the unit as Main and the project as Multiple. <DT></DT> <DD><B>4. </B>Now place a button on the form. Make the button's Name property ShowForm2 and the Caption property Show Form 2. <P> <DT></DT> <DD><B>5. </B>Choose File | New Form from the main menu (or click the New Form button on the toolbar) to create a new form. At this point, the new form has a name of Form1 and is placed exactly over the main form. You want the new form to be smaller than the main form and more or less centered on the main form. <P> <DT></DT> <DD><B>6. </B>Size and position the new form so that it is about 50 percent of the size of the main form and centered on the main form. Use the title bar to move the new form. Size the form by dragging the lower-right corner. <P> <DT></DT> <DD><B>7. </B>Change the new form's Name property to SecondForm and the form's Caption property to A Second Form. <P> <DT></DT> <DD><B>8. </B>Choose File | Save from the main menu (or click the Save File button on the toolbar) and save the new form with the name Second. <P> <DT></DT> <DD><B>9. </B>Choose a Label component and drop it on the new form. Change the label's Caption property to This is the second form. Change the label's size and color as desired. Center the label on the form. Your form should now look roughly similar to the one shown in Figure 4.7. <P></DL><P><A HREF="javascript:popUp('28670407.gif')"><B>FIGURE 4.7.</B></A><B> </B><I>Theform up to this point.</I></P><DL> <DT><I></I></DT> <P> <DT><I></I></DT> <DD><B>10. </B>Click on the main form. The second form is covered by the main form. Double-click the Show Form 2 button. The Code Editor is displayed and the cursor is placed just where you need to begin typing code (double-clicking a button is a shortcut way of generating an OnClick event handler). <P> <DT></DT> <DD><B>11. </B>Type in code so that the function looks like this (you have to type only one line of code): <P></DL><BLOCKQUOTE> <PRE>procedure TMainForm.ShowFrom2Click(Sender: TObject);begin SecondForm.ShowModal;</PRE> <PRE>end;</PRE></BLOCKQUOTE><PRE></PRE><DL> <DT></DT> <DD><B>12. </B>Run the program. <P></DL><P>At this point you get a message box that says, Form `MainForm' references form`SecondForm' declared in unit `second' which is not in your USES list. Do you wishto add it? Click Yes and Delphi will add the unit Second to the uses list of theunit Main. Click the Run button again and this time the application will run. Whenyou click the Show Form 2 button on the main form, the second form is displayed.You can close the second form by clicking the system close box on the form's titlebar.</P><P><H3><A NAME="Heading12"></A>Adding Units</H3><P>Rather than having Delphi prompt you to add a unit to your uses list, you canadd units yourself. You can manually type the unit name in the uses list for theform, or you can choose File | Use Unit from the main menu. When you choose the lattermethod, the Use Unit dialog box is displayed, as shown in Figure 4.8. The Use Unitdialog box shows a list of available units. Choose the unit you want to add and clickOK. Delphi will add the unit to the current form's uses list. Note that the Use Unitdialog box will show only those units that exist in the project <I>and</I> have notyet been included in this unit. Units that have already been included do not showup in the list of available units.</P><P><A HREF="javascript:popUp('28670408.gif')"><B>FIGURE 4.8.</B></A><B> </B><I>TheUse Unit dialog box.</I></P><P>As you can see, Delphi does a good job of managing units for you. Later, whenyour programming needs are more sophisticated, you'll have to do a little more sourcefile management, but at this stage of the game Delphi does most of the work for you.</P><P>Now let's take a moment to look at the different compiling options available toyou when writing programs in Delphi.</P><P><H3><A NAME="Heading13"></A>Compiling, Building, and Linking</H3><P>Each time you click the Run button, Delphi compiles and links your program. Butit doesn't necessarily compile every unit in the project. It only compiles any unitsthat have changed since the last compile. This feature saves you time because youdon't have to wait for the compiler to compile files that haven't changed. Delphikeeps track of which files have changed and which haven't, so you don't need to doanything special to use this feature--it's automatic.</P><P>Most of the time you want to see the results of any changes you have made in action.In those cases, click the Run button and the program is compiled, linked, and executed.Sometimes, however, you don't want to run the program. For example, you might justwant to compile the program to see whether there are any errors.</P><P>Delphi has three menu items in addition to Run that enable you to control thecompile/link process. If you choose the Project menu item on the main menu, you seethree menu items called Compile, Build, and Syntax Check. These menu items' textchanges to reflect the name of the active project. For example, when you first startDelphi, these menu items will say Compile Project1, Build Project1, and Syntax CheckProject 1. (There are also menu items called Compile All Projects and Build All Projects,but I'll save that discussion for Day 9 when I discuss project groups.) Let's takethese in order of simplest to most complex (from the compiler's perspective):</P><UL> <LI>The Syntax Check option is one I really like. This feature causes Delphi to compile the project and report any errors and warnings. This is the fastest way to check for errors in your code. Delphi only compiles the project--it does not perform a link. The purpose of the Syntax Check option is to check your code for syntax errors as quickly as possible. Because the link phase takes extra time, the Syntax Check option skips that step. <P> <LI>The Compile option compiles any units that have changed since the last compile just as Syntax Check does, but it also links the entire project. Naturally, this takes slightly longer than the Syntax Check option. Use the Compile option when you want to be sure the program will compile and link but you don't want to run the program.</UL><BLOCKQUOTE> <P><HR><strong>TIP:</strong> The keyboard shortcut for the Compile option is Ctrl+F9. <HR></BLOCKQUOTE><UL> <LI>The Build option takes the longest to perform. This option compiles every unit in the project regardless of whether it has changed since the last build. After compiling all units, Delphi links the entire project.</UL><P>So far, you have been letting Delphi add units to your projects. Further on downthe road you might have to do some hand-editing of your source files to add unitsor compiler directives. You might even end up editing the project source. From timeto time things can get goofed up (hey, we all make mistakes). Performing a buildwill bring everything up-to-date so that you can better sort out any problems youmight be running into. Sometimes a build resolves compiler and linker errors withoutthe need for you to do anything further.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> Any time you get unexpected (out of the ordinary) compiler or linker errors, first try a build. It could just be that something is out of sync, and a build might cure it. If performing a build doesn't fix the problem, you'll have to go to work figuring out where the problem lies. <HR></BLOCKQUOTE><P>Delphi gives you the option of displaying a compile status dialog box during compiles.You can turn on this option through the Environment Options dialog box (Preferencespage). When this option is on, the compile status dialog box shows the filename of
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?