C#打開chm定位到特定頁面


下面比較啰嗦,只一句即可:
Help.ShowHelp(null,"C:\help.hcm", HelpNavigator.Topic,"index.htm")

方法一: Process.Start()

在沒有更多需求的情況下,用這種方法可以打開 chm 文件並定位至文件首頁。

using System.Diagnostics;

string filepath = @"C:\helpdoc.chm";
Process.Start(filepath);


方法二: Help.ShowHelp()

如果需要在打開 chm 文件的同時定位到某一特定頁面,則可以使用 Help.ShowHelp(),
這個方法原本用於 WinForm 程序中操作幫助文檔。

方法有四個重載,詳見 http://msdn.microsoft.com/en-us/library/system.windows.forms.help.showhelp.aspx

Public methodStatic member ShowHelp(Control, String) Displays the contents of the Help file at the specified URL.
Public            methodStatic member ShowHelp(Control, String, String) Displays the contents of the Help file found at the specified URL for a specific keyword.
Public methodStatic member ShowHelp(Control, String, HelpNavigator) Displays the contents of the Help file found at the specified URL for a specific topic.
Public methodStatic member ShowHelp(Control, String, HelpNavigator, Object) Displays the contents of the Help file located at the URL supplied by the user.

其中 HelpNavigator 用於指定文件打開時顯示的內容。HelpNavigator 的具體介紹如下:
http://msdn.microsoft.com/en-us/library/system.windows.forms.helpnavigator.aspx

這里使用了 ShowHelp() 的最后一個重載:

using  System.Windows.Forms;

string filepath = @"C:\helpdoc.chm";
Help.ShowHelp(null, filename, HelpNavigator.Topic, "index.html"); 
//index.html 是打開文件時要顯示的頁面

url:
http://greatverve.cnblogs.com/archive/2012/05/24/csharp-chm.html

用C#實現利用F1打開幫助文件
最近需要做幫助文檔,所以在網上查了一下,這里把一些有用的資料摘錄一下。

     1. 首先這篇文章是比較詳細的一個介紹:

Open Help file on F1 function key press in windows application

This article explains how to open help file on F1 Function key press in windows application.

Almost all applications need Help file. It could be .chm , .PPT, . HTML, .PDF or any other kind of file which you want to open on F1 key press. Lets see how can we do that.

.NET Framework provides F1helpProvider component to handle F1 key press. You can use this component to show help file (.chm, .PPT, .PDF etc.,) to the user. The developer need not have to write even a single line of C# or Vb.Net code to display help file on press of F1 function key from keyboard.

You can find the helpProvider component under Component tab of ToolBox in Visual Studio. Below is the pic which will guide you to find Helpprovider component.

Now Just drag helpProvider into your application. Then go to the property of HelpProvider and Set its " HelpNamespace" property to your desired help file path. Below is the picture which will guide you to do that.

Once you set the property, then compile and run your sample application.

Now click F1 Button from your keyboard when the focus is on your application and you will see your help file opened.

You can also set the different property of helpProvider componet (Which you have added in the main form) from the MainForm property window, for eg., set the TOC when user clicks on the F1 key. You can also set the property to show window help button etc.,

Now we will see how to display help topic in a .chm file by keyword on a Button Click

Step 1 -> Click Button1 and display its properties.
Step 2 ->Set the HelpKeywordonHelpProvider1 property to "Your .CHM Topic". 
Step 3 -> Set the HelpNavigatoronHelpProvider1 property to "KeywordIndex".
Step 4 -> Compile and run your application

If you want to set using Code you can do like this

helpProvider1.HelpNamespace =

"C:/helpFile.chm";

HelpNavigator.KeywordIndex); //Set the Keyword

helpProvider1.SetHelpKeyword(Button1,

"Your .CHM Topic"); // Topic in help file.

helpProvider1.SetHelpNavigator(Button1,


Hope this will help all who wants to integrate F1 key press to open help file in windows application.

2. 這里主要用到HelpProvider類,它用於提供控件的彈出或聯機幫助。

每個 HelpProvider 實例均維護一個對關聯控件的引用的集合。若要使幫助文件與 HelpProvider 關聯,請設置 HelpNamespace 屬性。通過調用 SetHelpNavigator 方法並提供指定控件的 HelpNavigator 值來指定提供的幫助類型。通過調用 SetHelpKeyword 方法為幫助提供關鍵字或主題。若要打開特定主題的幫助,則應以 topicName.htm 的形式傳入關鍵字。

若要使特定的幫助字符串與控件關聯,請使用 SetHelpString 方法。如果用戶在控件包含焦點時按下 F1 鍵,使用此方法與控件關聯的字符串將顯示在彈出窗口中。

如果尚未設置 HelpNamespace 屬性,則必須使用 SetHelpString 方法提供幫助文本。如果同時設置了 HelpNamespace 和幫助字符串,則基於 HelpNamespace 的幫助信息優先。

HelpProvider 在 Help 類上調用方法來提供幫助功能。

HelpProvider控件可以掛起控件,顯示幫助主題。
1.SetShowHelp()方法:設置指定控件是否顯示幫助信息;
2.HelpNamespace()方法:設置幫助文件;
3.SetHelpKeyword()方法:為幫助文件設置關鍵字;
4.SetHelpNavigator()方法:設置顯示幫助中的元素;
5.SetHelpString()方法:將幫助信息的文本字符串關聯到控件上。


TestHelpProvider:

  1. using System;   
  2. using System.Collections.Generic;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Drawing;   
  6. using System.Text;   
  7. using System.Windows.Forms;   
  8.   
  9. namespace TestHelpProvider   
  10. {   
  11.     public partial class Form1 : Form   
  12.      {   
  13.         public Form1()   
  14.          {   
  15.              InitializeComponent();   
  16.          }   
  17.   
  18.         private void Form1_Load(object sender, EventArgs e)   
  19.          {   
  20.             //將幫助信息的文本字符串關聯到控件上,在相應控件上按下F1鍵時顯示   
  21.              helpProvider1.SetHelpString(textBox1, "Enter an age that is less than 65.");   
  22.              helpProvider1.SetHelpString(textBox2, "Enter a 5 digit post code.");   
  23.          }   
  24.      }   
  25. }  

3. 介紹了菜單項顯示幫助文檔:

The HelpProvider control is used to show how to link to specific keyword in a .chm help file.

private void mnuContentsHelp_Click(object sender, System.EventArgs e) 
{
        // Show the contents of the help file.
        Help.ShowHelp(this, hpAdvancedCHM.HelpNamespace);
    }

    private void mnuIndexHelp_Click(object sender, System.EventArgs e) 
{
        // Show index of the help file.
        Help.ShowHelpIndex(this, hpAdvancedCHM.HelpNamespace);
    }

    private void mnuSearchHelp_Click(object sender, System.EventArgs e) 
{
        // Show the search tab of the help file.
        Help.ShowHelp(this, hpAdvancedCHM.HelpNamespace, HelpNavigator.Find, "");
    }

4. 一個郵件介紹:

If you use F1HelpProvider control. When you press F1 on the active control It will shows a pop up with Text which you have set already. Follow the Steps Below: 

1. Select the F1HelpProvider control from ToolBox and Drag it to your Windows Form. 
2. Set the following code in the Page Load event of your Form. 

helpProvider1.SetHelpString(txtPinCode, "Please Enter Ur city pin Code."); 

In the above code txtPinCode is a TextBox. 
3. Then run your application by pressing F5. 
4. If you have focus on the Text Box Pin Code then press F1. It will shows a Message that you have set likely to be ToolTip.


5.一個最好的例子,看了就明白。

Donwload example project C# 2005 Express from:
http://www.help-info.de/download/CSharp_CHM.zip
The CHM file resides in the DEBUG folder and you have to do following before giving that a try:
to open the CHM file right-click the saved file, click Properties, and then click Unblock.

(他說需要對CHM文件做這樣的操作,我試了,可以直接F1打開,你們要是有問題就按照步驟)。

下面是代碼:

        private void Form1_Load(object sender, EventArgs e)
        {
            // set F1 help topic for this form
            helpProvider1.HelpNamespace = Application.StartupPath + @"\" + sHTMLHelpFileName;
            helpProvider1.SetHelpNavigator(this, HelpNavigator.Topic);
            helpProvider1.SetHelpKeyword(this, @"/Garden/garden.htm");
            helpProvider1.SetHelpNavigator(this.btnStart, HelpNavigator.Topic);
            helpProvider1.SetHelpKeyword(this.btnStart, @"/Garden/flowers.htm");
            helpProvider1.SetHelpNavigator(this.btnExit, HelpNavigator.Topic);
            helpProvider1.SetHelpKeyword(this.btnExit, @"/Garden/tree.htm");
            helpProvider1.SetHelpNavigator(this.chkMain, HelpNavigator.Topic);
            helpProvider1.SetHelpKeyword(this.chkMain, @"/HTMLHelp_Examples/jump_to_anchor.htm#AnchorSample");
       }


Thanking you

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM