程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 在C#中使用.chm幫助文件

在C#中使用.chm幫助文件

編輯:C#入門知識

將編譯好的.chm幫助文件(可以用EasyCHM進行制作,下載地址:http://download.csdn.net/detail/xiaoyaofriend/4501571)添加到C# 編寫的程序中時,需要調用Help類的ShowHelp和ShowHelpIndex靜態方法。下面對這兩個方法進行詳細介紹。
(1)ShowHelp方法。顯示幫助文件的內容。該方法有4種重載形式,它們的語法形式分別如下。
語法1:
public static void ShowHelp (Control parent,string url)
參數說明如下。
parent:標識“幫助”對話框的父級的Control。
url:幫助文件的路徑和名稱。
返回值:顯示指定URL處的幫助文件內容。
語法2:
public static void ShowHelp (Control parent,string url,HelpNavigatornavigator)
參數說明如下。
parent:標識“幫助”對話框的父級的Control。
url:幫助文件的路徑和名稱。
navigator:HelpNavigator值之一。HelpNavigator值
語法3:
public static void ShowHelp (Controlparent,string url,string keyword)
參數說明如下。
parent:標識“幫助”對話框的父級的Control。
url:幫助文件的路徑和名稱。
keyword:要為其顯示幫助信息的關鍵字。
返回值:顯示在指定URL處找到的有關特定關鍵字的幫助文件內容。
語法4:
public static void ShowHelp (Control parent,string url,HelpNavigatorcommand,Object parameter)
參數說明如下。
parent:標識“幫助”對話框的父級的Control。
url:幫助文件的路徑和名稱。
command:HelpNavigator值之一。 HelpNavigator值及說明如表4所示。
Parameter:任意類型的參數。
返回值:顯示位於用戶提供的URL處的幫助文件內容。
(2)ShowHelpIndex方法。顯示指定幫助文件的索引。
語法:
public static void ShowHelpIndex (Control parent,string url)
示例
在Windows應用程序中調用.chm幫助文件
本示例實現的是,當程序運行時,單擊【help】按鈕,在程序中調用.chm幫助文件。
程序主要代碼。
private void bnthelp_Click(object sender, EventArgs e)
{
string helpfile = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.Startup Path.LastIndexOf("\\")).LastIndexOf("\\"));
helpfile+=@"\help\mrHelp.chm";
Help.ShowHelp(this,   helpfile);
Help.ShowHelpIndex(this,   helpfile); //顯示指定幫助的索引
}
完整程序代碼如下:
★   ★★★★Form1.cs窗體代碼文件完整程序代碼★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _5_02
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void bnthelp_Click(object sender, EventArgs e)
{
string helpfile = Application.StartupPath.Substring(0,Application.StartupPath.Substring(0,Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
helpfile+=@"\help\mrHelp.chm";
Help.ShowHelp(this,   helpfile);
Help.ShowHelpIndex(this,   helpfile); //顯示指定幫助的索引
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
★   ★★★★Form1.Designer.cs窗體代碼文件完整程序代碼★★★★★
namespace _5_02
{
partial class Form1
{
/// <summary>
/// 必需的設計器變量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
/// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.bnthelp = new System.Windows.Forms.Button();

this.helpProvider1 = new System.Windows.Forms.HelpProvider();
this.SuspendLayout();
//
// bnthelp
//
this.bnthelp.Location = new System.Drawing.Point(91, 34);
this.bnthelp.Name = "bnthelp";
this.bnthelp.Size = new System.Drawing.Size(70, 23);
this.bnthelp.TabIndex = 0;
this.bnthelp.Text = "help";
this.bnthelp.UseVisualStyleBackColor = true;
this.bnthelp.Click += new System.EventHandler(this.bnthelp_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 99);
this.Controls.Add(this.bnthelp);
this.Name = "Form1";
this.Text = "幫助引用";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button bnthelp;
private System.Windows.Forms.HelpProvider helpProvider1;
}
}
★   ★★★★Program.cs主程序文件完整程序代碼★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _5_02
{
static class Program
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}

(3)通過進程Process去調用。

For example:

System.Diagnostics.Process p = new System.Diagnostics.Process();

p.StartInfo.FileName = "helpfile.chm";

p.Start();

在Web中好像還沒有比較好的查看chm的方法。

(4)HelpProvider控件

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

具體步驟如下:

A幫助按鈕不能與最大化和最小化按鈕同時存在。

設置窗體屬性:

MaximizeBox=false;
MinimizeBox=false;

HelpButton=true;


B添加控件helpProvider

該控件可以帶有幫助文件,chm


C將helpProvider控件和窗體聯系起來

設置窗體屬性:(別的控件也一樣)

HelpKeyword on helpProvider 索引和幫助文件中的索引對應

HelpNavigator on helpP


D給控件helpProvider加載幫助文件

public Form1()
 {
      InitializeComponent();
      string strpath = Application.StartupPath.Substring(0, Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\")).LastIndexOf("\\"));
      strpath += @"\mrHelp.chm";
      helpProvider1.HelpNamespace = strpath;
}

E實現點擊?幫助按鈕實現F1的功能

在幫助按鈕的點擊事件中

private void Form1_HelpButtonClicked(object sender, CancelEventArgs e)
 {

         SendKeys.Send("{F1}");
        //SendKeys.SendWait("{F1}");
 }

使用代碼實現:
TestHelpProvider:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestHelpProvider
{
    public partial class Form1 : Form
    {
        public Form1()
       {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           //將幫助信息的文本字符串關聯到控件上,在相應控件上按下F1鍵時顯示
            helpProvider1.SetHelpString(textBox1, "Enter an age that is less than 65.");
            helpProvider1.SetHelpString(textBox2, "Enter a 5 digit post code.");
        }
   }
}

作者:xiaoyaofriend

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved