基于文件夹目录生成CHM电子书

昨天因为时间的原因,就草草的写了个比较简单的方法,调用hha.dll来编译CHM文件,昨天晚上回去,将这个方法整理到ChmHelper.dll中了,具体的代码如下:

CHMDocument.cs文件

添加编译的重载方法

代码
/// <summary>
        
/// 使用hha.dll进行编译
        
/// </summary>
        
/// <param name="file"></param>
        
/// <returns></returns>
        public bool Compile(string file)
        {
            
this.CreateHhp();
            
this.OpenHhc();
            
this.OpenHhk();
            HHA_CompileHHP(
this.strHhp, GetInfo1, GetInfo1, 0);
            
return true;
        }


关于hha.dll中的 代码

代码
delegate string GetInfo(string log);

        [DllImport(
"hha.dll")]
        
static extern bool HHA_CompileHHP(string hhp, GetInfo pro, GetInfo fi, int flag);

        
public string GetInfo1(string log)
        {
            
this.OutPutText += log;
            
return log;
        }  


新的创建hhp的方法

代码
/// <summary>
        
/// 创建hhp文件
        
/// </summary>
        
/// <param name="htmFile">htm文件名</param>
        private void CreateHhp()
        {
            FileStream fs 
= new FileStream(strHhp, FileMode.Create); //创建hhp文件
            streamWriter = new System.IO.StreamWriter(fs, System.Text.Encoding.GetEncoding(config.EncodeType));//可能会报警告 
            streamWriter.WriteLine("[OPTIONS]");
            streamWriter.WriteLine(
"Compatibility=1.1 or later");
            streamWriter.WriteLine(
"Compiled file=" + "Alexis.chm");  //chm文件名,要带后缀名
            streamWriter.WriteLine("Contents file=" + strHhc);  //hhc文件名
            streamWriter.WriteLine("Index file=" + strHhk);
            streamWriter.WriteLine(
"Default topic=" + config.DefaultPage);  //默认页
            streamWriter.WriteLine("Display compile progress=yes"); //是否显示编译过程
            streamWriter.WriteLine("Language=0x804 中文(中国)");  //chm文件语言
            streamWriter.WriteLine("Title=" + _title);//标题
            streamWriter.WriteLine("Default Window=Main");
            streamWriter.WriteLine();
            streamWriter.WriteLine(
"[WINDOWS]");
            streamWriter.WriteLine(
"Main=,\"xeditor.hhc\",\"xeditor.hhk\",,,,,,,0x20,180,0x104E, [80,60,720,540],0x0,0x0,,,,,0");//这里最重要了,一般默认即可
            streamWriter.WriteLine();
            streamWriter.WriteLine(
"[FILES]");
            NodesHhp(nodeList);
            streamWriter.WriteLine();
            streamWriter.Close();
        }


 

昨天的“农民伯伯”说想整个基于文件夹目录的生成CHM的软件,想了下,也不是很难,主要的难点就是如果确定子节点、父节点的关系。于是乎,昨晚花了点时间,将其实现,现将代码和实现跟大家分享:

 

 这个是测试的文件夹目录:

 

 

 程序的界面如下:很简单的一个界面,选择根目录,编译(使用hha.dll)

 

 

这个是生成的chm电子书,基于文件夹目录的

 

 

 代码:

遍历整个目录,生成父节点、子节点

代码
private void GetFiles(string filePath, CHMNode node)
        {
            DirectoryInfo folder 
= new DirectoryInfo(filePath);
            node.Name 
= folder.Name;

            FileInfo[] chldFiles 
= folder.GetFiles("*.*");
            
foreach (FileInfo chlFile in chldFiles)
            {
                
if (chlFile.Extension == ".htm" || chlFile.Extension == ".html")
                {
                    CHMNode chldNode 
= new CHMNode();
                    chldNode.Name 
= chlFile.Name;
                    chldNode.Local 
= chlFile.FullName;
                    node.Nodes.Add(chldNode);
                }
            }

            DirectoryInfo[] chldFolders 
= folder.GetDirectories();
            
foreach (DirectoryInfo chldFolder in chldFolders)
            {
                CHMNode chldNode 
= new CHMNode();
                chldNode.Name 
= folder.Name;
                node.Nodes.Add(chldNode);
                GetFiles(chldFolder.FullName, chldNode);
            }

        }


编译按钮,在这里设置跟节点

代码
private void btnComplie_Click(object sender, EventArgs e)
        {
            
if (this.txtPath.Text=="")
            {
                MessageBox.Show(
"请选择目录");
                
return;
            }
            
            CHMDocument document 
= new CHMDocument();
            document.FileName 
= "Made by Alexis";
            document.Title 
= "Alexis";//设置根目录的名字
            
//根节点
            CHMNode root = new CHMNode();
            root.Name 
= this.txtPath.Text.Substring(this.txtPath.Text.LastIndexOf('\\'+ 1);
            document.Nodes.Add(root);
            GetFiles(
this.txtPath.Text, root);
            
//编译
            document.Compile("a");
        }


 

程序下载

 

 

 

posted @ 2010-11-23 12:48  Alexis  阅读(5685)  评论(18编辑  收藏  举报