📄 ch11.htm
字号:
<pre><font color="#008000"> WinHelp(HID_CENTERING);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>This single line of code fires up the Help system, passing it the Help topic <font color="#008000">ID HID_CENTERING</font>. For this to compile, that Help topic ID has to be known to the compiler, so in <font color="#008000">ShowString.h</font>, add
this line:</P>
<pre><font color="#008000">#define HID_CENTERING 0x01</font></pre>
<P>The Help topic IDs in the range <font color="#008000">0x0000</font> to <font color="#008000">0xFFFF</font> are reserved for user-defined Help topics, so <font color="#008000">0x01</font> is a fine choice. Now the C++ compiler is happy, but when this
runs, the call to <font color="#008000">WinHelp()</font> is not going to find the topic that explains centering. You need to add a <I>help mapping entry</I>. This should be done in a new file, named <font color="#008000">ShowStringx.hm</font> (the <font
color="#008000">x</font> is for extra, because extra help mapping entries are added here.) Choose <U>F</U>ile, <U>N</U>ew, select the Files tab, highlight Text File, fill in the file name as ShowStringx.hm, and click OK. In the new file, type in this
line:</P>
<pre><font color="#008000">HID_CENTERING 0x01</font></pre>
<P>Save the file. Next you need to edit the Help project file, <font color="#008000">ShowString.hpj</font>. If you double-click this from a folder such as Windows 95 Explorer, the Help Compiler opens it. In this case you actually want to edit it as text,
so you should open it with Developer Studio. In Developer Studio's Project Workspace Window, click the FileView tab and then open <font color="#008000">ShowString.hpj</font> by double-clicking it in the File View (and you wondered what the File View was
good for) and add this line at the very bottom:</P>
<pre><font color="#008000">#include <ShowStringX.hm></font></pre>
<P>Now both the Help system and the compiler know about this new Help topic ID. Later in this chapter, when you write the Help text, you add a section that explains centering and connect it to this Help topic ID.</P>
<P>The other common use of command Help is to add a Help button to a dialog box that gives an overview of the dialog box. This used to be standard behavior but is now recommended only for large dialog boxes, especially those with complex interactions
between the various controls. Simply follow the steps you followed to add the menu item <U>H</U>elp, <U>U</U>nderstanding Centering, but add a button rather than a menu item. Do not create a new .hm file; add the button's Help topic ID to <font
color="#008000">ShowStringX.hm</font>, which continues to grow in the next section.</P>
<H3><B>Programming for Context Help</B></H3>
<P>Your first task in arranging for context Help is to get a Question button onto the Options dialog box, since AppWizard already added one to the toolbar. Open the Options dialog box by double-clicking it in the Resource View, and then choose
<U>V</U>iew, <U>P</U>roperties. Click the Extended Styles tab, and then make sure that the Conte<U>x</U>t Help check box is selected, as shown in Figure 11.5.</P>
<A HREF="Mfigs05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch11/Mfigs05.gif"><b>Fig. 11.5</b></A>
<P><I>The Context Help check box on the Extended Styles tab of the Dialog Properties dialog box arranges for the </I><I>Question box on the Options dialog box of ShowString.</I></P>
<P>As mentioned earlier, there are two messages sent in context Help: <font color="#008000">WM_HELP</font> when a user clicks something while in What's This? mode, and <font color="#008000">WM_CONTEXTMENU</font> when a user right-clicks something. You
need to arrange for your dialog box class, <font color="#008000">COptionsDialog</font>, to catch these messages. You do so by adding entries outside the special ClassWizard comments. The message map in <font color="#008000">OptionsDialog.h</font> should
look like this:</P>
<pre><font color="#008000">// Generated message map functions</font></pre>
<pre><font color="#008000"> //{{AFX_MSG(COptionsDialog)</font></pre>
<pre><font color="#008000"> // NOTE: the ClassWizard will add member functions here</font></pre>
<pre><font color="#008000"> //}}AFX_MSG</font></pre>
<pre><font color="#008000"> afx_msg BOOL OnHelpInfo(HELPINFO* lpHelpInfo);</font></pre>
<pre><font color="#008000"> afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);</font></pre>
<pre><font color="#008000"> DECLARE_MESSAGE_MAP()</font></pre>
<P>The message map in <font color="#008000">OptionsDialog.cpp</font> should look like this:</P>
<pre><font color="#008000">BEGIN_MESSAGE_MAP(COptionsDialog, CDialog)</font></pre>
<pre><font color="#008000"> //{{AFX_MSG_MAP(COptionsDialog)</font></pre>
<pre><font color="#008000"> // NOTE: the ClassWizard will add message map macros here</font></pre>
<pre><font color="#008000"> //}}AFX_MSG_MAP</font></pre>
<pre><font color="#008000"> ON_WM_HELPINFO()</font></pre>
<pre><font color="#008000"> ON_WM_CONTEXTMENU()</font></pre>
<pre><font color="#008000">END_MESSAGE_MAP()</font></pre>
<P>These macros arrange for <font color="#008000">WM_HELP</font> to be caught by <font color="#008000">OnHelpInfo()</font>, and for <font color="#008000">WM_CONTEXTMENU</font> to be caught by <font color="#008000">OnContextMenu()</font>. The next step is
to write those functions. They both need to use a table to connect resource IDs to Help topic IDs. Add these lines at the beginning of <font color="#008000">OptionsDialog.cpp</font>, after the comment block that reads <font color="#008000">//
COptionsDialog </font><font color="#008000">dialog</font>:</P>
<pre><font color="#008000">static DWORD aHelpIDs[] =</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> IDC_OPTIONS_STRING, HIDD_OPTIONS_STRING,</font></pre>
<pre><font color="#008000"> IDC_OPTIONS_BLACK, HIDD_OPTIONS_BLACK,</font></pre>
<pre><font color="#008000"> IDC_OPTIONS_RED, HIDD_OPTIONS_RED,</font></pre>
<pre><font color="#008000"> IDC_OPTIONS_GREEN, HIDD_OPTIONS_GREEN,</font></pre>
<pre><font color="#008000"> IDC_OPTIONS_HORIZCENTER, HIDD_OPTIONS_HORIZCENTER,</font></pre>
<pre><font color="#008000"> IDC_OPTIONS_VERTCENTER, HIDD_OPTIONS_VERTCENTER,</font></pre>
<pre><font color="#008000"> IDOK, HIDD_OPTIONS_OK,</font></pre>
<pre><font color="#008000"> IDCANCEL, HIDD_OPTIONS_CANCEL,</font></pre>
<pre><font color="#008000"> 0, 0</font></pre>
<pre><font color="#008000">};</font></pre>
<P>The Help system uses this array (you pass the address to the <font color="#008000">WinHelp()</font> function) to connect resource IDs and Help topic IDs. The compiler, however, has never heard of <font color="#008000">HIDD_OPTIONS_STRING</font>, so add
these lines to <font color="#008000">OptionsDialog.h</font>, before the definition of the <font color="#008000">COptionsDialog</font> class:</P>
<pre><font color="#008000">#define HIDD_OPTIONS_STRING 2</font></pre>
<pre><font color="#008000">#define HIDD_OPTIONS_BLACK 3</font></pre>
<pre><font color="#008000">#define HIDD_OPTIONS_RED 4</font></pre>
<pre><font color="#008000">#define HIDD_OPTIONS_GREEN 5</font></pre>
<pre><font color="#008000">#define HIDD_OPTIONS_HORIZCENTER 6</font></pre>
<pre><font color="#008000">#define HIDD_OPTIONS_VERTCENTER 7</font></pre>
<pre><font color="#008000">#define HIDD_OPTIONS_OK 8</font></pre>
<pre><font color="#008000">#define HIDD_OPTIONS_CANCEL 9</font></pre>
<P>The numbers are chosen arbitrarily. Now, as before, the compiler is happy, since all these constants are defined, but the Help system doesn't know what's going on because these topics are not in the Help mapping file yet. So, add these lines to <font
color="#008000">ShowStringX.hm</font>:</P>
<pre><font color="#008000">HIDD_OPTIONS_STRING 0x02</font></pre>
<pre><font color="#008000">HIDD_OPTIONS_BLACK 0x03</font></pre>
<pre><font color="#008000">HIDD_OPTIONS_RED 0x04</font></pre>
<pre><font color="#008000">HIDD_OPTIONS_GREEN 0x05</font></pre>
<pre><font color="#008000">HIDD_OPTIONS_HORIZCENTER 0x06</font></pre>
<pre><font color="#008000">HIDD_OPTIONS_VERTCENTER 0x07</font></pre>
<pre><font color="#008000">HIDD_OPTIONS_OK 0x08</font></pre>
<pre><font color="#008000">HIDD_OPTIONS_CANCEL 0x09</font></pre>
<P>Be sure to use the same numbers as in the <font color="#008000">#define</font> statements in <font color="#008000">OptionsDialog.h</font>. The stage is set; all that remains is to write the functions. Here's what <font
color="#008000">OnHelpInfo()</font> looks like:</P>
<pre><font color="#008000">BOOL COptionsDialog::OnHelpInfo(HELPINFO *lpHelpInfo)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> if (lpHelpInfo->iContextType == HELPINFO_WINDOW) // must be for a control</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> // have to call SDK WinHelp not CWinApp::WinHelp</font></pre>
<pre><font color="#008000"> // because CWinApp::WinHelp doesn't take a</font></pre>
<pre><font color="#008000"> // handle as a parameter.</font></pre>
<pre><font color="#008000"> ::WinHelp((HWND)lpHelpInfo->hItemHandle, </font></pre>
<pre><font color="#008000"> AfxGetApp()->m_pszHelpFilePath, </font></pre>
<pre><font color="#008000"> HELP_WM_HELP, (DWORD)aHelpIDs);</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> return TRUE;</font></pre>
<pre><font color="#008000">}</font></pre>
<P>This function just calls the SDK WinHelp function and passes the handle to the control, the path to the Help file, the command <font color="#008000">HELP_WM_HELP</font> to request a context-sensitive pop-up Help topic, and the table of resource IDs and
Help topic IDs built earlier. There's no other work for your function to do after kicking <font color="#008000">WinHelp()</font> into action.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>If you've never seen the :: scope resolution operator used without a class name before it, it means call the function that is not in any class, and in Windows programming that generally means the SDK function.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>The third parameter of this call to WinHelp directs the Help system to put up a certain style of Help window. <font color="#008000">HELP_WM_HELP</font> gets you a pop-up menu, as does <font color="#008000">HELP_WM_CONTEXTMENU</font>. <font
color="#008000">HELP_CONTEXT</font> gets an ordinary Help window, which can be resized and moved, and allows Help navigation. <font color="#008000">HELP_FINDER</font> brings up the Help Topics dialog box. <font color="#008000">HELP_CONTENTS</font> and
<font color="#008000">HELP_INDEX</font> are obsolete and should be replaced with <font color="#008000">HELP_FINDER</font> if you maintain code that uses them.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<p><font color="#008000">OnContextMenu()</font> is even simpler:</P>
<pre><font color="#008000">void COptionsDialog::OnContextMenu(CWnd *pWnd, CPoint /*point*/)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> ::WinHelp((HWND)*pWnd, AfxGetApp()->m_pszHelpFilePath, </font></pre>
<pre><font color="#008000"> HELP_CONTEXTMENU, (DWORD)aHelpIDs);</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -