سؤال

I'm doing maintenance on a legacy MFC application. When the user selects Help → Help from the main menu along the top of the app, I need my custom OnHelp() to be called. From my research, I've learned that MFC normally intercepts this command automatically and brings up your help file itself. But you can override this command and intercept this message yourself. I have this in my message map:

BEGIN_MESSAGE_MAP(MyApp, MyBaseApp)
   //{{AFX_MSG_MAP(MyApp)
   ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
   ON_COMMAND(ID_HELP, OnHelp)
   //}}AFX_MSG_MAP
   // Standard file based document commands
END_MESSAGE_MAP()

The "About" OnAppAbout() gets called, but not my OnHelp() (MFC still intercepts it and brings up the help by itself). My implementation is pretty straightforward:

void MyApp::OnHelp()
{
    // This never gets called
   MessageBox( NULL, "HtmlHelp: MyApp", "Hey", MB_OK );

   CString csHelpFile;
   csHelpFile.Format( "%s/MyHelp.chm", MyDoc::GetHelpPath() );
   ::HtmlHelp(
       NULL,
       csHelpFile,
       HELP_WM_HELP,
       NULL );
}

I know it's not getting called because my MessageBox never appears (I can't use Visual Studio to debug this; message boxes only). I also tried to wedge it into the CMDIFrameWnd, with the message map and similar implementation, with no success either (a different item from the Help menu item is implemented here and works fine). Any idea what I need to do to hook into my own custom help function?

هل كانت مفيدة؟

المحلول

You need to add ON_WM_HELPINFO() into your CMainFrame's message map. Define afx_msg BOOL OnHelpInfo(HELPINFO* pHelpInfo); in your header file for the Main Frame, and implement it in the cpp file:

BOOL CMainFrame::OnHelpInfo(HELPINFO* pHelpInfo)
{
#ifdef _DEBUG
  MessageBox( NULL, "HtmlHelp: MyApp", "Hey", MB_OK );
#endif

  CString csHelpFile;
  csHelpFile.Format( "%s/MyHelp.chm", MyDoc::GetHelpPath() );
  ::HtmlHelp(
   NULL,
   csHelpFile,
   HELP_WM_HELP,
   NULL );
  return CFrameWnd::OnHelpInfo(pHelpInfo);
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top