ch04.htm

来自「delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具」· HTM 代码 · 共 1,371 行 · 第 1/5 页

HTM
1,371
字号
each unit as the unit is being compiled. If there are errors, the compile statusdialog box will report There are errors and will list the number of errors that weredetected as well as any warnings.</P><P>Figure 4.9 shows the compile status dialog box after detecting errors. Delphicompiles your projects so quickly that the compile status dialog box is not generallynecessary. In fact, the compile status dialog box will increase your compile timesbecause it takes time to display information in the compile status dialog box.</P><P><A HREF="javascript:popUp('28670409.gif')"><B>FIGURE 4.9.</B></A><B> </B><I>Thecompile status dialog box reporting errors.</I></P><P>Regardless of the method chosen to compile the project, if errors are detected,the Code Editor will come to the top and the message window at the bottom of theCode Editor will show a list of errors and warnings. The Code Editor highlights theline where the first error occurred. After a successful syntax check, compile, orbuild, you can immediately run the program via the Run button if you choose.</P><P><H2><A NAME="Heading14"></A>Compiling and Building Other Object Pascal Programs</H2><P>Delphi's strength is in its visual programming environment. That environment istied directly to VCL and cannot be separated from it. To get the most out of Delphi,you will most likely be writing applications based on VCL. There are times, however,when you might want to write other types of applications.</P><P><P><strong>New Term:</strong> A <I>dynamic link library</I> (DLL) is an external file thatcontains code that can be executed from a program or from another DLL.</P><PRE></PRE><P>Probably the most obvious type of &quot;other&quot; program you might want tobuild is a DLL. DLLs might seem a bit like black magic, but they are really not verycomplicated: They are simply bits of compiled code that you can call from your application.After you create the DLL, calling a function contained in a DLL is no different thancalling a function contained in your main program. That's a bit of an oversimplification,but it is still accurate. DLLs are discussed on Day 19, &quot;Creating and UsingDLLs.&quot;</P><P>Another type of application you might write with Delphi is a Win32 console application.</P><P><strong>New Term:</strong> A Win32 <I>console application</I> is a 32-bit program thatruns in a DOS box under Windows 95 or Windows NT.</P><P>Console applications are useful for small utility programs, servers such as TCP/IPservers or mail servers, and a whole host of other possibilities. Basically, anyapplication that does not require a graphical interface is a good candidate for aconsole application.</P><P><H2><A NAME="Heading15"></A>More About Delphi Forms</H2><P>Before I continue with the discussion about the Delphi IDE, I need to spend sometime explaining forms. You have seen several forms in action as you have worked throughthis book, and on Day 6 you are going to learn all about the Form Designer. Beforeyou get there, you need some more background information on forms, so I'll coverthat now.</P><P><H3><A NAME="Heading16"></A>Main Window Forms</H3><P>Forms are the main building block of a Delphi application. Every GUI applicationhas at least one form that serves as the main window. The main window form mightbe just a blank window, it might have controls on it, or it might have a bitmap displayedon it. In a typical Windows program, your main window would have a menu. It mightalso have decorations such as a toolbar or a status bar. Just about anything goeswhen creating the main window of your application. Each application is unique, andeach has different requirements.</P><P><H3><A NAME="Heading17"></A>Dialog Box Forms</H3><P>Forms are also used for dialog boxes. In fact, to the user there is no differencebetween a Delphi form acting as a dialog box and a true dialog box. (By &quot;truedialog box&quot; I mean a dialog box created the traditional way with a resourceeditor and a resource script file. This is how dialog boxes are created in some otherprogramming environments. Delphi doesn't use traditional dialog boxes, so you willlikely never have to deal with dialog boxes on this level.) Dialog boxes usuallyhave several traits that distinguish them from ordinary windows:</P><UL>	<LI>Dialog boxes are not usually sizable. They usually perform a specific function,	and sizing of the dialog box is neither useful nor desirable.	<P>	<LI>Dialog boxes almost always have an OK button. Some dialog boxes have a button	labeled Close that performs the same task. A simple dialog box such as an About dialog	box typically has only the OK button.	<P>	<LI>Dialog boxes can also have a Cancel button and a Help button.	<P>	<LI>Dialog boxes typically have only the system close button on the title bar. They	do not usually have minimize and maximize buttons.	<P>	<LI>Some dialog boxes are <I>tabbed dialog boxes</I> that display several tabs from	which the user can choose. When a tab is clicked, a different page of the dialog	box is displayed.	<P>	<LI>The Tab key can be used to move from one control to the next in most dialog boxes.</UL><P>There are certainly exceptions to every rule. Most dialog boxes have the usualcharacteristics, but some dialog boxes perform specialty tasks and as such departfrom the norm in one way or another.</P><P><H4>Dialogs the Old-Fashioned Way</H4><P>In a traditional Windows program (one written in Borland Pascal or using a frameworksuch as OWL), a dialog box is created with a dialog box editor. In most cases, thedialog box editor is a visual tool that works somewhat like the Delphi Form Designer.When the user is done designing the dialog box, the visual representation of thedialog box is converted into a dialog box definition in a resource script file. Toillustrate, take a look at the dialog box shown in Figure 4.10.</P><P><strong>New Term:</strong> A <I>resource script</I> is a text file that is later compiledinto a binary resource file by the resource compiler.</P><P><A HREF="javascript:popUp('28670410.gif')"><B>FIGURE 4.10.</B></A><B> </B><I>Atypical About dialog box.</I></P><P>Figure 4.10 shows a typical About dialog box. It contains the program name, thecopyright information, and the application's icon. The resource script definitionfor the dialog box is shown in Listing 4.1.</P><P><H4>LISTING 4.1. A DIALOG BOX RESOURCE DEFINITION.</H4><PRE> 1: IDD_ABOUT DIALOG 58, 53, 194, 119 2: STYLE DS_MODALFRAME or WS_POPUP | 3:   WS_VISIBLE or WS_CAPTION or WS_SYSMENU 4: CAPTION `About TMMPlayer Example Program' 5: FONT 8, `MS Sans Serif' 6: { 7:  DEFPUSHBUTTON `OK', IDOK, 72, 96, 50, 14 8:  CTEXT `TMMPlayer Example Program', -1, 48, 22, 128, 8 9:  CTEXT `Copyright &#169; 1996, by Kent Reisdorph', -1, 32, 47, 136, 810:  CTEXT `March 15, 1996', -1, 24, 59, 146, 811:  CONTROL `', 99, `button', BS_GROUPBOX |12:    WS_CHILD or WS_VISIBLE or WS_GROUP, 12, 4, 176, 7013:  CONTROL 1, 1, `static', SS_ICON |14:    SS_SUNKEN or WS_CHILD or WS_VISIBLE, 24, 17, 20, 20</PRE><P>15: }</P><P>The resource script contains information that Windows uses to build the dialogbox at runtime. This information includes the number and type of controls on thedialog box, their size, position, text, options, and so on. Of course, the resourcescript also includes the same type of information for the actual dialog box.</P><P>Some Windows programmers don't use a dialog box editor at all, but prefer to writethe dialog box definition from scratch with a text editor. Although I can't faultthose programmers for creating dialog boxes in that manner, I can say that for mostprogrammers to take that approach would be, er..., less than 100 percent efficient.It takes many times longer to create a dialog box in that manner as opposed to thevisual approach.</P><P>Usually, all the application's dialog box definitions are contained in a singleresource script file that has a filename extension of .rc. At some point in the program-creationprocess, the resource script is compiled into a .res file (the binary resource file),which then is linked to the .exe by the linker. At runtime the dialog box is displayedeither modally or modelessly depending on the dialog box's intended purpose. Whenthe dialog box is invoked, Windows loads the dialog box resource from the executablefile, builds the dialog box, and displays it.</P><P><H4>Dialog Boxes the Delphi Way</H4><P>In Delphi, dialog boxes are simply another form. You create a dialog box justlike you do a main window form or any other form. To prevent the dialog box frombeing sized, you can change the BorderStyle property to bsDialog or bsSingle. Ifyou use bsDialog, your dialog box will have only the close box button on the titlebar, which is traditional for dialog boxes. Other than that, you don't have to doanything special to get a form to behave like a dialog box. All Delphi forms havetabbing support built in. You can set the tab order by altering the TabOrder propertyof the individual controls on the dialog box.</P><P><strong>New Term:</strong> A <I>modal</I> dialog box is one that must be dismissed beforethe user can continue using the application. The main window of an application isdisabled while this type of dialog box is open. Most dialog boxes are modal.</P><P><strong>New Term:</strong> A <I>modeless</I> dialog box is one that allows the user tocontinue to work with the application while the dialog box is displayed. The Finddialog box in some word-processing programs is an example of a modeless dialog box.</P><P>A Delphi dialog box (any Delphi form, actually) is modal or modeless dependingon how it is displayed. To execute a modal dialog box, you call the ShowModal methodof TForm. To create a modeless dialog box, you call the Show method.</P><P><H4>Creating a Dialog Form</H4><P>You can now add an About box to the multiple-forms project you created earlier.If you don't have that project open, choose File | Open Project from the main menuor click the Open Project button on the toolbar and locate the file. (You shouldhave saved it with the project name of Multiple.)</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> Delphi keeps a list of the files and projects you have used most recently.	Choose File | Reopen to view the MRU (most recently used) list. The MRU list is divided	into two parts. The top part shows the projects you have used most recently, and	the bottom part shows the individual files that you have used most recently. Just	click on one of the items to reopen that project or file. <HR></BLOCKQUOTE><P>First you'll add a button to the form that displays the About dialog box:</P><DL>	<DT></DT>	<DD><B>1. </B>Bring the main form into view. Choose the Button component from the	Component palette and drop a button on the form.	<P>	<DT></DT>	<DD><B>2. </B>Arrange the two buttons that are now on the form to balance the look	of the form.	<P>	<DT></DT>	<DD><B>3. </B>Change the Name property of the new button to AboutButton and the Caption	property to About....	<P>	<DT></DT>	<DD><B>4. </B>Double-click the AboutButton you just created on the form. The Code	Editor is displayed with the cursor placed in the event-handler function. Add this	line of code at the cursor:	<P></DL><PRE>AboutBox.ShowModal;</PRE><P>You haven't actually created the About box yet, but when you do you'll name itAboutBox, so you know enough to type the code that will display the About box.</P><P>Now create the dialog box itself by following these steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Create a new form (click the New Form button on the toolbar). Size	the form to the size of a typical About box (roughly the same size as the form named	SecondForm you created earlier).	<P>	<DT></DT>	<DD><B>2. </B>Change the Name property to AboutBox and change the Caption property	to About This Program.	<P>	<DT></DT>	<DD><B>3. </B>Locate the BorderStyle property (it's just above Caption) and change	it to bsDialog.	<P>	<DT></DT>	<DD><B>4. </B>Now add three text labels to the box. Edit the labels so that the About	box resembles the one shown in Figure 4.11. (You can type any text you want, of course.)	You can leave the default names Delphi generates for the text labels' Name properties.	You aren't actually going to do anything with the Name property, so you don't need	a descriptive name.	<P></DL><P><A HREF="javascript:popUp('28670411.gif')"><B>FIGURE 4.11.</B></A><B> </B><I>TheAbout box with text labels added.</I></P><DL>	<DT><I></I></DT>	<P></DL><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> The copyright symbol (&quot;) has an ASCII value of 169 in most typefaces.	To create the copyright symbol, press and hold the Alt key and type the numbers 0169	on the numeric keypad (be sure Num Lock is on). When you let go of the Alt key, the	copyright symbol appears. You can insert the ASCII value of any character this way.	You must type all four numbers, though. For example, the ASCII value of a capital	A is 65. To insert an A, you have to hold down Alt and type 0065 on the numeric keypad.	<HR></BLOCKQUOTE><DL>	<DT></DT>	<P></DL><P>Next, you can add an icon to the About box:</P><DL>	<DT></DT>	<DD><B>1. </B>Click the Additional tab on the Component palette and choose the Image	component. Place the component to the left of the text on the form.	<P>	<DT></DT>	<DD><B>2. </B>Locate the AutoSize property for the Image component and change it	to True.	<P>	<DT></DT>	<DD><B>3. </B>Locate the Picture property and double-click the Value column. The	Picture Editor dialog box is displayed.	<P>	<DT></DT>

⌨️ 快捷键说明

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