当前位置:编程学习 > C#/ASP.NET >>

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

将编译好的.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;

n

补充:软件开发 , C# ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,