C#实现电脑桌面端的本地txt电子书阅读器

写在前面的话

手机阅读是为了利用更多的碎片时间,但有时候桌面端阅读更方便,有需求就去找大牛的作品,试了几个,不是自己想要的,那就动手自己写一个了,满足基本功能的同时,也是一种自我学习和提高,分享出来给需要的朋友参考和下载使用。

开源地址:

github.com/zqunyan/DesktopReader

软件下载地址:

这里有已经编译打包好的可以下载交流分享 DesktopReader

常用功能

目前已基本实现功能:
  • 电子书的打开,保存,删除
  • 关闭窗口记录当前阅读历史
  • 目录浏览跳转功能
  • 查找功能
  • 滚动翻页功能,可自订滚动值
  • 自由位置移动
  • 窗口大小随意调整
  • 自定义背景颜色,行高,字体大小
  • 自定编码

常用快捷键

  • Ctrl + S #保存当前电子书到书单,如果已经存在,则设置当前电子书为默认打开书目
  • Ctrl + F #查找功能
  • Ctrl + B #目录浏览跳转

核心代码

加载电子书

private void OpenFile(string strPath, Encoding encoding)
{
    Cursor.Current = Cursors.WaitCursor;
    bookPath = strPath;
    bookName = strPath.Substring(strPath.LastIndexOf('\\') + 1, strPath.LastIndexOf('.') - strPath.LastIndexOf('\\') - 1);
    tsmiBookName.Text = bookName;
    try
    {
        FileStream fs = new FileStream(strPath, FileMode.Open);
        StreamReader sr = new StreamReader(fs, encoding);
        rtbContent.Text = sr.ReadToEnd();
        rtbContent.Text = Regex.Replace(rtbContent.Text, @"(?s)\n\s*\n", "\n");
        sr.Dispose();
        sr.Close();
        getContentsList(rtbContent.Text);
        LoadSetting();
    }
    catch (Exception err)
    {
        MessageBox.Show(err.ToString());
    }
    Cursor.Current = Cursors.Default;
}

加载本地设置

private void LoadSetting()
{
    int index = rtbContent.GetCharIndexFromPosition(new Point(1, 1));
    rtbContent.SelectAll();
    xml.Load(xmlPath);
    XmlElement node = (XmlElement)xml.SelectSingleNode("//settings");
    XmlNode nodeFontName = node.SelectSingleNode("//fontName");
    XmlNode nodeFontSize = node.SelectSingleNode("//fontSize");
    XmlNode nodeLineSpace = node.SelectSingleNode("//lineSpace");
    XmlNode nodeBackColor = node.SelectSingleNode("//backColor");
    XmlNode nodeScrollValue = node.SelectSingleNode("//scrollValue");

    if (nodeLineSpace != null && nodeLineSpace.InnerText.Trim() != "") 
        Pub.SetLineSpace(rtbContent, Convert.ToInt32(nodeLineSpace.InnerText.Trim()));
    if (nodeFontName != null && nodeFontSize != null && nodeFontName.InnerText.Trim() != "" && nodeFontSize.InnerText.Trim() != "")
        rtbContent.Font = new Font(nodeFontName.InnerText.Trim(), Convert.ToInt32(nodeFontSize.InnerText.Trim()));
    if (nodeBackColor != null && nodeBackColor.InnerText != "")
        rtbContent.BackColor = Color.FromArgb(Convert.ToInt32(nodeBackColor.InnerText.Trim()));
    if (nodeScrollValue != null && nodeScrollValue.InnerText != "") 
        scrollValue = nodeScrollValue.InnerText;

    rtbContent.Select(index, 0);
    rtbContent.ScrollToCaret();
}

强推

无广告追书神器APP-阅读
一款超好用的,无广告,轻量小巧,免费开源的android手机电子书阅读器。

  • 下载地址 https://www.coolapk.com/apk/com.gedoor.monkeybook
  • 开源地址 https://github.com/gedoor/MyBookshelf
    开源Github上有相关说明,包括书源怎么操作,祝君阅读快乐!
PS

虽然出了 3.x 的版本,但还是感觉 2.x 的好用些。

猜你喜欢

转载自blog.csdn.net/ymtianyu/article/details/105864323