📄 ch06.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 6 -- Working with the Form Designer and the Menu Designer</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch05/ch05.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch07/ch07.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1>- 6 -</H1></CENTER><CENTER><H1>Working with the Form Designer and the Menu Designer</H1></CENTER><UL> <LI><A HREF="#Heading1">Working with the Form Designer</A> <UL> <LI><A HREF="#Heading2">The Form Designer's Context Menu</A> <LI><A HREF="#Heading3">Placing Components</A> <LI><A HREF="#Heading4">The Form Designer Grid</A> <LI><A HREF="#Heading5">Selecting Components</A> <LI><A HREF="#Heading6">Moving Components</A> <LI><A HREF="#Heading7">Preventing Components from Being Moved or Sized</A> <LI><A HREF="#Heading8">Ordering, Cutting, Copying, and Pasting Components</A> <LI><A HREF="#Heading9">Sizing Components</A> <LI><A HREF="#Heading10">Aligning Components</A> <LI><A HREF="#Heading11">Setting the Tab Order</A> </UL> <LI><A HREF="#Heading12">Building an Example Application</A> <UL> <LI><A HREF="#Heading13">Step 1: Starting a New Application</A> <LI><A HREF="#Heading14">Step 2: Adding a Toolbar</A> <LI><A HREF="#Heading15">Step 3: Adding a Status Bar</A> <LI><A HREF="#Heading16">Step 4: Adding the Memo Component</A> <LI><A HREF="#Heading17">Running the Program</A> </UL> <LI><A HREF="#Heading18">May I See a Menu, Please?</A> <UL> <LI><A HREF="#Heading19">Creating a Main Menu</A> <LI><A HREF="#Heading20">Writing the Code</A> <LI><A HREF="#Heading21">And Now, the Moment You've All Been Waiting For...</A> <LI><A HREF="#Heading22">Pop-Up Menus (Context Menus)</A> <LI><A HREF="#Heading23">Creating and Saving Menu Templates</A> </UL> <LI><A HREF="#Heading24">Summary</A> <LI><A HREF="#Heading25">Workshop</A> <UL> <LI><A HREF="#Heading26">Q&A</A> <LI><A HREF="#Heading27">Quiz</A> </UL> <LI><A HREF="#Heading28">Exercises</A></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>As you know by now, Delphi is heavily form-based, a model that takes maximum advantageof the visual programming environment. In this chapter you will explore</P><UL> <LI>The Form Designer <P> <LI>The Menu Designer</UL><P>To illustrate the use of the Form Designer, you will build an application thatapproximates the Windows Notepad program. Along the way you will gain valuable experienceworking with the Form Designer. Later in the chapter, you'll explore the Menu Designerin detail.</P><P>This chapter might seem elementary if you have used Delphi extensively. Even so,be sure to take a quick look to discover things previously unknown or to rediscoverthings you've forgotten. I'm willing to bet there is at least one thing in this chapterthat will be new to you.</P><P><H2><A NAME="Heading1"></A>Working with the Form Designer</H2><P>The Delphi Form Designer is a powerful visual programming tool. It enables youto place, select, move, resize, and align components and much more. The Form Designeralso enables you to size and position the form itself, add menus, and create specializeddialog boxes--everything you need to create the user interface to a typical Windowsprogram.</P><P>I'll examine each Form Designer feature in the following sections. As you read,I encourage you to stop and experiment any time you are curious about how somethingworks. Sometimes a few minutes playing around can teach you a technique that youwill use for a long time to come.</P><P><H3><A NAME="Heading2"></A>The Form Designer's Context Menu</H3><P>When you first start Delphi or when you create a new project, you are presentedwith a blank form in the Form Designer. The Form Designer, like most Delphi windows,has a context menu associated with it. Table 6.1 lists and describes each item onthe Form Designer context menu.</P><P><H4>TABLE 6.1. THE FORM DESIGNER'S CONTEXT MENU ITEMS.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Item</I></TD> <TD ALIGN="LEFT"><I>Description</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Align to Grid</TD> <TD ALIGN="LEFT">Aligns selected components to the Form Designer grid.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Bring to Front</TD> <TD ALIGN="LEFT">Brings selected components to the front of all other components.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Send to Back</TD> <TD ALIGN="LEFT">Sends selected components behind all other components.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Revert to Inherited</TD> <TD ALIGN="LEFT">Causes the selected control to revert to its original state when you are working with a form that you have inherited from the Object Repository. (Inheriting forms from the Object Repository is covered on Day 8, "Creating Applications in Delphi.")</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Align</TD> <TD ALIGN="LEFT">Displays the Alignment dialog box.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Size</TD> <TD ALIGN="LEFT">Displays the Size dialog box.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Scale</TD> <TD ALIGN="LEFT">Displays the Scale dialog box.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Tab Order</TD> <TD ALIGN="LEFT">Displays the Edit Tab Order dialog box.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Creation Order</TD> <TD ALIGN="LEFT">Displays the Creation Order dialog box.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Flip Children</TD> <TD ALIGN="LEFT">For non-English versions of Windows, this command changes the reading order of a component. Disabled for English versions of Windows.</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">Add to Repository</TD> <TD ALIGN="LEFT">Adds this form to the Object Repository. Custom forms can be saved to be used later. (The Object Repository is discussed on Day 8.)</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">View as Text</TD> <TD ALIGN="LEFT">Shows the form description as text in the Code Editor. You can edit the form's text version if you like. Choose View as Form from the Code Editor context menu to go back to the form. You can also use Alt+F12 to switch from the View as Text and View as Form options.</TD> </TR></TABLE><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Delphi creates a form file (DFM) for every form you create and places it in your project's directory. The form file is a binary resource file that can't be read by mere humans. When you choose the View as Text context menu item, Delphi converts the binary resource to readable form. When you switch back to the View as Form option, Delphi recompiles the form file to implement any changes you have made. <HR></BLOCKQUOTE><P>Most of the context menu options are discussed in the following sections. Othersare discussed in later chapters when you examine the particular aspect of Delphito which they pertain.</P><P><H3><A NAME="Heading3"></A>Placing Components</H3><P>Placing a component on a form is simple. All you have to do is select the componentyou want from the Component palette and click on the form to place the component.When you click on the form, the component's upper-left corner is placed at the locationyou clicked. Notice that when you click a button on the Component palette, the buttonappears as pressed. When you click on the form to place the component, the buttonon the Component palette pops up again to indicate that the action is complete.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> As you learned on Day 4, "The Delphi IDE Explored," to place a component on a form multiple times, press and hold Shift when you first select the component's button on the Component palette. Each time you click on the form, a new component will be added. Click the Arrow button on the Component palette to stop placing components. <HR></BLOCKQUOTE><P>Most components can be sized. You can place a component on a form and then sizeit, or you can size the component at the same time you place it on the form. To sizewhile placing the component, click on the form where you want the top-left cornerto be placed and then drag with the mouse until the component is the desired size.When you release the mouse, the component will be placed at the size you specified.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Not all components can be sized in this manner. Nonvisual components, for example, are represented on the form by an icon. Although you can click and drag to place a nonvisual component, the drag size will be ignored. Another example is a single-line edit component. The edit component can be placed by dragging, but only the drag width will be used. The drag height will be ignored because the height of a single-line edit component defaults to the height of a single-line edit control. <HR></P> <P><HR><strong>CAUTION:</strong> If you change your mind while placing the control via the dragging method, you can press the Esc key on the keyboard before you release the mouse button to cancel the operation. The component's button will still be pressed on the Component palette, however, so you might need to click the Arrow button to return to component-selection mode. <HR></BLOCKQUOTE><P>Placing components is simple enough that you don't need to spend much time onthe subject. You had some experience with placing components yesterday, so let'smove on to other things.</P><P><H3><A NAME="Heading4"></A>The Form Designer Grid</H3><P>The Form Designer has a built-in grid that aids in designing forms. By default,Delphi shows the grid. The grid size is initially set to eight pixels horizontallyand eight pixels vertically. When the Form Designer is set to display the grid, adot is placed at the intersection of each grid point. Components placed on a formwill snap to the nearest grid point. By<I> snap to</I> I mean that the component'stop-left corner will automatically jump to the nearest grid point. This is an advantagebecause you frequently want a group of controls to be aligned either on their left,right, top, or bottom edge. When the Snap to Grid option is on, you merely get closeenough to the correct location and the Form Designer automatically places your componentat the nearest grid point. This saves you time by sparing you from tweaking the individualcomponent's size or position on the form.</P><P>The grid settings can be modified via the Preferences page of the EnvironmentOptions dialog box. (I'll discuss the Environment Options in detail on Day 9, "Projects,the Code Editor, and the Code Explorer.") From here you can change the gridsize or turn off the Snap to Grid feature. You can also turn the grid display onor off. When the grid display is off, the grid is still active (assuming Snap toGrid is on), but the dots marking grid points are not drawn on the form.</P><P><H3><A NAME="Heading5"></A>Selecting Components</H3><P>After you place a component on a form, you often have to select the componentin order to modify it in some way. You might have to select a component to performone of the following actions:</P><UL> <LI>Move the component.<BR> <BR> <LI>Change the component's properties. <P> <LI>Align the component. <P> <LI>Size the component. <P> <LI>Cut or copy the component to the Clipboard. <P> <LI>Order the component (bring to front or send to back). <P> <LI>Delete the component.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -