📄 ch09.htm
字号:
<HR></BLOCKQUOTE><P>You can also open multiple files at one time. To open multiple files, choose thefiles you want to open in the Open dialog box and click OK. Each file selected willbe loaded, and a tab for each file will be placed at the top of the editor window.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> You can also use drag-and-drop to open files. For example, you can choose a file (or a group of files) in Explorer, drag it onto the Code Editor, and drop it. The file will be opened in the Code Editor. <HR></BLOCKQUOTE><P>To save a file, choose File|Save or File|Save As from the main menu or type Ctrl+Son the keyboard. If the file has not been previously saved, the Save As dialog boxwill appear, and you can enter a filename at that time.</P><P><H3><A NAME="Heading17"></A>Highlighting Text</H3><P>Although text highlighting is basic text-editor stuff, I thought it wouldn't hurtto remind you of a few basic highlighting techniques you can use in the Delphi CodeEditor.</P><P>To highlight a short block of text, you can use the mouse to drag across any textyou want to highlight. After you've selected the text, you can cut, copy, or pasteas needed. To highlight longer blocks of code, you can use the Click+Shift+Clickmethod. First, click at the beginning of the block you want to highlight. Next, holdthe Shift key on the keyboard and then click again at the end of the block. All textbetween the starting point and the ending point is highlighted.</P><P>Another useful feature is the capability to quickly select an individual word.To select a keyword, function name, or variable, just double-click on the word. Nowyou can perform any editing operations you want with the highlighted word.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> To select a single line of code with the mouse, click at the beginning of the line and drag straight down to the beginning of the next line. To highlight a single line of code with the keyboard, first press the Home key to move to the beginning of the line and then use Shift+down-arrow key to highlight the line. <HR></BLOCKQUOTE><P>Dozens of keyboard combinations can be used to highlight text and do other editingchores. For a complete list of all the keyboard shortcuts available, consult theDelphi online help.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> As you program you often add, delete, or move blocks of text. Sometimes you will need to indent an entire block of code. At other times you will need to un-indent (outdent?) an entire block of code. To indent a block of code, highlight the lines that you want to indent and then press Ctrl+Shift+I on the keyboard. The entire block will be indented. To un-indent a block of code, press Ctrl+Shift+U on the keyboard. <HR></BLOCKQUOTE><P>The Code Editor also supports drag-and-drop editing. To move a section of code,first highlight it. Next, place the mouse cursor over the highlighted text and drag.Drag until the cursor reaches the location where you want the code to be placed.Release the mouse button and the code will be moved to the new location. To copytext rather than move it, repeat the preceding steps but hold down the Ctrl key beforeyou drop the text.</P><P><H4>Undo</H4><P>The Code Editor has a virtually limitless number of undo levels (32,767 by default).Normally, you can only undo commands up to the last time you saved a file. By changingthe editor options, you will be able to undo past commands even after saving thefile. I'll talk about editor options and preferences later in the chapter in thesection titled "Changing the Editor Options."</P><P>In general, it pays to remember this simple maxim: "Undo is your friend."</P><P><H4>Find and Replace</H4><P>Find and Replace are used fairly heavily in programming. You might use Find tofind a specific piece of code or a specific variable in your code. You might useReplace to change a variable's name or to change the name of a method. The possibilitiesare endless.</P><P>The Delphi Find Text and Replace Text dialog boxes implement more or less standardfind-and-replace operations. To bring up the Find Text dialog box, choose Search|Findfrom the main menu or press Ctrl+F. Enter text in the Text to find field and clickOK or press Enter. If the text is found, it will be highlighted.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Text highlighted by the Find Text dialog box is not the same as text highlighted with the mouse. You will notice that searched text is highlighted in black whereas text selected with the mouse is highlighted in blue (assuming you haven't changed the editor defaults). Text highlighted after a search operation is not selected for editing, it's just marked so that you can see it better. <HR></BLOCKQUOTE><P>To invoke the Replace Text dialog box, choose Search|Replace from the menu orpress Ctrl+R. Figure 9.12 shows the Delphi Replace Text dialog box. With a few obviousexceptions, the Find Text dialog box contains the same options.</P><P>For the most part, the options on the Find Text and Replace Text dialog boxesdo exactly what they indicate. If you choose the Case Sensitive option, you musttype the search text exactly as it appears in the source file.</P><P><A HREF="javascript:popUp('28670912.gif')"><B>FIGURE 9.12.</B></A><B> </B><I>TheReplace Text dialog box.</I></P><P>Use the Whole Words Only option when you want to be sure that text for which youare searching is not part of a longer word or variable name. For example, let's sayyou want to replace Form with MyForm. In this case, you would want to search forwhole words only because the word Form might be used in other variable names (suchas TForm).</P><P>The Regular Expressions option requires explanation as well. When this optionis on, you can use special and wildcard characters when doing searches. The specialcharacters enable you to find elements such as the beginning of a line or the endof a line in your search strings. Wildcard characters work much like they do in directoryoperations. For a complete description of regular expressions, see the Delphi onlinehelp under the topic "Regular Expressions."</P><P>When replacing text, it is safest to leave the Prompt on Replace option on. Whenyou do a Replace All operation with this option on, the editor will highlight eachfound word and prompt you whether to replace it. It is easy to miscalculate the resultsof a Replace All operation, so always use Replace All with care. Even then, it stillpays to remember that maxim: "Undo is your friend."</P><P>The rest of the Find and Replace options are self-explanatory and therefore don'tneed additional mention.</P><P><H4>Find in Files</H4><P>Find in Files is a great tool for searching for text in multiple files. I frequentlyuse Find in Files to search the VCL source code for particular methods, variables,or classes. This tool is useful and convenient, and you should learn how to use it.</P><P>To display the Find Text dialog box, you can choose Search|Find in Files fromthe main menu. Perhaps an easier way is to type Ctrl+F on the keyboard to bring upthe Find Text dialog box and then click the Find in Files tab. Figure 9.13 showsthe Find Text dialog box with the Find in Files tab showing.</P><P><A HREF="javascript:popUp('28670913.gif')"><B>FIGURE 9.13.</B></A><B> </B><I>TheFind Text dialog box.</I></P><P>Find in Files uses some of the same search options as the regular Find operation(case-sensitive, whole word only, and regular expressions). In addition, you havethe option to search all files in the project, all open files, or files in a particulardirectory, including subdirectories.</P><P>When you start Find in Files, a small window with a title of Searching appearsin the lower-right portion of your screen. This window shows the status of the Findin Files operation. It will show you the current file being searched and the numberof matches up to this point. To cancel the search, just close the Searching window.</P><P>Any matches are reported in the Code Editor's message window. The message windowshows the filename of the file in which the text was found, the line number wherethe text was found, and the line containing the search text with the search textdisplayed in bold. To view the file that contains a match, just double-click a linein the message window. Delphi will open the appropriate file and display the exactline containing the text for which you are searching. Figure 9.14 shows Delphi searchinga set of files.</P><P>When specifying the file mask, all the usual wildcard characters apply. For example,if you want to search all text files in a directory, you would enter the followingin the File masks field:</P><P><PRE>c:\MyStuff\*.txt</PRE><P>Find in Files is an indispensable tool. I find myself using it all the time. Learningto use Find in Files will save you a lot of time.</P><P><A HREF="javascript:popUp('28670914.gif')"><B>FIGURE 9.14.</B></A><B> </B><I>Delphisearching for text.</I><H4>Getting Help</H4><P>One of the most useful features of the Code Editor is its integration with theDelphi help system. Just place the editor cursor over an Object Pascal keyword, aVCL property or method, or any other Delphi-specific text and press F1. If a helptopic for the text under the cursor exists in the Delphi help files, Windows Helpwill run with the appropriate page showing. If no help topic exists for the selectedtext, an error message will be displayed.</P><P>This feature is extremely useful when you can't remember how to use a particularaspect of Delphi, Object Pascal, or VCL. Help, as they say, is just a keystroke away.</P><P><H3><A NAME="Heading18"></A>Specialized Editor Features</H3><P>The Delphi Code Editor has a few features that are extremely useful when you arewriting a lot of code. They are explained in the following sections.</P><P><H4>Code Templates</H4><P>Code templates are another nice feature of the Delphi Code Editor. Code templatesenable you to insert any predefined code (or any text, for that matter) in your sourceunits. To use code templates, just type Ctrl+J on the keyboard while editing in theCode Editor. When you do, a list box will pop up giving you a list of templates fromwhich to choose. Choose a template from the list, press Enter, and the text correspondingto that code template will be inserted into your source code. Figure 9.15 shows thecode template list box as it appears when you type Ctrl+J.</P><P><A HREF="javascript:popUp('28670915.gif')"><B>FIGURE 9.15.</B></A><B> </B><I>TheDelphi code template list box.</I></P><P>You can add new templates or edit existing templates via the Code Insight pageof the Environment Options dialog box. Or, if you prefer, you can open the code templatefile with any text editor (such as the Delphi Code Editor) and edit the code templatesthere. The code template file is called DELPHI32.DCI and is located in the Delphi4\Bin directory.</P><P>Feel free to modify the code templates in any way you see fit. For example, Ihave modified my code template representing the for statement to look like this:</P><P><PRE>for I := 0 to Pred(|) do beginend;</PRE><P>Notice the pipe character (|) in the code snippet. The pipe in a code templateentry is a placeholder that determines where the cursor will be placed after thetext is inserted into your source code.</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> If you make a lot of modifications to the code template file, be sure to keep a backup of the file in a safe location. You will need a backup because the Delphi installation program will overwrite your modified DELPHI32.DCI file when you update or reinstall Delphi. <HR></BLOCKQUOTE><P>You can use code templates for more than just code. Here at TurboPower Softwareour source files always have a header at the top of the unit. It looks somethinglike this:</P><P><PRE>{*********************************************************};{* Filename and version *};{* Copyright (c) TurboPower Software 1998 *};{* All rights reserved. *};{*********************************************************};</PRE><P>Because most of this text stays constant, I have a template that quickly insertsthe header in any new source units I create. Use code templates for any text thatyou use frequently in your day-to-day programming.</P><P><H4>Code Parameters</H4><P>The code parameters feature of the Code Editor displays a tooltip that promptsyou with the needed parameters of a VCL method or Windows API function. With hundredsof VCL methods and Windows API functions, there is virtually no way to remember everyfunction's parameters. The code parameters feature saves you time by showing youthe parameters of a method as you type. For example, let's say you are calling theSetBounds method. When you type the opening parentheses, a hint window pops up asshown in Figure 9.16.</P><P><A HREF="javascript:popUp('28670916.gif')"><B>FIGURE 9.16.</B></A><B> </B><I>Codeparameters in action.</I></P><P>As you can see in Figure 9.16, each parameter is listed in the tooltip. The parameteryou need to type next is displayed in bold. As you type a parameter, the next parameteris displayed in bold. This continues until you type all the method's parameters.After you have typed all the required parameters, the code parameters tooltip disappears.The code parameters options are set on the Code Insight page of the Environment Optionsdialog boxes. I'll discuss that page later in the section "The Code InsightPage."</P><P><H4>Code Completion</H4><P>Code completion is another Code Editor feature that can save you development time.Type a class variable name followed by the dot operator, and the Code Editor willdisplay a list box with all of that class's properties and methods. If, for example,you have a memo component called Memo, you will type</P><P><PRE>Memo.</PRE><P>and pause for a moment. A list box pops up as shown in Figure 9.17.</P><P><A HREF="javascript:popUp('28670917.gif')"><B>FIGURE 9.17.</B></A><B> </B> <I>Codecompletion displaying the properties and methods of TMemo.</I></P><P>When you see the list box, you ca
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -