ch04.htm
来自「delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具」· HTM 代码 · 共 1,371 行 · 第 1/5 页
HTM
1,371 行
main form unit, and any other units in the project. Several things happen duringthis process. First, the Object Pascal compiler compiles the project's units intobinary object files. Then, the resource compiler compiles any resources, such asthe program's icon and form files, into binary resource files. Next, the linker takesover. The linker takes the binary files the compiler created, adds any library filesthe project needs, and binds them all together to produce the final executable file.When it's all over, you have a stand-alone program that can be run in the usual ways.</P><P>Okay, but what are all those files for? Table 4.1 lists the file extensions Delphiuses with a description of the role that each file type plays.</P><P><H4>TABLE 4.1. TYPES OF FILES USED IN DELPHI.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Extension</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.pas</TD> <TD ALIGN="LEFT">The Object Pascal source files. There will be one for each unit, as well as any other source files that you add to the project.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.dfm</TD> <TD ALIGN="LEFT">The form file. This file is actually a binary resource file (.res) in disguise. It is a description of the form and all its components. Each form has its own .dfm file.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.dsk</TD> <TD ALIGN="LEFT">The project desktop file. This file keeps track of the way the desktop appeared when you last saved (or closed) the project. All the open windows' sizes and positions are saved so that when you reopen the project it looks the same as you left it. This file is created only if you turn on the option to save your desktop (Environment Options dialog box).</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.dof</TD> <TD ALIGN="LEFT">The project options file. This file contains the project options as set in the Project Options dialog.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.exe</TD> <TD ALIGN="LEFT">The final executable program.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.cfg</TD> <TD ALIGN="LEFT">The project configuration file. This file primarily contains the current compiler and linker settings for the project.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.dcu</TD> <TD ALIGN="LEFT">The compiled binary object files. These are the files that the compiler produces when it compiles your Object Pascal units.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.dpr</TD> <TD ALIGN="LEFT">The project source file.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">.res</TD> <TD ALIGN="LEFT">The compiled binary resource file.</TD> </TR></TABLE><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Delphi has other associated file extensions as well. For example, the .bpg extension is used to denote a project group, the .dpk extension is used to designate a Delphi package source file, and the .bpl extension represents a compiled package. Packages are discussed in detail on Day 8, "Creating Applications in Delphi," and project groups are discussed on Day 9. <HR></BLOCKQUOTE><P>The files that Delphi produces can be divided into two categories: files Delphirelies on to build the project and files that Delphi creates when it compiles andlinks a project. If you were to move your source files to another computer, for example,you wouldn't have to move <I>all</I> the files, just the files Delphi needs to buildthe application. Conveniently, the source files happen to be the smallest files inthe project. It does not take a lot of disk space to back up just the project sourcefiles.</P><P>The minimum set of files consists of the .pas, .dfm, and .dpr files. All otherfiles are files that Delphi will re-create when you compile the program. The desktopfile (.dsk) is one that you might want to hang on to because it keeps track of thestate your project was in when you last worked on it.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> In addition to the source files I've mentioned, some applications use a resource script file (resource scripts have an .rc extension). <I>Resource scripts</I> are text files that are used to define resources such as bitmaps, icons, or cursors. If you use a resource script, be sure to keep it with the project if you move the project to another location. Resource script files are not commonly used with Delphi projects. <HR></BLOCKQUOTE><P>Figure 4.2 illustrates how Delphi compiles source files and links them to formthe final executable file.</P><P><A HREF="javascript:popUp('28670402.gif')"><B>FIGURE 4.2.</B></A><B> </B><I>TheDelphi compile/link process.</I></P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> If you find yourself running low on hard disk space, you can delete some of the Delphi files from projects you are not currently working on. It is safe to delete the files with a .dcu extension. These files will be regenerated when the project is built, and there is no use in keeping them for noncurrent projects. <HR></P> <P><HR><BR> <strong>CAUTION:</strong> Do not delete any files from the Delphi directories other than the Examples directory. If in doubt, <I>don't delete!</I> <HR></BLOCKQUOTE><H3></H3><H3><A NAME="Heading4"></A>Source Code Units</H3><P>Earlier I mentioned that most applications of any size have several source files,which are called <I>units</I>. Each time you create a new form, Delphi does the following:</P><UL> <LI>Creates a form file (.dfm) <P> <LI>Derives a class from TForm <P> <LI>Creates a unit (.pas file) for the class definition <P> <LI>Adds the new form information to the project source</UL><P>Initially Delphi assigns a default name of Form1 to the form and Unit1.pas tothe form's unit. The second form created for the project would have a default nameof Form2, and so on. Each time you create a new form, Delphi creates a new unit (.pas)and form file (.dfm) for that form.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> As soon as you create a new project, you should save it with a meaningful name. Likewise, every time you create a new form, you should save it with a descriptive name. This makes it easier to locate forms and units when you need to make modifications. Remember, you can use long filenames when naming your units. <HR></P> <P><HR><strong>NOTE:</strong> When writing a technical book, a difficult situation often arises. I want to use meaningful examples to reinforce the presentation of information. In order to write those examples, I have to use techniques or methods that I haven't talked about yet. But I can't talk about those methods until I've given you some good, meaningful examples. But I can't...well, you see my dilemma. So I'm going to digress a little here and talk about the main menu, toolbar, and Component palette. As you read the next section, remember that I'm off on a tangent, but for a good reason. <HR></BLOCKQUOTE><H2></H2><H2><A NAME="Heading5"></A>The Delphi Main Menu and Toolbar</H2><P>The Delphi main menu has all the choices necessary to make Delphi work. Becauseprogramming in Delphi is a highly visual operation, you might not use the main menuas much as you might with other programming environments. Still, just about anythingyou need is available from the main menu if you prefer to work that way. I'm notgoing to go over every item on the main menu here because you will encounter eachitem as you work through the next several chapters.</P><P>The Delphi toolbars provide a convenient way of accomplishing often-repeated tasks.A button is easier to locate than a menu item, not to mention that it requires lessmouse movement. The Delphi main window toolbars are illustrated in Figure 4.3. (TheComponent palette has been removed for clarity.)</P><P><A HREF="javascript:popUp('28670403.gif')"><B>FIGURE 4.3.</B></A><B> </B><I>TheDelphi main window toolbars.</I></P><P>If you are like me, you often forget to use the toolbar. But I'm telling you:Don't forget to learn and use the toolbar! As the old saying goes, "Do as Isay, not as I do." If you take the time to learn the toolbar, it will save youtime and make you more efficient in the long run. One of the reasons you bought Delphiwas to produce Windows applications quickly, so you might as well make the most ofit.</P><P>The Delphi toolbar is fully customizable. You can place the toolbars anywhereyou want on the main window. You can rearrange the placement of the menu, the toolbars,or the Component palette to suit the way you work.</P><P>Customizing the toolbars is remarkably easy. Delphi enables you to add buttonsto the toolbar, remove buttons, and rearrange buttons however you see fit. To configurea toolbar, right-click on the toolbar to display the context menu. Choose Customizefrom the context menu. When you choose this menu item, the Customize dialog box isdisplayed.</P><P>The Customize dialog box contains three tabs:</P><UL> <LI>The first tab, Toolbars, shows you the toolbars available with a check mark next to toolbars that are currently visible. You can add or remove existing toolbars or reset the toolbars to their original default settings. <P> <LI>The second tab, labeled Commands, shows all the available toolbar buttons. To add a button to the toolbar, just locate its description in the Commands list box and drag it to the place you want it to occupy on any toolbar. To remove a button from a toolbar, grab it and drag it off the toolbar. It's as simple as that. Figure 4.4 shows the act of adding a button to a toolbar. If you really make a mess of things, simply go back to the Toolbars page and click the Reset button. The toolbar will revert to its default settings.</UL><P><A HREF="javascript:popUp('tyd0404.gif')"><B>FIGURE 4.4.</B></A><B> </B><I>Customizingthe toolbar.</I><UL> <LI>The third tab, Options, contains options such as whether the tooltips are displayed and how they are displayed.</UL><P>Feel free to customize the Delphi IDE any way you like. It's your developmentenvironment, so make it work for you.</P><P><H2><A NAME="Heading6"></A>Using the Component Palette</H2><P>The Delphi Component palette is used to select a component or other control (suchas an ActiveX control) in order to place that control on a form. The Component paletteis a multipage window. Tabs are provided to enable you to navigate between pages.Clicking on a tab displays the available components or controls on that page.</P><P>Placing a component on a form is a two-step process. First, go to the Componentpalette and select the button representing the component you want to use. Then clickon the form to place the component on the form. The component appears with its upper-leftcorner placed where you clicked with the mouse.</P><P>You have already seen the Component palette's basic operations, but it has a coupleof other features that you haven't seen yet. The following sections explain thesefeatures.</P><P><H3><A NAME="Heading7"></A>Placing Multiple Copies of a Component</H3><P>So far you have placed only one component at a time on a form. You can easilyplace multiple components of the same type without selecting the component from theComponent palette each time. To place multiple components on a form, press and holdthe Shift key as you select the component from the Component palette. After you selectthe component, you can release the Shift key.</P><P>The component's button on the Component palette will appear pressed and will behighlighted with a blue border. Click on the form to place the first component. Noticethat the button stays pressed on the Component palette. You can click as many timesas you like; a new component will be placed each time you click the form. To stopplacing components, click the selector button on the Component palette (the arrowbutton). The component button pops up to indicate that you are done placing components.</P><P>Seeing is believing, so follow these steps:</P><DL> <DT></DT> <DD><B>1. </B>Create a new project. <P> <DT></DT> <DD><B>2. </B>Press and hold the Shift key on the keyboard and click the Label component button in the Component palette. <P> <DT></DT> <DD><B>3. </B>Click three times on the form, moving the cursor each time to indicate where you want the new component placed. A new Label is placed on the form each time you click. <P> <DT></DT> <DD><B>4. </B>Click the arrow button on the Component palette to end the process and return to form design mode. <P>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?