ch04.htm

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

HTM
1,371
字号
	<DD><B>4. </B>Click the Load button. In the File Open dialog box, navigate to the	\Borland Shared Files\Images\Icons directory and choose an icon from the icon files	listed. Click Open. The icon you selected is displayed in the Picture Editor window.	Click OK to close the Picture Editor. The icon is displayed on the form. Note that	the Image component has sized itself to the size of the icon.	<P>	<DT></DT>	<DD><B>5. </B>Position the icon as desired.	<P></DL><P>At this point you need an OK button on the form. Let's branch out a little andtake a look at a new component:</P><DL>	<DT></DT>	<DD><B>1. </B>If you're not already there, click the Additional tab on the Component	palette. Select the BitBtn component and place a BitBtn on the form near the bottom	and center it horizontally.	<P>	<DT></DT>	<DD><B>2. </B>Locate the Kind property and change it to bkOK. Notice that a green	check mark has appeared on the button and the Caption property has changed to OK.	That's all you have to do with the button. The BitBtn component already includes	code to close the form when the OK button is clicked.	<P></DL><P>Let's add one final touch to the About box:</P><DL>	<DT></DT>	<DD><B>1. </B>Locate the Bevel button (on the Additional tab in the Component palette)	and click it.	<P>	<DT></DT>	<DD><B>2. </B>Move to the form, but rather than clicking on it, drag a box around	the three text labels. The Bevel component appears when you stop dragging. If you	didn't get it quite right, you can resize or reposition the component.	<P>	<DT></DT>	<DD><B>3. </B>Locate the Shape property and change it to bsFrame. You now have a	3D frame around the static text.	<P></DL><P>Your form should now look something like the one shown in Figure 4.12. Save theunit (File | Save) and give it the name About.</P><P><A HREF="javascript:popUp('28670412.gif')"><B>FIGURE 4.12.</B></A><B> </B><I>Thefinished About box.</I></P><P>Are you ready to compile and run the program? Not yet. You need to add the Aboutunit to the main form's uses list. Perform these steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Switch to the Code Editor (press F12) and select the Main.pas tab.	<P>	<DT></DT>	<DD><B>2. </B>Choose File | Use Unit from the main menu.	<P>	<DT></DT>	<DD><B>3. </B>Choose the About unit from the Use Unit dialog box and click OK.	<P></DL><P>Now you're ready to run the program, so click the Run button. When the programruns, click the About button, and the About dialog box is displayed. Note that thedialog box is modal (you can't go back to the main window while the dialog box isdisplayed) and that it cannot be sized. The About form behaves in every way likea regular Windows dialog box.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> The common dialog box classes (TOpenDialog, TSaveDialog, TFontDialog,	and so on) do not represent dialog boxes created as Delphi forms. Windows provides	these dialog boxes as a set of common dialog boxes that all Windows applications	can use (the actual dialog boxes are contained in a file called COMDLG32.DLL). The	VCL dialog box classes encapsulate the common dialog boxes to make using them easier.	<I>Encapsulate</I> in this sense means to take a common dialog box's functionality	and roll it into a VCL component. The component makes it much easier to use the dialog	box than it would otherwise be. <HR></P>	<P><HR><strong>NOTE:</strong> Delphi includes several prebuilt forms that you can choose from to	help you build dialog boxes as quickly as possible. I'll discuss those on Day 8.	<HR></BLOCKQUOTE><P><H3><A NAME="Heading18"></A>Secondary Windows Versus Dialog Boxes</H3><P>A <I>secondary window</I> is a form that you display from your main window. Sowhen is a form a secondary window and when it is a dialog box? When it really comesdown to it, there is no difference between a secondary window and a dialog box inDelphi. You might have windows that resemble dialog boxes, and you might have otherwindows that resemble a traditional window.</P><P>In the grand scheme of things, they all are forms and it doesn't make much senseto differentiate between the terms <I>dialog box</I> and <I>secondary form</I>. It'sall the same in the end. In traditional programming environments, you have to specificallycreate a dialog box or specifically create a secondary window in an application.Delphi frees you from that restriction and enables you to treat both dialog boxesand windows exactly the same.</P><P><H3><A NAME="Heading19"></A>The Multiple Document Interface Model</H3><P>So far, you have built only single document interface (SDI) applications. An <I>SDIapplication</I> has a single main window and typically displays dialog boxes as needed,but does not otherwise display child windows.</P><P><strong>New Term:</strong> Some programs follow the multiple document interface (MDI)model. <I>MDI applications</I> consist of a main window (the MDI parent) and childwindows (the MDI children).</P><P>Examples of programs that use the MDI model are the Windows System ConfigurationEditor (SYSEDIT) and the Windows 3.1 Program Manager.</P><P>One of the most obvious characteristics of the MDI model is that the MDI childwindows are confined to the parent. You can drag the child windows within the parentwindow, but you cannot drag them outside the parent. MDI applications almost alwayshave a Window item on their main menu. This menu usually contains items named Cascadeand Tile, which enable you to display the MDI child windows in either a cascadedor tiled arrangement. When an MDI child is minimized, its icon is contained withinthe MDI parent's frame. When a regular (non-MDI) child window is minimized, its iconis placed on the Windows desktop.</P><P>To create an MDI application in Delphi, you must set the main form's FormStyleproperty to fsMDIForm. Each of the MDI child windows must have the FormStyle propertyset to fsMDIChild. Aside from that restriction, there is very little to creatingan MDI application in Delphi. You simply create the main window form and one or moreforms to be used as child windows, and you're off and running.</P><P><H3><A NAME="Heading20"></A>Key Properties for Forms</H3><P>The TForm class has a lot of properties. Some of these properties are obscureand rarely used; others are widely used. I'll touch on the most widely used propertieshere. I won't include obvious properties such as Caption, Color, Left, Top, Width,and Height unless they have a particular feature you should be aware of.</P><P><H4>Design-Time Properties</H4><P>The properties outlined in this section can be set at design time and also atruntime. Almost all these properties can be read at runtime as well.</P><P><B>ActiveControl&#160;&#160;</B>The ActiveControl property is used to set thecontrol that will have focus when the form is activated. For example, you might wanta particular edit control to have focus when a dialog box form is displayed. At designtime, the Value column for the ActiveControl property contains a list of componentson the form. You can choose one of the components from this list to make that componentthe active control when the form is first displayed.</P><P><B>AutoScroll, HorzScrollBar, and VertScrollBar&#160;&#160;</B>Together, the AutoScroll,HorzScrollBar, and VertScrollBar properties control the scrollbars for a form. IfAutoScroll is set to True (the default), scrollbars automatically appear when theform is too small to display all its components. The HorzScrollBar and VertScrollBarproperties each have several properties of their own that control the scrollbar operations.</P><P><B>BorderIcons&#160;&#160;</B>The BorderIcons property controls which system buttonswill appear on the form at runtime. Choices include the system menu, the minimizebutton, the maximize button, and the help button.</P><P><B>BorderStyle&#160;&#160;</B>The BorderStyle property indicates what type ofborder the form will have. The default value is bsSizeable, which creates a windowthat can be sized. Non-sizable styles include bsDialog and bsNone.</P><P><B>ClientWidth and ClientHeight&#160;&#160;</B>You can specify the client areawidth and height rather than the full form's width and height by using the ClientWidthand ClientHeight properties. (The <I>client area</I> of the form is the area insideof the borders and below the title bar and menu bar.) Use these properties when youwant the client area to be a specific size and the rest of the window to adjust asnecessary. Setting the ClientWidth and ClientHeight properties makes automatic changesto the Width and Height properties.</P><P><B>Constraints&#160;&#160;</B>The Constraints property is used to set the maximumand minimum width and height of the form. Simply set the MaxHeight, MaxWidth, MinHeight,and MinWidth values as desired and the form will conform to those constraints.</P><P><B>DefaultMonitor&#160;&#160;</B>The DefaultMonitor property determines whichmonitor the form will appear on in a multimonitor environment (such as Windows 98).</P><P><B>DockSite&#160;&#160;</B>The DockSite property determines whether the form willact as a dock site for dockable components. Dock sites and dockable components arediscussed on Day 13, &quot;Beyond the Basics.&quot;</P><P><B>Font&#160;&#160;</B>The Font property specifies the font that the form uses.The important issue to understand here is that the form's font is inherited by anycomponents placed on the form. This also means that you can change the font usedby all components at one time by changing just the form's font. If an individualcontrol's font has been manually changed, that control's font will not be changedwhen the main form's font changes.</P><P><B>FormStyle&#160;&#160;</B>This property is usually set to fsNormal. If you wanta form to always be on top, use the fsStayOnTop style. MDI forms should use the fsMDIFormstyle and MDI child forms should use the fsMDIChild style. MDI forms and MDI childwindows were discussed earlier in this chapter in the section &quot;The MultipleDocument Interface Model.&quot;</P><P><B>HelpContext and HelpFile&#160;&#160;</B>The HelpContext property is used toset the help context ID for a form. If context help is enabled for a form, the WindowsHelp system will activate when the F1 key is pressed. The context ID is used to tellthe Help system which page in the help file to display. The HelpFile property isthe name of the help file that will be used when F1 is pressed.</P><P><B>Icon&#160;&#160;</B>The Icon property sets the icon that is used on the titlebar for the form when the form is displayed at runtime and also when the form isminimized. In some cases, setting this property has no effect. For example, whenthe FormStyle is set to fsDialog, the Icon property is ignored.</P><P><B>KeyPreview&#160;&#160;</B>When KeyPreview is True, the form's OnKeyPress andOnKeyDown events will be generated when a key is pressed in any component on theform. Ordinarily, forms don't receive keyboard events when a component on the formhas focus.</P><P><B>Position&#160;&#160;</B>The Position property determines the size and positionof the form when the form is initially displayed. The three basic choices are poDesigned,poDefault, and poScreenCenter:</P><UL>	<LI>poDesigned causes the form to be displayed in the exact position it was in when	it was designed.	<P>	<LI>poDefault enables Windows to set the size and position according to the usual	Windows Z-ordering algorithm. (Z-ordering is what Windows uses to decide where it	displays a new window on the screen. If the new window does not have specific placement	information, it will be displayed just below and to the right of the last window	displayed on the screen.)	<P>	<LI>The poScreenCenter option causes the form to be displayed in the center of the	screen each time it is shown.</UL><P><B>Visible&#160;&#160;</B>The Visible property controls whether the form is initiallyvisible. This property is not particularly useful at design time, but at runtimeit can be read to determine whether the form is currently visible. It can also beused to hide or display the form.</P><P><B>WindowState&#160;&#160;</B>The WindowState property can be read to determinethe form's current state (maximized, minimized, or normal). It can also be used toindicate how the form should initially be displayed. Choices are wsMinimized, wsMaximized,and wsNormal.</P><P><H4>Runtime-Only Properties</H4><P>Some properties can be accessed only at runtime through code. The following arethe most commonly used runtime properties.</P><P><B>ActiveMDIChild&#160;&#160;</B>When read, the ActiveMDIChild property returnsa pointer to the currently active MDI child window. This property is read only. Ifno MDI child is currently active or if the application is not an MDI application,ActiveMDIChild returns nil.</P><P><B>Canvas&#160;&#160;</B>The form's canvas represents the drawing surface of theform. The Canvas property gives you access to the form's canvas. By using the Canvasproperty, you can draw bitmaps, lines, shapes, or text on the form at runtime. Mostof the time you will use a Label component to draw text on a form, an Image componentto display graphics, and a Shape component to draw shapes. However, there are timeswhen you need to draw on the canvas at runtime and the Canvas property enables youto do that. The Canvas property can also be used to save an image of the form todisk. Canvases are discussed in more detail on Day 12, &quot;Graphics and MultimediaProgramming.&quot;</P><P><B>ClientRect&#160;&#160;</B>The ClientRect property contains the top, left, right,and bottom coordinates of the client area of the form. This is useful in a varietyof programming situations. For example, you might need to know the client area'swidth and height in order to place a bitmap on the center of the form.</P><P><B>Handle&#160;&#160;</B>The Handle property returns the window handle (HWND)of the form. Use this property when you need the window handle to pass to a WindowsAPI function.</P><P><B>ModalResult&#160;&#160;</B>The ModalResult property is used to indicate howa modal form was closed. If you have a dialog box that has OK and Cancel buttons,you can set ModalResult to mrOK when the user clicks the OK button and to mrCancelwhen the user clicks the Cancel button. The calling form can then read ModalResultto see which button was clicked to close the form. Other possibilities include mrYes,mrNo, and mrAbort.</P><P><B>Owner&#160;&#160;</B>The Owner property is a pointer to the owner of the form.The owner of the form is the object that is responsible for deleting the form whenthe form is no longer needed. The parent of a component, on the other hand, is thewindow (a form or another component) that acts as the container for the component.In the case of a main form, the application object is both the owner of the formand the parent of the form. In the case of components, the owner would be the form,but the parent could be another component such as a panel.</P><P><B>Parent&#160;&#160;</B>The Parent property is a pointer to the parent of theform. See the previous section about Owner for an explanation of Owner versus Parent.</P><P><H3><A NAME="Heading21"></A>Form Methods</H3><P>Forms are components, too. As such, forms have many methods in common with components.Common methods include Show, ShowModal, and Invalidate, to name just a few. Thereare some methods, however, that are specific to forms. As before, I'll discuss onlythe most commonly used methods.</P><P><H4>BringToFront</H4><P>The B

⌨️ 快捷键说明

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