Click here to Skip to main content
12,078,489 members (47,017 online)
Click here to Skip to main content
Add your own
alternative version

Stats

102.9K views
2.4K downloads
30 bookmarked
Posted

Implementing Context-sensitive Help in MFC Apps

, 22 Nov 1999
Rate this:
Please Sign up or sign in to vote.
Implementing context-sensitive Help in MFC apps.

Introduction

I am implementing context-sensitive Help in my MFC app, and, as always with Windows programming, it isn't quite as simple as we'd like it to be. I found that to get the behavior I wanted, I had to tweak the message handling in the dialogs and Property Sheets a little. The result was that I created 2 base classes for all my dialogs and Property Sheets. These are CHelpDlg and CHelpPrSheet. You should be able to use these as is in your apps as well.

Features

  1. Provides general Help for the dialog when F1 is pressed.
  2. Provides context Help for the control with focus when Shift + F1 is pressed. (An alternative is to show the floating pointer when Shift + F1 is pressed. See code for how to do this.)
  3. Provides context-sensitive Help for controls in your dialog.
  4. (Dialog only) Provides general Help for the dialog if you include a button with ID = IDHELP.

Requirements

When creating your Visual Studio project, make sure you check Context-sensitive Help in the AppWizard. I'm assuming you are somewhat familiar with the support that MFC provides for context-sensitive Help. If not, read TN028 on MSDN.

Dialog Boxes

  1. Build your dialog box using the resource editor as you normally would.
  2. If you want context-sensitive Help, make sure you check the Context Help check box in the More Styles tab of the resource editor.
  3. For any controls that you want context-sensitive Help, make sure you check the Help ID check box in the General tab of the resource editor.
  4. If you want a Help button in your dialog, put one there with the ID = IDHELP.
  5. Use Class Wizard and derive from CDialog.
  6. Edit the .h and .cpp files and make CHelpDlg the base class instead of CDialog. Note: you must replace all refs to CDialog with CHelpDlg.

Property Sheets

  1. Build your Property Pages as you normally would.
  2. If you want context-sensitive Help, make sure you check the Context Help check box in the More Styles tab of the resource editor.
  3. For any controls that you want context-sensitive Help, make sure you check the Help ID check box in the General tab of the resource editor.
  4. Use Class Wizard and derive from CPropertyPage as you normally would.
  5. Use Class Wizard and create a new class derived from CPropertySheet.
  6. Edit the .h and .cpp files and make CHelpPrSheet the base class instead of CPropertySheet. Note you must replace all refs to CPropertySheet with CHelpPrSheet.

Implementation Notes

There are 2 main tricks employed here. First, is handling the WM_HELPINFO message. The default handler that MFC supplies doesn't do the right thing for context Help. Here is what the handler looks like:

BOOL CHelpDlg::OnHelpInfo(HELPINFO* pHelpInfo)
{
    // TODO: Add your message handler code here and/or call default
 
    if(pHelpInfo->iContextType == HELPINFO_WINDOW) {
        AfxGetApp()->WinHelp(pHelpInfo->dwContextId, HELP_CONTEXTPOPUP);
    }

    return(TRUE);
}

The other trick is figuring out what you want to do with F1 and Shift + F1. My solution is to trap a magical mystical undocumented MFC msg 0x4d and handle it in PreTranslateMessage as shown below:

BOOL CHelpDlg::PreTranslateMessage(MSG* pMsg)
{
    // TODO: Add your specialized code here and/or call the base class

    // Magical mystical MFC F1 Help msg!
    if(pMsg->message == 0x4d) 
    {
        if (GetKeyState(VK_SHIFT) > = 0) 
        {
            // Shift key not down

            // Supply general dialog level help
            OnCommandHelp(0, 0);

            return(TRUE); // Eat it
        }
#ifdef FLOATING_ARROW
        else 
        {
            // Use this if you want Shift+F1
            // to create the floating arrow instead
            SendMessage(WM_SYSCOMMAND, SC_CONTEXTHELP);

            return(TRUE);
        }
#endif // FLOATING_ARROW
    }

    return CDialog::PreTranslateMessage(pMsg);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Peter Moss
Web Developer
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionProblem with F1 key with HTMLHelp Pin
DougVC30-Apr-09 5:53
memberDougVC30-Apr-09 5:53 
GeneralProblems with F1 key Pin
Lucecilla7-Dec-05 6:38
memberLucecilla7-Dec-05 6:38 
GeneralRe: Problems with F1 key Pin
JKJKJK29-Dec-05 6:08
memberJKJKJK29-Dec-05 6:08 
Generalproblem with Demo Pin
olis16-Mar-04 22:37
memberolis16-Mar-04 22:37 
GeneralRe: problem with Demo Pin
jkaspzyk30-Sep-04 7:52
memberjkaspzyk30-Sep-04 7:52 
GeneralRe: problem with Demo Pin
okline17-Oct-04 21:40
memberokline17-Oct-04 21:40 
QuestionAnother solution? Pin
Sam Hobbs5-Mar-03 13:51
memberSam Hobbs5-Mar-03 13:51 
Generalurgent help !!! Pin
pvijaya20-Oct-02 10:57
susspvijaya20-Oct-02 10:57 
GeneralCompile problems Pin
dessi10-Sep-02 12:22
memberdessi10-Sep-02 12:22 
GeneralRe: Compile problems Pin
Peter Moss12-Sep-02 4:04
memberPeter Moss12-Sep-02 4:04 
GeneralRe: Compile problems Pin
Weishan7-Aug-03 11:15
memberWeishan7-Aug-03 11:15 
GeneralRe: Compile problems Pin
stephangeue5-Dec-03 6:21
memberstephangeue5-Dec-03 6:21 
GeneralAWESOME !!!! Pin
Anonymous26-Aug-02 12:06
sussAnonymous26-Aug-02 12:06 
QuestionWho could help me ? Pin
Anonymous8-Oct-01 1:25
memberAnonymous8-Oct-01 1:25 
AnswerRe: Who could help me ? Pin
Roger Allen21-Aug-02 2:58
memberRoger Allen21-Aug-02 2:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160212.1 | Last Updated 23 Nov 1999
Article Copyright 1999 by Peter Moss
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid