Search code examples
windowswinapimfcwinhelp

Overwrite the default F1 Windows Help behavior


I have a simple MFC application, where I want to customize the Help Button functions provided by the application. When clicking on F1 or Help button, it opens up a Windows help support page by default. How can I disable this default behavior and have it show nothing?

By show nothing, I mean not show the default windows support page. Ideally, when I should press F1 or click on help button, it should open no windows.


Solution

  • //Free the string allocated by MFC at CWinApp startup. 
    //m_pszHelpFilePath is the member variable of CWinApp that stores the 
    //location to default help window.
    //initialize it to an empty string just to be extra sure that default windows 
    //support page location is never found.
    //This needs to be set before CWinApp::InitInstance() is called.
    
    free((void*)m_pszHelpFilePath);
    m_pszHelpFilePath = _tcsdup(_T(""))
    

    In the MainFrame.cpp, declare the MessageMap:

    BEGIN_MESSAGE_MAP(MainFrame, CWinApp)
    ON_MESSAGE(WM_COMMANDHELP, OnCommandHelp)
    END_MESSAGE_MAP()
    

    Then, call the OnCommandHelp() that is a message handler which will be used to process F1 when in disabled mode.

    LRESULT  MainFrame::OnCommandHelp(WPARAM wParam, LPARAM lParam)
    {
        CWnd *pWnd = GetFocus();
        if (pWnd != NULL)
        {
            CWinApp* theApp = AfxGetApp();
            CString helpFilePath = theApp->m_pszHelpFilePath;
            // we have a control with the focus, quit help display
            ::WinHelp(m_hWnd, helpFilePath, HELP_QUIT, NULL);
            return TRUE;
        }
        return FALSE;        // let default handling process it
    }
    

    Here, WinHelp() is called which launches Windows Help (Winhelp.exe) and passes additional data that indicates the nature of the help requested by the application. HELP_QUIT as one of its parameters, closes the default windows help support page requested.

    Also, don't forget to declare the OnCommandHelp() in MainFrame.h :

    afx_msg LRESULT OnCommandHelp(WPARAM wParam, LPARAM lParam);