Programming with Microsoft Visual C++.NET 6ed [Electronic resources]

George Shepherd, David Kruglinski

نسخه متنی -صفحه : 319/ 177
نمايش فراداده

Example Ex19b: Help Command Processing

Ex19b is based on example Ex18d from Chapter 18. It's a two-view MDI application with view-specific help added. Each of the two view classes has an OnCommandHelp message handler to process F1 help requests and an OnHelpHitTest message handler to process Shift+F1 help requests.

Header Requirements

The compiler recognizes help-specific identifiers only if the following #include statement is present:

#include <afxpriv.h>

In Ex19b, the statement is in the

StdAfx.h file.

CStringView

The modified string view in

StringView.h needs message map function prototypes for both F1 help and Shift+F1 help, as shown here:

afx_msg LRESULT OnCommandHelp(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnHelpHitTest(WPARAM wParam, LPARAM lParam);

Here are the message map entries in

StringView.cpp :

ON_MESSAGE(WM_COMMANDHELP, OnCommandHelp)
ON_MESSAGE(WM_HELPHITTEST, OnHelpHitTest)

The OnCommandHelp message handler member function in

StringView.cpp processes F1 help requests. It responds to the message sent from the MDI main frame and displays the help topic for the string view window, as shown here:

LRESULT CStringView::OnCommandHelp(WPARAM wParam, LPARAM lParam)
{
if (lParam == 0) { // context not already determined
lParam = HID_BASE_RESOURCE + IDR_STRINGVIEW;
}
AfxGetApp()->WinHelp(lParam);
return TRUE;
}

Finally, the OnHelpHitTest member function handles Shift+F1 help, as shown here:

LRESULT CStringView::OnHelpHitTest(WPARAM wParam, LPARAM lParam)
{
return HID_BASE_RESOURCE + IDR_STRINGVIEW;
}

In a more complex application, you might want OnHelpHitTest to set the help context ID based on the mouse cursor position.

CHexView

The CHexView class processes help requests the same way as the CStringView class does. Following is the necessary header code in

HexView.h :

afx_msg LRESULT OnCommandHelp(WPARAM wParam, LPARAM lParam);
afx_msg LRESULT OnHelpHitTest(WPARAM wParam, LPARAM lParam);

Here are the message map entries in

HexView.cpp :

ON_MESSAGE(WM_COMMANDHELP, OnCommandHelp)
ON_MESSAGE(WM_HELPHITTEST, OnHelpHitTest)

And here is the implementation code in

HexView.cpp :

LRESULT CHexView::OnCommandHelp(WPARAM wParam, LPARAM lParam)
{
if (lParam == 0) { // context not already determined
lParam = HID_BASE_RESOURCE + IDR_HEXVIEW;
}
AfxGetApp()->WinHelp(lParam);
return TRUE;
}
LRESULT CHexView::OnHelpHitTest(WPARAM wParam, LPARAM lParam)
{
return HID_BASE_RESOURCE + IDR_HEXVIEW;
}

Resource Requirements

Two new symbols were added to the project's

Resource.h file. Their values and corresponding help context IDs are shown here:

Symbol

Value

Help Context ID

Value

IDR_STRINGVIEW

101

HIDR_STRINGVIEW

0x20065

IDR_HEXVIEW

102

HIDR_HEXVIEW

0x20066

Help File Requirements

Two topics were added to the

AfxCore.rtf file with the help context IDs HIDR_STRINGVIEW and HIDR_HEXVIEW, as shown here:

The generated

Ex19b.hm file, which is in the project's \hlp subdirectory, should look like this:

// Commands (ID_* and IDM_*) 
HID_WINDOW_NEWHEXWINDOW                      0x10082
HID_WINDOW_NEWSTRINGWINDOW                   0x10083
// Prompts (IDP_*) 
HIDP_OLE_INIT_FAILED                    0x30064
// Resources (IDR_*) 
HIDR_MANIFEST                           0x20001
HIDR_MAINFRAME                          0x20080
HIDR_Ex19bTYPE                          0x20081
HIDR_STRINGVIEW                         0x20065
HIDR_HEXVIEW                            0x20066
// Dialogs (IDD_*) 
HIDD_ABOUTBOX                           0x20064
// Frame Controls (IDW_*)

Testing the Ex19b Application

To test the application, open a string child window and a hexadecimal child window. Test the action of F1 help and Shift+F1 help within those windows.