11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 16. Context Sensitive Help<br />

page, make sure that “None” is selected from the “Underline” combo box, and check “Hidden” check box<br />

within “Effects” section.<br />

ID Mapping<br />

In order to support context sensitive help, we must link the footnotes to their corresponding command<br />

IDs. In our case, we need to link “help_test_A” to ID_HELPTEST_TESTA, “help_test_B” to<br />

ID_HELPTEST_TESTB… and so on.<br />

This ID mappings are implemented in “.hm” file, so by opening file “Help.hm” with a text editor, we<br />

will see all the IDs of the help items supported in the sample. Generally help IDs are generated according to<br />

certain rules: it bases the help ID of a control (or a window) on its resource ID. By default, for a command<br />

whose ID starts with “ID_”, <strong>MFC</strong> generates symbolic help ID by prefixing a character ‘H’ to the resource<br />

ID. For example, for command ID_HELPTEST_TESTA, the help ID generated by <strong>MFC</strong> is<br />

HID_HELPTEST_TESTA. For the actual ID value, <strong>MFC</strong> generates it by adding a fixed number to the<br />

corresponding resource ID value. For example, if the value of ID_HELPTEST_TESTA is 0x8004, the value of<br />

HID_HELPTEST_TESTA would be 0x18004 ⎯ here a number of 0x1000 is added to the command resource<br />

ID.<br />

By doing this, when the user executes a command in the help mode (When the mouse cursor changes<br />

to a question mark after the user clicks command ID_CONTEXT_HELP), the application will first find out the<br />

resource ID of the command being executed, add a fixed value, then pass the result to the help. By using<br />

this rule to generate ID for a help item, it is easier for us to implement context-sensitive help.<br />

Although the ID mapping is customizable, which means we can prefix different character(s) to a<br />

resource ID for generating help ID (For example, the help ID of command ID_HELPTEST_TESTA could be<br />

HLID_HELPTEST_TESTA), and we can add any value to the resource ID to generate a help ID, it is more<br />

convenient if we stick to the rules of <strong>MFC</strong>. For example, if we add 0x20000 instead of 0x10000 to the<br />

resource ID to make a help ID, we also need to add some code to the program to customize its default<br />

behavior.<br />

Another problem still remains: since we’ve already added footnotes and defined our own tags, the<br />

command IDs are still not directly linked to the footnote tags. For example, the help ID of command<br />

ID_HELPTEST_TESTA is HID_HELPTEST_TESTA, while the footnote tag implemented in the help file is<br />

“help_test_A”. This can be solved by defining alias names in the help project file. By opening “*.HPJ” file,<br />

we will see an [ALIAS] session. Under this session, a help ID can be linked directly to a footnote tag. In<br />

our case, the footnote tags are “help_test_A”, “help_test_B”…, and the help IDs automatically generated<br />

by <strong>MFC</strong> are HID_HELPTEST_TESTA, HID_HELPTEST_TESTB…. To link them together, we can add the following<br />

to alias session:<br />

……<br />

[ALIAS]<br />

HID_HELPTEST_TESTA=help_test_A<br />

HID_HELPTEST_TESTB=help_test_B<br />

HID_HELPTEST_TESTC=help_test_C<br />

HID_HELPTEST_TESTD=help_test_D<br />

Obviousely, if we use the default help ID strings to implement footnote tags (i.e., use<br />

HID_HELPTEST_TESTA instead of help_test_A as the footnote tag), the ID mapping could be eleminated.<br />

By doing this, when the user executes certain command in the help mode, the corresponding help page<br />

will be automatically brought up.<br />

Help Topics Dialog Box<br />

The footnotes can also be referenced from “Help Topics” dialog box. This dialog box is activated<br />

when the user executes command ID_HELP_FINDER. The contents contained in “Help Topics” dialog box<br />

are stored in “.cnt” file. To edit this file, we can open it using Help Workshop. In the Help Workshop, we<br />

can add two types of items: Heading and Tab Entry. A heading will not be linked to any footnote. Under<br />

each heading, we can add several tab entries, which need to be linked to footnotes. A new item can be<br />

added by clicking buttons labeled “Add Above…”, “Add Below…”. After that, a dialog box will pop up<br />

asking us to input the description text as well as the help ID (the footnote tag). The description of an item<br />

485

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!