学C#也学了好多些日子了,最近写了个小说阅读器来练练手。或许写的比较水,还请大家多多包涵啦~
部分界面截图:
现在面临一个问题:
书签功能和自动翻页功能不会写..
以下是核心代码(Main.cs)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Diagnostics;
namespace WordReader
{
public partial class Main : Form
{
bool AutoScr;
int SQ;
string nowPath,FullPath,FileHash;
[System.Runtime.InteropServices.DllImport("user32")]
static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
const int AW_HOR_POSITIVE = 0x0001;
const int AW_HOR_NEGATIVE = 0x0002;
const int AW_VER_POSITIVE = 0x0004;
const int AW_VER_NEGATIVE = 0x0008;
const int AW_CENTER = 0x0010;
const int AW_HIDE = 0x10000;
const int AW_ACTIVATE = 0x20000;
const int AW_SLIDE = 0x40000;
const int AW_BLEND = 0x80000;
public Main()
{
InitializeComponent();
}
private void Main_Load(object sender, EventArgs e)
{
if (File.Exists(@"C:\TextReader\cache\key.dat"))
{
DES des = new DES();
MachineCode m = new MachineCode();
StreamReader sr = new StreamReader(@"C:\TextReader\cache\key.dat");
string str1 = m.GetHDid() + m.GetCpuInfo().Trim().ToUpper();
str1 = des.Encrypt(str1, "********");
string t1 = sr.ReadLine();
string t2 = sr.ReadLine();
string str2 = des.Encrypt(t1, "********");
str2 = str2.Substring(5, 5) + "-" + str2.Substring(8, 5) + "-" + str2.Substring(14, 5) + "-" + str2.Substring(20, 5) + "-" + str2.Substring(23, 5);
str2 = str2.ToUpper();
sr.Close();
sr.Dispose();
if (t1 == str1)
{
if (t2 == str2)
{
RegED();
}
}
}
Welcome w = new Welcome();
w.ShowDialog();
}
private void RegED()
{
tabControl1.TabPages.Remove(tabPage2);
tabControl1.TabPages.Remove(tabPage3);
toolStripMenuItem2.Enabled = false;
toolStripMenuItem2.Text = "已注册,谢谢支持!";
}
private void tabPage2_Enter(object sender, EventArgs e)
{
tabControl1.TabPages.Add("Test");
}
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "打开";
openFileDialog1.FileName = "";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fname = openFileDialog1.FileName;
richTextBox1.LoadFile(fname, RichTextBoxStreamType.PlainText);
FullPath = openFileDialog1.FileName;
GetFileInformation(this.FullPath);
this.Text = "爱读TXT 小说阅读器 - " + GetFileName(openFileDialog1.FileName);
LB_NowFile.Text = "当前文件:" + GetFileName(openFileDialog1.FileName) + " 字数:" + richTextBox1.TextLength + "字";
if (File.Exists("c:\\TextReader\\cache\\" + FileHash + ".dat"))
{
if (MessageBox.Show("检测到有书签,是否读取?", "发现书签", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
StreamReader sr = new StreamReader("c:\\TextReader\\cache\\" + FileHash + ".dat");
richTextBox1.SelectionStart = Convert.ToInt32(sr.ReadLine());
sr.Close();
sr.Dispose();
}
}
}
private void 添加书签ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(!Directory.Exists(@"C:\TextReader"))
{
Directory.CreateDirectory(@"C:\TextReader");
}
if (!Directory.Exists(@"C:\TextReader\cache\"))
{
Directory.CreateDirectory(@"C:\TextReader\cache\");
}
FileStream fs = new FileStream(@"C:\TextReader\cache\" + FileHash + ".dat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(richTextBox1.SelectionStart);
sw.Close();
fs.Close();
SQ = richTextBox1.SelectionStart ;
}
private void 回到书签位置ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (File.Exists("c:\\TextReader\\cache\\" + FileHash + ".dat"))
{
StreamReader sr = new StreamReader("c:\\TextReader\\cache\\" + FileHash + ".dat");
richTextBox1.SelectionStart = Convert.ToInt32(sr.ReadLine());
sr.Close();
sr.Dispose();
}
}
private void GetFileInformation(string name)
{
FileInfo fi = new FileInfo(name);
FileHash = fi.Name + fi.Length;
FileHash = FileHash.Replace(".txt", "-");
}
private string GetFileName(string name)
{
FileInfo fi = new FileInfo(name);
return fi.Name;
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "打开";
openFileDialog1.FileName = "";
openFileDialog1.FilterIndex = 1;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fname = openFileDialog1.FileName;
richTextBox1.LoadFile(fname, RichTextBoxStreamType.PlainText);
FullPath = openFileDialog1.FileName;
GetFileInformation(this.FullPath);
this.Text = "爱读TXT 小说阅读器 - " + GetFileName(openFileDialog1.FileName);
LB_NowFile.Text = "当前文件:" + GetFileName(openFileDialog1.FileName) + " 字数:" + richTextBox1.TextLength + "字";
if (File.Exists("c:\\TextReader\\cache\\" + FileHash + ".dat"))
{
if (MessageBox.Show("检测到有书签,是否读取?", "发现书签", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
StreamReader sr = new StreamReader("c:\\TextReader\\cache\\"+FileHash+".dat");
richTextBox1.SelectionStart = Convert.ToInt32(sr.ReadLine());
sr.Close();
sr.Dispose();
}
}
}
private void NowTime_Tick(object sender, EventArgs e)
{
LB_NowTime.Text = "当前时间:"+DateTime.Now.ToString();
}
private void richTextBox1_MouseMove(object sender, MouseEventArgs e)
{
TB_RichIndex.Text = richTextBox1.SelectionStart.ToString();
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
richTextBox1.SelectionStart = Convert.ToInt32(TB_RichIndex.Text);
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
LB_FolderIndex.Text = "当前目录:\t\n"+folderBrowserDialog1.SelectedPath;
try
{
DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath.ToString());
FileInfo[] txtFile = di.GetFiles("*.txt");
for (int i = 0; i < txtFile.Length; i++)
{
listBox1.Items.Add(txtFile[i].Name);
}
nowPath = folderBrowserDialog1.SelectedPath.ToString();
}
catch (Exception ex)
{
MessageBox.Show("出错啦!" + ex.Message);
}
}
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
try
{
DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath.ToString());
FileInfo[] txtFile = di.GetFiles("*.txt");
for (int i = 0; i < txtFile.Length; i++)
{
listBox1.Items.Add(txtFile[i].Name);
}
nowPath = folderBrowserDialog1.SelectedPath.ToString();
}
catch (Exception ex)
{
MessageBox.Show("您还没选择文件夹呢!" + ex.Message);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
LB_NowFile.Text = "正在读取文件...";
string fname = listBox1.SelectedItem.ToString();
richTextBox1.LoadFile(nowPath + "\\" + fname, RichTextBoxStreamType.PlainText);
this.Text = "爱读TXT 小说阅读器 - " + GetFileName(openFileDialog1.FileName);
LB_NowFile.Text = "当前文件:" + GetFileName(openFileDialog1.FileName) + " 字数:" + richTextBox1.TextLength + "字";
FullPath = nowPath + "\\" + fname;
GetFileInformation(this.FullPath);
if (File.Exists("c:\\TextReader\\cache\\" + FileHash + ".dat"))
{
if (MessageBox.Show("检测到有书签,是否读取?", "发现书签", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
return;
}
StreamReader sr = new StreamReader("c:\\TextReader\\cache\\" + FileHash + ".dat");
richTextBox1.SelectionStart = Convert.ToInt32(sr.ReadLine());
sr.Close();
sr.Dispose();
}
}
catch (Exception ex)
{
LB_NowFile.Text = "无法读取文件! "+ex.Message;
}
}
private void 视力保护模式ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.ForeColor = Color.Black;
this.richTextBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(128)))));
}
private void 夜间模式ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.BackColor = Color.Black;
this.richTextBox1.ForeColor = Color.White;
}
private void 默认模式ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.richTextBox1.BackColor = System.Drawing.Color.SeaShell;
this.richTextBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(255)))));
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Set set = new Set();
set.ShowDialog();
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
if (FullPath == "" || FullPath == null)
{
MessageBox.Show("您还没打开文件呢!","错误啦!",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
if (MessageBox.Show("您真的想删除这个文件吗?\r\n" + FullPath, "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)==DialogResult.Yes)
{
File.Delete(FullPath);
richTextBox1.Text = "注意:文件已被删除!";
}
}
private void 关于AToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 ab = new AboutBox1();
ab.ShowDialog();
}
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
{
exit();
}
private void richTextBox1_DoubleClick(object sender, EventArgs e)
{
if (AutoScr)
{
TM_AutoScr.Enabled = false;
AutoScr = false;
}
else
{
TM_AutoScr.Enabled = true;
AutoScr = true;
}
}
private void TM_AutoScr_Tick(object sender, EventArgs e)
{
richTextBox1.SelectionStart++;
}
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
exit();
}
private void exit()
{
AnimateWindow(this.Handle, 1000, AW_SLIDE | AW_HIDE | AW_VER_NEGATIVE);
Environment.Exit(1);
}
private void Web1(object sender, EventArgs e)
{
webBrowser1.Url = new Uri("http://hao.xloli.net");
webBrowser1.Refresh();
}
private void button2_Click(object sender, EventArgs e)
{
webBrowser2.Url = new Uri("http://pindao.huoban.taobao.com/tms/channel/onsale.htm?pid=mm_11275696_0_0&eventid=101586");
webBrowser2.Refresh();
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
Process.Start("IEXPLORE.EXE", "http://www.xloli.net");
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
Reg r = new Reg();
r.ShowDialog();
r.Dispose();
}
}
}
声明:本代码仅供个人学习与参考,可以转载,但是禁止用于商业用途。转载本代码必须在明显位置著名出处。否则产生的一切后果自负!!
EXE下载(需要 .Net2.0 运行库):
%xyq95 %xyq95 大话的表情怪好看的。
个人最喜欢AlReader2这款阅读器。