⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P><strong>New Term:</strong> A <I>prototype</I> is an application that has the appearanceof a working application but lacks full functionality, usually because it's in theearly stages of design.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> Delphi is perfect for quick prototyping of an application. You can	have the main screens and dialog boxes designed and displayed in much less time than	it would take with traditional Windows programming tools. That is not, however, to	say that Delphi is just for prototyping. Delphi is fully capable of handling all	your 32-bit Windows programming needs. <HR></P></BLOCKQUOTE><H3><A NAME="Heading13"></A>Step 1: Starting a New Application</H3><DL>	<DT></DT>	<DD><B>1. </B>Choose File | New Application from the main menu. If prompted to save	the current project, click No.	<P>	<DT></DT>	<DD><B>2. </B>The form is selected, so change the Name property to MainForm.	<P>	<DT></DT>	<DD><B>3. </B>Change the Caption to ScratchPad 1.0.	<P>	<DT></DT>	<DD><B>4. </B>Choose Project | Options from the main menu. Click on the Application	tab and enter ScratchPad 1.0 for the application's title. Click OK to close the Project	Options dialog box.	<P></DL><H3><A NAME="Heading14"></A>Step 2: Adding a Toolbar</H3><P>Most Windows applications these days have a toolbar. Building a toolbar requiresseveral steps in itself. I'm not quite ready to explain everything there is to knowabout toolbars in Delphi, so I'll save that for Day 13, &quot;Beyond the Basics.&quot;You'll add a toolbar to the ScratchPad program at that time. What you can do fornow, though, is add a toolbar that can be used as a placeholder for the real toolbarthat you add later. Follow these steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Click on the Win32 tab on the Component palette and choose the ToolBar	component (it's the third from the right).	<P>	<DT></DT>	<DD><B>2. </B>Click anywhere on the form to add the toolbar. Notice that the toolbar	automatically aligns itself to the top of the form.	<P>	<DD><B>3. </B>Right-click on the toolbar and choose New Button. A button appears	on the toolbar.	<DT></DT>	<DD><B>4. </B>Repeat step 3 to add a second button.	<P></DL><P>That's all you are going to do with the toolbar for now. As I said, you'll makea real toolbar for this program on Day 13.</P><P><H3><A NAME="Heading15"></A>Step 3: Adding a Status Bar</H3><P>Okay, so far, so good. Windows Notepad doesn't have a status bar (or a toolbar,for that matter), but you can put one in your application by following these steps:</P><DL>	<DT></DT>	<DD><B>1. </B>Click on the Win32 tab on the Component palette and choose the StatusBar	component.	<P>	<DT></DT>	<DD><B>2. </B>Click anywhere on the form. The status bar is automatically placed	at the bottom of the form. The status bar has a default Align value of alBottom.	<P>	<DT></DT>	<DD><B>3. </B>Change the Name property to StatusBar.	<P></DL><P>The form will now look like Figure 6.16.</P><P><A HREF="javascript:popUp('28670616.gif')"><B>FIGURE 6.16.</B></A><B> </B><I>TheScratchPad form up to this point.</I></P><P><H3><A NAME="Heading16"></A>Step 4: Adding the Memo Component</H3><P>You need some component in which to type text, so you can use a memo componentto add this feature (believe it or not, you're almost done with your prototype):</P><DL>	<DT></DT>	<DD><B>1. </B>Click on the Standard tab on the Component palette and choose a Memo	component. Place the memo anywhere on the form's client area.	<P>	<DT></DT>	<DD><B>2. </B>Change the Name property to Memo.	<P>	<DT></DT>	<DD><B>3. </B>Double-click the Value column next to the Lines property. The String	List Editor is displayed. Delete the word Memo and click OK.	<P>	<DT></DT>	<DD><B>4. </B>Change the Scrollbar property to ssVertical. (Initially, you want only	a vertical scrollbar on the memo.)	<P>	<DT></DT>	<DD><B>5. </B>Change the Name property of the Font property to Fixedsys. (Because	this is a Notepad copycat, you'll use the system font.)	<P>	<DT></DT>	<DD><B>6. </B>Change the Align property to alClient. The memo expands to fill the	client area between the toolbar and the status bar.	<P></DL><P>Stand back and admire your work. This is starting to look like a real application!If the form looks too large or too small, resize it by dragging the lower-right corner.It's your program, so make it look the way you want it to look.</P><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> Pressing the Esc key selects the parent of the control that currently	has the selection. For example, our form's client area is covered by components,	which makes it impossible to select the form itself. To make the form the active	component in the Object Inspector, select the memo component and then press the Esc	key on the keyboard. You can also choose the form from the Component Selector combo	box on the Object Inspector. <HR></BLOCKQUOTE><P>Notice that all the controls automatically resize themselves to retain their relationshipwith the parent window--the form, in this case. That is one of the main advantagesto the Align property. The form now looks like the one in Figure 6.17.</P><P><H3><A NAME="Heading17"></A>Running the Program</H3><P>You can now click the Run button to run the program. You can type text in thewindow's client area and you can press the toolbar buttons (although they don't doanything at this point). Keep in mind that this is a prototype and is mostly forshow right now. You add more to the program by the end of the day.</P><P><A HREF="javascript:popUp('28670617.gif')"><B>FIGURE 6.17.</B></A><B> </B><I>Thecompleted prototype.</I></P><P>You'd better save the project because you're going to use it later in the chapter.Choose File | Save All from the main menu. Save the main form's source unit as SPMainand the project as Scratch.</P><P><H2><A NAME="Heading18"></A>May I See a Menu, Please?</H2><P>Menus are a big part of most Windows applications. Some Windows programs don'thave menus, but the vast majority do. Delphi makes creating menus easy with the MenuDesigner. The Menu Designer has the following features:</P><UL>	<LI>It can create both main menus and pop-up menus (context menus).	<P>	<LI>It provides immediate access to the Code Editor to handle the OnClick events	for menu items.	<P>	<LI>It can insert menus from templates or from resource files.	<P>	<LI>It can save custom menus as templates.</UL><P>All the Menu Designer's commands are accessed via the Menu Designer context menuor by interacting with the Object Inspector. Figure 6.18 shows the Menu Designer'scontext menu.</P><P>For the most part, these menu items are self-explanatory, so I'm not going togo over each one. Rather, you can learn about them by working with them. To begin,let's add a main menu to the ScratchPad application you created earlier. After thatyou'll add a context menu.</P><P><A HREF="javascript:popUp('28670618.gif')"><B>FIGURE 6.18.</B></A><B> </B><I>TheMenu Designer's context menu.</I></P><P><H2><I></I></H2><H3><A NAME="Heading19"></A>Creating a Main Menu</H3><P>The Menu Designer enables you to quickly build any menu. The menu structure fora main menu consists of a MainMenu component, which is represented by the VCL classTMainMenu. Each item on the menu is a MenuItem component that is encapsulated inthe TMenuItem class. You don't need to be too concerned about the intricacies ofhow these classes work together because the Menu Designer makes creating menus easy.With that brief overview, let's add a main menu to the ScratchPad application.</P><P><H4>Adding a Main Menu to the Form</H4><P>The first thing you must do is add a MainMenu component to your form.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> By now you have had some experience with Delphi. From this point	on I will abbreviate some steps that you need to take to perform certain actions.	For example, from here on I'll say, &quot;Place a MainMenu component on the form&quot;	rather than &quot;Click on the Standard tab on the Component palette. Click the MainMenu	button and click on the form to place the component.&quot; Don't worry, I'll still	give plenty of details when new operations are introduced. <HR></BLOCKQUOTE><DL>	<DT></DT>	<P>	<DD><B>1. </B>Open the ScratchPad project you created earlier in the chapter.	<P>	<DT></DT>	<DD><B>2. </B>Place a MainMenu component on the form and change its Name property	to MainMenu. Notice that a MainMenu component has very few properties and no events.	All the menu's work is done by the individual menu items.	<P>	<DT></DT>	<DD><B>3. </B>Double-click on the MainMenu icon. The Menu Designer is displayed.	<P></DL><P>The Menu Designer looks like a blank form without grid points. The Menu Designercan be sized in any way you want. The size is just for your convenience and has nobearing on how the menu operates at runtime. At this point, the Menu Designer iswaiting for you to begin building the menu. After you have created your first menu,you will find that menu creation is easy and intuitive.</P><P><H4>Creating a Menu by Hand</H4><P>Although there are easier ways to create a File menu, you will create your firstmenu by hand. The Menu Designer always has a blank menu item that acts as a placeholderfor any new menu items you will create. When you first start the Menu Designer, theblank item is selected.</P><DL>	<DT></DT>	<DD><B>1. </B>Change the Name property to FileMenu.	<P>	<DT></DT>	<DD><B>2. </B>Click on the Caption property in the Object Inspector, type &amp;File,	and press Enter.	<P></DL><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> The ampersand (&amp;) is used to create the underlined character	for a menu item. The underlined character is the <I>accelerator</I> the user can	type in combination with the Alt key to navigate a menu using the keyboard. You can	put ampersands anywhere in the menu item's text. For instance, the customary text	string for the Exit menu item would be E&amp;xit so that the <I>x</I> is the accelerator.	All you have to do is provide the ampersands before the appropriate letter, Windows	will take it from there. <HR></BLOCKQUOTE><P>At this point, several things happen. First, the File menu shows up in the MenuDesigner. It also appears on the main form behind the Menu Designer. The other thingthat happens is that a new, blank placeholder is added below the File menu you justcreated (you'll have to click on the File menu in the Menu Designer to see the placeholder).In addition, a new pop-up placeholder is created to the right of the File menu. TheObject Inspector is displaying a blank MenuItem component, waiting for you to enterthe Caption and Name property values. Figure 6.19 shows the Menu Designer as it appearsat this point.</P><P><A HREF="javascript:popUp('28670619.gif')"><B>FIGURE 6.19.</B></A><B> </B><I>TheMenu Designer and Object Inspector after creating the File menu.</I></P><P>Let's continue with the creation of the menu:</P><DL>	<DT></DT>	<DD><B>1. </B>Change the Name property for the new item to FileNew.	<P>	<DT></DT>	<DD><B>2. </B>Change the Caption property to &amp;New and press Enter. Again, a blank	item is created in the Menu Designer.	<P>	<DT></DT>	<DD><B>3. </B>Repeat steps 1 and 2 and create menu items for Open, Save, and Save	As. If you need help on where to place the ampersand, refer to Figure 6.20. Don't	worry that you might not get it exactly right. You can always go back later and fix	any errors.	<P></DL><BLOCKQUOTE>	<P><HR><strong>TIP:</strong> Make your menus as standard as possible. Be sure that your accelerators	(the underlined characters) are the same as in other Windows programs. Also, remember	that an ellipsis (...) following a menu item's text is a visual cue to the user that	choosing the menu item will invoke a dialog box. <HR></BLOCKQUOTE><P>At this point, you need a menu separator.</P><P><strong>New Term:</strong> A <I>separator</I> is the horizontal line on a menu that separatesgroups of menu items.</P><P>Adding a separator is easy with the Delphi Menu Designer. All you have to do isput in a hyphen for the Caption property. Select the blank menu item under Save As,type a hyphen for the Caption, and press Enter. A separator is placed in the menu.Continue adding menu items until your menu looks like the one in Figure 6.20. Ifyou need to modify a menu item, just click on it and change properties in the ObjectInspector as needed.</P><P><A HREF="javascript:popUp('28670620.gif')"><B>FIGURE 6.20.</B></A><B> </B><I>TheMenu Designer with the finished File menu.</I></P><BLOCKQUOTE>	<P><HR><

⌨️ 快捷键说明

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