【问题标题】:Open .chm file at specific page/topic using command line arguments使用命令行参数在特定页面/主题打开 .chm 文件
【发布时间】:2012-06-20 01:47:43
【问题描述】:

我正在尝试使用 C++ 中的系统调用在特定页面/主题打开 .chm 文件(Windows 帮助文件)。

我可以通过以下代码成功将 .chm 文件打开到起始页,但是如何将 .chm 文件打开到帮助文件中的特定页面/主题?

system("start c:/help/myhelp.chm");

PS:我知道系统是邪恶的/不鼓励,但系统部分与我试图确定的 .chm 文件(将指定我要打开的页面)一起传递的命令行参数并不真正相关。

【问题讨论】:

标签: c++ chm view-helpers


【解决方案1】:

好的,参数是这样的:

system(" /Q /E:ON /C HH.EXE ms-its:myChm.chm::myPageName.htm");

【讨论】:

  • 感谢您发布这个独立于语言的解决方案。在 Python 中,subprocess.Popen("hh,exe ms-its:C:/path/x.chm::/dir/pg.html") 效果很好,我可能会在 Windows 上使用它来显示空闲帮助。我很好奇你是否知道/Q /E:ON /C 标志应该做什么。
【解决方案2】:

Windows SDK 中有一个名为 HtmlHelp 的 API,位于 HtmlHelp.h 文件中。你可以这样调用:

HtmlHelp(GetDesktopWindow(), L"C:\\helpfile\\::/helptopic.html", HH_DISPLAY_TOPIC, NULL);

Microsoft Docs - HtmlHelpA function 提供有关该功能的更多信息。 HtmlHelp() 通常会解析为 HtmlHelpA()HtmlHelpW(),具体取决于是否设置了 Unicode 编译器选项。

另见Microsoft Docs - HTML Help API Overview

【讨论】:

    【解决方案3】:

    另一个选项 - 使用 ShellExecute。 Microsoft 帮助不容易使用。这种方法更容易并且符合您的问题。这是打开帮助文件并传递 ID 号的快速例程。我刚刚设置了一些简单的字符,所以你可以看到发生了什么:

        void DisplayHelpTopic(int Topic)
    {
    
        // The .chm file usually has the same name as the application - if you don’t want to hardcode it...
        char *CmndLine = GetCommandLine(); // Gets the command the program started with.
        char Dir[255];
        GetCurrentDirectory (255, Dir);
        char str1[75] = "\0"; // Work string
        strncat(str1, CmndLine, (strstr(CmndLine, ".exe") - CmndLine)); // Pull out the first parameter in the command line (should be the executable name) w/out the .exe
        char AppName[50] = "\0";
        strcpy(AppName, strrchr(str1, '\\')); // Get just the name of the executable, keeping the '\' in front for later when it is appended to the directory
    
        char parms[300];
        // Build the parameter string which includes the topic number and the fully qualified .chm application name
        sprintf(parms,_T("-mapid %d ms-its:%s%s.chm"), Topic, Dir, AppName);
        // Shell out, using My Window handle, specifying the Microsoft help utility, hh.exe, as the 'noun' and passing the parameter string we build above
    // NOTE: The full command string will look like this:
    //   hh.exe -mapid 0 ms-its:C:\\Programs\\Application\\HelpFile.chm
        HINSTANCE retval = ShellExecute(MyHndl, _T("open"), _T("hh.exe"), parms, NULL, SW_SHOW);
    }
    

    主题在您的 .chm 文件中进行编号。我为每个主题设置了一个#define,所以如果我必须更改 .chm 文件,我可以只更改包含文件以匹配,而不必担心在代码中搜索硬编码值。

    【讨论】:

      猜你喜欢
      • 2014-04-13
      • 2015-12-04
      • 2012-04-30
      • 2021-03-22
      • 2015-08-06
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      相关资源
      最近更新 更多