Note: The information in this topic is intended for developers who need to connect context-sensitive WebHelp topics to Visual C++ applications.
The sample code shown below uses a function called ShowHelp that will launch a local or remote WebHelp topic regardless of the browser type.
ShowHelp takes two arguments. The first, szTopic, is a URL or local file name. The second argument, bIsLocal, is a boolean value that indicates whether the file is a local file name (TRUE) or a remote URL (FALSE). If the file is local, ShowHelp will tell the browser to find the file in the "Help" subfolder of the folder containing the application program.
BOOL ShowHelp(LPCTSTR szTopic,
BOOL bIsLocal)
{
TCHAR szDir[MAX_PATH] =
"";
if (bIsLocal)
{
// Get directory of application
DWORD dw = GetModuleFileName(AfxGetInstanceHandle(), szDir, MAX_PATH);
TCHAR* pchEnd = _tcsrchr(szDir, '\\') + 1;
ASSERT_POINTER(pchEnd, TCHAR);
*pchEnd = '\0';
// Append subfolder name
_tcscat(szDir, _T("Help"));
}
// Launch topic
HINSTANCE hinst = ShellExecute(NULL, //no parent hwnd
NULL, // open
szTopic, // topic file or URL
NULL, // no parameters
szDir, // folder containing file
SW_SHOWNORMAL); // yes, show it
// handle less than 32
indicates failure
return hinst > (HINSTANCE)32;
}
ShowHelp requires an explicit file name or URL. But for context-sensitive Help, it is better to code the application to use context IDs rather than explicit topic names. Use the following sample function, ShowHelpContext, which maps integer context IDs to topic name strings, then calls ShowHelp to launch the topic.
BOOL ShowHelpContext(int
nContextId)
{
CString strTopic;
BOOL bIsLocal = TRUE;
switch (nContextId)
{
case HH_GADGET_DIALOG:
strTopic = _T("gadget.htm");
break;
case HH_WHATSIT_DIALOG:
strTopic = _T("whatsit.htm");
break;
case HH_WIDGET_DIALOG:
strTopic = _T("widget.htm");
break;
case HH_TECH_SUPPORT:
strTopic = _T("http://www.mycompany.com");
bIsLocal = FALSE;
break;
default:
strTopic = _T("unknown-context.htm");
break;
}
return ShowHelp(strTopic, bIsLocal);
}
Using ShowHelpContext makes maintaining context-sensitive Help much easier because if a topic name changes, only one function has to be modified. Note that the context IDs (for example, HH_GADGET_DIALOG) should be declared in a header file that can be shared among the various program modules that use context-sensitive WebHelp. This can be the same file where the prototypes for ShowHelp and ShowHelpContext are defined.