📄 ch14.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 14 -- Advanced Programming</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1></CENTER><CENTER><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch13/ch13.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch15/ch15.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>- 14 -</H1></CENTER><CENTER><H1>Advanced Programming</H1></CENTER><UL> <LI><A HREF="#Heading1">Implementing Context-Sensitive Help</A> <UL> <LI><A HREF="#Heading2">Writing the Help File</A> <LI><A HREF="#Heading3">Context Identifiers and the HelpContext Property</A> <LI><A HREF="#Heading4">Implementing Context-Sensitive Help</A> <LI><A HREF="#Heading5">Adding Menu Support for the Help File</A> <LI><A HREF="#Heading6">Using Help Include Files</A> <LI><A HREF="#Heading7">Putting It to Work</A> </UL> <LI><A HREF="#Heading8">Checking Errors with Exception Handling</A> <UL> <LI><A HREF="#Heading9">Exception-Handling Keywords: try, except, finally, and raise</A> <LI><A HREF="#Heading10">Raising Exceptions</A> <LI><A HREF="#Heading11">Using finally</A> <LI><A HREF="#Heading12">Catching Unhandled Exceptions at the <BR> Application Level</A> <LI><A HREF="#Heading13">Debugging and Exception Handling</A> </UL> <LI><A HREF="#Heading14">Using the Registry</A> <UL> <LI><A HREF="#Heading15">Registry Keys</A> <LI><A HREF="#Heading16">Registry Data Types</A> <LI><A HREF="#Heading17">The TRegistry Class</A> <LI><A HREF="#Heading18">Using TRegistry</A> </UL> <LI><A HREF="#Heading19">Implementing Specialized Message <BR> Handling</A> <UL> <LI><A HREF="#Heading20">More on Windows Messages</A> <LI><A HREF="#Heading21">Sending Versus Posting</A> <LI><A HREF="#Heading22">Handling Events</A> <LI><A HREF="#Heading23">Handling Other Windows Messages</A> <LI><A HREF="#Heading24">User-Defined Messages</A> </UL> <LI><A HREF="#Heading25">Summary</A> <LI><A HREF="#Heading26">Workshop</A> <UL> <LI><A HREF="#Heading27">Q&A</A> <LI><A HREF="#Heading28">Quiz</A> <LI><A HREF="#Heading29">Exercises</A> </UL> <LI><A HREF="#InReview">Review</A></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>Today you get into some of the more advanced aspects of Windows programming withDelphi. Specifically, you learn about</P><UL> <LI>Implementing context-sensitive help <P> <LI>Exception handling <P> <LI>Using the Windows Registry <P> <LI>Specialized message handling</UL><P>You have a lot to do today, so let's get right to it.</P><P><H2><A NAME="Heading1"></A>Implementing Context-Sensitive Help</H2><P>Not too long ago, you might have been able to get by with not providing context-sensitivehelp in your applications. In fact, for small applications you might have been ableto get by without providing a help file at all. I wouldn't recommend that in today'smarket, though. Users are increasingly demanding when it comes to features. A helpfile is a no longer an option but a required component of a full-featured application.</P><P><I>Context-sensitive help</I> means that when the user presses the F1 key, a particularpage of the application's help file is displayed depending on the program's currentcontext. Take the Delphi IDE, for example. Let's say you have the Project Optionsdialog box open and have the Application page displayed. When you press F1 (or pressthe Help button on the dialog box), WinHelp runs and displays the help topic forthe Application page of the Project Options dialog box.</P><P>Likewise, if you press F1 when the Object Repository Options dialog box is onthe screen, you get help for that particular dialog box. Context-sensitive help workswith menu items as well. If you highlight a menu item and press F1, you are takento the help file page regarding that menu item.</P><P>To implement context-sensitive help in your application, you must perform thefollowing steps:</P><DL> <DT></DT> <DD><B>1. </B>Create a help file for your application. <P> <DT></DT> <DD><B>2. </B>Assign the name of your help file to the HelpFile property of the Application object. <P> <DT></DT> <DD><B>3. </B>Set the HelpContext property of any forms or components for which context-sensitive help is supported. <P> <DT></DT> <DD><B>4. </B>Create menu items for the Help menu so that your users can invoke help from the menu. <P></DL><P>Let's examine these steps one at a time.</P><P><H3><A NAME="Heading2"></A>Writing the Help File</H3><P>Writing help files is a chore. I don't exactly hate it, mind you, but it's notone of the tasks I look forward to. If you are fortunate, you have a documentationdepartment that writes the help files for you.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> Even some limited-budget programmers have documentation departments that write help files for them. Frequently they refer to the doc writers as "Honey" or "Dear." For example, a programmer might say to her documentation writer, "Honey, when you get done with that help page would you put the kids to bed?" <HR></BLOCKQUOTE><P>Regardless of who writes the help file, there must be some coordination betweenthe help-file writer and the programmer. The context identifiers in the help filemust match those specified for the components in the program itself. Although thisis not exactly a difficult task, coordination is still required so that everyoneis on the same page (pun intended).</P><P>A Windows help file might be constructed from several individual files. The sourcefor a Windows help file is called the <I>topic file</I>. The topic file is a richtext format (.rtf) file with lots of special codes that the help compiler understands.If your help file includes graphics, you might have one or more <I>graphics files</I>.Graphics files used in creating a help file include bitmap (.bmp), Windows metafile(.wmf), and some other specialized graphics files.</P><P>Finally, you have the help <I>project file</I> (.hpj). The project file containsa description of how the help compiler should go about merging the topic file, thegraphics files, and any other specialized files the target help file needs. It alsoincludes a [MAP] section that maps context ID numbers to particular help topics.After the project file is created, it is compiled with a help compiler such as theMicrosoft Help Workshop (you can find it in the \Delphi 4\Help\Tools directory).The Help Workshop takes the help project file and compiles it to produce the finalhelp file (.hlp).</P><BLOCKQUOTE> <P><HR><strong>TIP:</strong> Although you can write help files with any word processor that supports .rtf files, I strongly recommend buying a help-authoring program. A good help-authoring package can take much of the frustration out of writing help files. Commercial help authoring tools include ForeHelp by Fore <BR> Front, Inc. (<A target="_new" HREF="http://www.ff.com">http://www.ff.com</A>) and RoboHelp by Blue Sky Software (<A target="_new" HREF="http://www.blue-sky.com">http://www.blue-sky.com</A>). I have used ForeHelp and, for the most part, I like the way it works. There are also some good shareware help authoring tools out there. One such shareware help authoring program is HelpScribble. You can check out HelpScribble at http://www.ping.be/jg/. <HR></BLOCKQUOTE><H3><A NAME="Heading3"></A>Context Identifiers and the HelpContext Property</H3><P>Regardless of what method you use to create the help file, you need to have acontext number associated with each major topic in the help file. The <I>contextnumber</I> is used by the Windows help system, WinHelp32.exe, to display a particularpage in the help file.</P><P>For example, let's say you have an Options dialog box in your application. Whenthe user presses F1 with the Options dialog box open, your program will pass thecontext ID for that dialog box to WinHelp. WinHelp will run and display the pagein the help file that explains the application's Options dialog box. You don't needa context ID for every page in the help file, but you should have a help contextID for your main topic pages, dialog boxes, and other major components of your application.</P><P>Most components (forms, menu items, and controls) have a property called HelpContext.This property contains the context ID that will be passed to WinHelp if the userpresses the F1 key when that component has focus. By default, the HelpContext propertyis set to 0. If the HelpContext property is 0 for a component, the component willinherit the HelpContext value of its parent window. This enables you to set the HelpContextfor a form and then, no matter which component on the form has input focus, the helpcontext for the form will be used when F1 is pressed.</P><BLOCKQUOTE> <P><HR><strong>NOTE:</strong> You might have noticed that the SpeedButton and ToolButton components do not have a HelpContext property. Because these components are non-windowed controls, they can never gain input focus. Therefore, the F1 key cannot be used with speed buttons and toolbar buttons. <HR></BLOCKQUOTE><P>At a minimum, you should provide context-sensitive help for each form in yourapplication (a form being a dialog box or secondary window). Ultimately, it is upto you to decide which items to provide context-sensitive help for. If you are goingto err, you should probably err on the side of providing too much context-sensitivehelp (if there is such a thing) rather than not enough.</P><P><H3><A NAME="Heading4"></A>Implementing Context-Sensitive Help</H3><P>Implementing context-sensitive help in your Delphi applications is relativelyeasy. As I said earlier, the real work in adding context-sensitive help to your Delphiapplications is in the writing of the help file. The rest is easy in comparison.</P><P><H4>Setting the Help File</H4><P>Regardless of how you implement context-sensitive help, you first have to tellWindows the name of the help file for your application. To do that, you assign thehelp filename to the HelpFile property of the Application class. You can do thisin one of two ways. The easier way is at design time via the Project Options dialogbox.</P><P>On Day 9, "Projects, the Code Editor, and the Code Explorer," you learnedabout the project options. I discussed the fact that the Application page of theProject Options dialog box has a field called Help File that is used to specify thename of the help file for the application. Simply type the name of your help filein this field. VCL will assign the help file name to the HelpFile property, and thenthe application will use that filename each time help is requested.</P><P>You can also set the name of the help file at runtime. This might be necessaryif you enable your users to place the help file in a directory of their own choosing.You could store the location of the help file in the Windows Registry (the Registryis discussed later, in the section titled "Using the Registry") and thenassign the help file's path and filename to the Application object's HelpFile property.For example, part of your OnCreate event handler might look like this:</P><P><PRE>var Filename : string;begin Filename := GetHelpFileName; { user-defined function } Application.HelpFile := Filename;</PRE><P>Although it's not very common, you can change the HelpFile property at differentpoints in your program if you want. You might do this to switch between differenthelp files, for example. After you have the help filename set up, you can go on tothe actual implementation of the help system.</P><P><H4>Adding F1 Key Support</H4><P>To add F1 support for forms and components, all you have to do is set the HelpContextproperty to the matching context ID in the help file; VCL takes it from there. Besure that you have assigned a help filename to the HelpFile property of the Applicationclass and that the help file contains valid context identifiers.</P><P><H3><A NAME="Heading5"></A>Adding Menu Support for the Help File</H3><P>In addition to F1 key support, most applications have one or two menu items underthe Help menu (where else?) that can be used to start WinHelp. There will usuallybe a menu item called Contents. Choosing this item will display the contents pagefor the help file. In addition to a Contents menu item, some applications have aHelp menu item called Help Topics. Choosing this menu item displays the index forthe help file. (The index is created as part of the help file creation process.)</P><P>To implement these and other Help items, you have to do some programming. (It's
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -