public static void Save(TextBook tb) { BinaryFormatter bw = new BinaryFormatter(); Stream sw = File.Create(tb.CatalogsFileName); bw.Serialize(sw, tb); sw.Close(); }
void Books_Click(object sender, EventArgs e) { ToolStripMenuItem tsmi = sender as ToolStripMenuItem; tb = bs.Find(b => b.BookName.Equals(tsmi.Text)); GenCategoriesMenu(); this.Text = string.Format(APPLICATION_CAPTION, this.tb.Author, "-" + this.tb.BookName, " "); this.panelContent.ResetText(); }
public static TextBook Restore(string cacheFile) { Stream sr = File.OpenRead(cacheFile); BinaryFormatter bf = new BinaryFormatter(); TextBook t = (TextBook)bf.Deserialize(sr); sr.Close(); return(t); }
void Books_Click(object sender, EventArgs e) { ToolStripMenuItem tsmi = sender as ToolStripMenuItem; tb = bs.Find(b => b.BookName.Equals(tsmi.Text)); GenCategoriesMenu(); this.Text = string.Format(APPLICATION_CAPTION, this.tb.Author, "-" + this.tb.BookName, " "); TRTextBox content = (TRTextBox)this.pContent.Controls["content"]; content.Text = ""; }
private void TextReader_Load(object sender, EventArgs e) { this.Text = this.Text = string.Format(APPLICATION_CAPTION, "", "", ""); Image bgimage = Image.FromFile(@"skins\black.gif"); this.panelContent.BackgroundImage = bgimage; this.panelContent.BackgroundImageLayout = ImageLayout.Tile; cacheDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Settings.Default.CachePath); if (!Directory.Exists(cacheDir)) { Directory.CreateDirectory(cacheDir); } string[] cacheFiles = Directory.GetFiles(cacheDir, "*.txt.~"); foreach (string cacheFile in cacheFiles) { TextBook t = TextBook.Restore(cacheFile); bs.Add(t); ToolStripMenuItem item = new ToolStripMenuItem(t.BookName); item.Click += new EventHandler(Books_Click); this.tsBooks.DropDownItems.Add(item); } lbContent.BackgroundImage = bgimage; lbContent.BackgroundImageLayout = ImageLayout.Tile; //panelContent.Size = this.panelContent.Size; lbContent.ScrollBars = ScrollBars.Vertical; lbContent.Multiline = true; //lbContent.Enabled = false; lbContent.Font = new Font("微软雅黑", 20); //this.panelContent.Controls.Add(content); this.panelContent.Location = new Point(0, 0); this.lbContent.Location = this.lbContent.Parent.Location; this.panelContent.Size = this.ClientSize; this.pControl.Visible = false; ResetControl(); }
/// <summary> /// 打开小说 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tsOpen_Click(object sender, EventArgs e) { //打开对话框 DialogResult dr = openFileDialog1.ShowDialog(); if (dr == DialogResult.OK) { FileInfo fi = new FileInfo(openFileDialog1.FileName); tb = TextBook.NewTextBook(fi.FullName, cacheDir); //查找是否有缓存的章节目录 if (File.Exists(tb.CatalogsFileName)) { //如果存在则导入 tb = TextBook.Restore(tb.CatalogsFileName); } else { //如果不存在则生成目录章节 TextBook.NewCatalog(ref tb); TextBook.Save(tb); } //显示书名和作者 this.Text = string.Format(APPLICATION_CAPTION, tb.Author, "-" + tb.BookName, " "); bs.Add(tb); ToolStripMenuItem item = new ToolStripMenuItem(tb.BookName); item.Click += new EventHandler(Books_Click); this.tsBooks.DropDownItems.Add(item); GenCategoriesMenu(); } }
public static void NewCatalog(ref TextBook tb) { Catalogs catalogs = new Catalogs(); //以文本方式打开小说 BufferedStream bs = new BufferedStream(File.OpenRead(tb.FullName)); StreamReader sr = new StreamReader(bs, Encoding.Default); long position = 0; string buffer; Regex re = new Regex("书名:\\w{1,50}", RegexOptions.IgnorePatternWhitespace); while (string.IsNullOrEmpty(tb.BookName)) { buffer = sr.ReadLine(); tb.Lines++; if (!string.IsNullOrEmpty(buffer)) { position += buffer.Length; Match m = re.Match(buffer); if (m.Success) { tb.BookName = m.Value; break; } } } re = new Regex("作者:\\w{1,50}", RegexOptions.IgnorePatternWhitespace); while (string.IsNullOrEmpty(tb.Author)) { buffer = sr.ReadLine(); tb.Lines++; if (!string.IsNullOrEmpty(buffer)) { position += buffer.Length; Match m = re.Match(buffer); if (m.Success) { tb.Author = m.Value; break; } } } re = new Regex(@"第(一|二|三|四|五|六|七|八|九|十|零|百|千|万|\d){1,50}章.+\s", RegexOptions.IgnorePatternWhitespace); CatalogPuple cp = null; StringBuilder sb = new StringBuilder(); while (!sr.EndOfStream) { buffer = sr.ReadLine(); tb.Lines++; if (!string.IsNullOrEmpty(buffer)) { position += buffer.Length; Match m = re.Match(buffer); //判断章节 if (m.Success) { position += Environment.NewLine.Length; if (cp != null) { //是章节保存内容 cp.Content = sb.ToString(); sb.Remove(0, sb.Length); } cp = new CatalogPuple(); cp.Position = position; cp.Name = m.Value; cp.Begin = tb.Lines; if (catalogs.Count > 1) { catalogs[catalogs.Count - 1].End = cp.Begin - 1; } catalogs.Add(cp); } else { if (cp != null) { //不是章节则是内容 sb.AppendLine(buffer); } } } } sr.Close(); tb.Catalogs = catalogs; }