应为无聊 自己写的一个 TXT小说阅读器,支持老板键,自动贴边隐藏,划水神器^^
主要特色:
①支持拖拽txt文件到阅读器中自动打开txt文件,主要代码:
//拖拽TXT文件到窗体并加载TXT文件private void Form1_DragEnter(object sender, DragEventArgs e){string file = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();//获得路径if (File.Exists(file)){string Extension = System.IO.Path.GetExtension(file).ToLower();if (Extension == ".txt"){this.Text = System.IO.Path.GetFileName(file);this.richTextBox1.LoadFile(file, RichTextBoxStreamType.PlainText);//读取txt文件RememberTxt(file);//记录txt文件的路径LoadChapter(file);//读取章节DeleteBookmark();//删除书签(防止打开B小说,却加载了A小说书签的bug)}}}
②自动抓取txt文件里面的 章节 标题,代码如下:
//读取章节public void LoadChapter(string file){try{this.listBox1.Items.Clear();if (File.Exists(file)){FileStream fs = new FileStream(file, FileMode.Open);using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default)){string strLine = sr.ReadLine(); //读取文件中的一行while (strLine != null){string[] patArr = { "第(.*?)章", "第(.*?)回", "第(.*?)集", "第(.*?)卷", "第(.*?)部", "第(.*?)篇", "第(.*?)节", "第(.*?)季" };foreach (var pattern in patArr){Regex reg = new Regex(pattern);if (reg.IsMatch(strLine)){if (strLine.Length < 20)//超过20个字的标题,可能是小说正文里面出现的“书中书、文中文...的标题”...{this.listBox1.Items.Add(strLine.Trim().Replace("\r\n", "").Replace("\r", "").Replace("\n", ""));//添加标题行}continue;}}strLine = sr.ReadLine();//Thread.Sleep(1);}}}}catch { }}
③老板键 Alt + 1 ,代码如下:
//注册热键private void Form1_Activated(object sender, EventArgs e){HotKey.RegisterHotKey(Handle, 100, HotKey.KeyModifiers.Alt, Keys.D1);//注册热键 Alt+1 老板键 我用着顺手}//注销热键private void Form1_Leave(object sender, EventArgs e){HotKey.UnregisterHotKey(Handle, 100);//注销Id号为100的热键设定}//执行热键protected override void WndProc(ref Message m){const int WM_HOTKEY = 0x0312;//按快捷键 switch (m.Msg){case WM_HOTKEY:switch (m.WParam.ToInt32()){case 100://按下的是Alt+1 BossKey();//老板键break;}break;}base.WndProc(ref m);}//老板键:显示|隐藏 窗体private void BossKey(){if (this.WindowState == FormWindowState.Normal){this.WindowState = FormWindowState.Minimized;this.Hide();//隐藏窗体this.notifyIcon1.Visible = true; //使托盘图标可见}else{this.Visible = true;this.WindowState = FormWindowState.Normal;this.notifyIcon1.Visible = false;}}using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;namespace myTXTreader
{class HotKey{//如果函数执行成功,返回值不为0。//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。[DllImport("user32.dll", SetLastError = true)]public static extern bool RegisterHotKey(IntPtr hWnd, //要定义热键的窗口的句柄int id, //定义热键ID(不能与其它ID重复)KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效Keys vk //定义热键的内容);[DllImport("user32.dll", SetLastError = true)]public static extern bool UnregisterHotKey(IntPtr hWnd, //要取消热键的窗口的句柄int id //要取消热键的ID);public enum KeyModifiers{None = 0,Alt = 1,Ctrl = 2,Shift = 4,WindowsKey = 8}}
}
④仿QQ的 贴边隐藏,代码如下:
//窗口贴边(Ctrl+1)private void 窗口贴边Ctrl1ToolStripMenuItem_Click(object sender, EventArgs e){TieBian();}bool tb = true;public void TieBian(){if (tb){tb = false;this.隐藏章节ToolStripMenuItem.Text = "显示章节(Shift+1)";this.splitContainer1.Panel1Collapsed = true;//收起章节this.窗口贴边ToolStripMenuItem.Text = "取消贴边(Ctrl+1)";int w = Screen.GetWorkingArea(this).Width;//显示器的宽度int h = Screen.GetWorkingArea(this).Height;//显示器的高度this.Width = w / 5;this.Height = h;this.Left = w - this.Width;this.Top = h - this.Height;}else{tb = true;this.隐藏章节ToolStripMenuItem.Text = "隐藏章节(Shift+1)";this.splitContainer1.Panel1Collapsed = false;//展开章节this.窗口贴边ToolStripMenuItem.Text = "窗口贴边(Ctrl+1)";int w = Screen.GetWorkingArea(this).Width;//显示器的宽度int h = Screen.GetWorkingArea(this).Height;//显示器的高度this.Width = 800;this.Height = 600;this.Left = w / 5;this.Top = h / 5;}}//自动贴边隐藏后 鼠标移动上去 自动展出面板private void mStopAnhor(){if (this.Top <= 0 && this.Left <= 0){StopAanhor = AnchorStyles.None;}else if (this.Top <= 0){StopAanhor = AnchorStyles.Top;}else if (this.Left <= 0){StopAanhor = AnchorStyles.Left;}else if (this.Left >= Screen.PrimaryScreen.Bounds.Width - this.Width){StopAanhor = AnchorStyles.Right;}else if (this.Top >= Screen.PrimaryScreen.Bounds.Height - this.Height){StopAanhor = AnchorStyles.Bottom;}else{StopAanhor = AnchorStyles.None;}}
⑤修改阅读器的背景色,文字字体、颜色和大小
⑥添加 书签
截图:
↑↑↑章节和正文节目↑↑↑
↑↑↑收起章节↑↑↑
↑↑↑右侧贴边↑↑↑↑
以上全部源代码下载地址:https://download.csdn.net/download/djk8888/12350994