该文档阅读器可打开\保存txt文件,进行窗口设置,打开的文件只读,可设置字体颜色、背景颜色、字体格式,具有首页、尾页、下一页、上一页和跳转的按钮,同时还可以设置每页显示的字符数。
Form1.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace example14._5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
openFileDialog1.InitialDirectory = @"桌面";
openFileDialog1.Filter="文本文件(*.txt)|*.txt";
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.myfilename = openFileDialog1.FileName;
f2.Text = new FileInfo(openFileDialog1.FileName).Name;//文件夹名字显示在form2
f2.Show();
}
}
private void 水平排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
private void 垂直排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
}
private void 层叠排列ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.Cascade);
}
private void 最大化ToolStripMenuItem_Click(object sender, EventArgs e)
{
//遍历所有子窗体
foreach(var item in this.MdiChildren)
{
item.WindowState = FormWindowState.Maximized;
}
}
private void 最小化ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach(var item in this.MdiChildren)
{
item.WindowState = FormWindowState.Minimized;
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach(var item in this.MdiChildren)
{
item.Close();
}
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.MdiParent = this;
f3.Text = "未命名";
f3.Show();
}
}
}
Form2.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace example14._5
{
public partial class Form2 : Form
{
private int currentPage;//当前页
private int totalPage;//总页数
private int lengthPerPage;//每页字符数
public Form2()
{
InitializeComponent();
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
lengthPerPage = Convert.ToInt32(numericUpDown2.Value);
totalPage = mytxt.Length / lengthPerPage + 1;
numericUpDown1.Maximum = totalPage;
}
public string myfilename;
private string mytxt;
private void Form2_Load(object sender, EventArgs e)
{
mytxt = File.ReadAllText(myfilename);
//赋值
currentPage = 1;
lengthPerPage = Convert.ToInt32(numericUpDown2.Value);
totalPage = mytxt.Length / lengthPerPage + 1;
numericUpDown1.Maximum = totalPage;
numericUpDown1.Minimum = 1;
//刷新页面
ReadText();
}
//字符串的截取和显示函数
private void ReadText()
{
//当文件长度 < 每页显示字符数 (只有一夜)
if (mytxt.Length < lengthPerPage)
{
textBox1.Text = mytxt;
numericUpDown1.Value = 1;
}
//最后一页
else if (currentPage==totalPage)//当前页等于总页数,即最后一页
{
textBox1.Text = mytxt.Substring((totalPage-1) * lengthPerPage);
numericUpDown1.Value = totalPage;
}
//正常:按每页字符数截取
else
{
int startIndex = (currentPage - 1) * lengthPerPage;
textBox1.Text = mytxt.Substring(startIndex, lengthPerPage);
numericUpDown1.Value = currentPage;
}
}
private void btnFirstPage_Click(object sender, EventArgs e)
{
currentPage = 1;
ReadText();
}
private void btnPrePage_Click(object sender, EventArgs e)
{
currentPage--;
if (currentPage > 0)
{
ReadText();
}
else
{
currentPage = 1;
MessageBox.Show("已经是第一页了!", "提示");
}
}
private void btnNextPage_Click(object sender, EventArgs e)
{
currentPage++;
if (currentPage <= totalPage)
{
ReadText();
}
else
{
currentPage = totalPage;
MessageBox.Show("已经是最后一页了!", "提示");
}
}
private void btnLastPage_Click(object sender, EventArgs e)
{
currentPage = totalPage;
ReadText();
}
private void btnJump_Click(object sender, EventArgs e)
{
currentPage = Convert.ToInt32(numericUpDown1.Value);
ReadText();
}
private void 文档背景色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.BackColor = colorDialog1.Color;
}
}
private void 文字颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.ForeColor = colorDialog1.Color;
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Font = fontDialog1.Font;
}
}
}
}
Form3.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace example14._5
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
saveFileDialog1.InitialDirectory = @"桌面";
saveFileDialog1.Filter = "文本文件(*.txt)|*.txt";
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saveFileDialog1.FileName, textBox1.Text);
this.Text = saveFileDialog1.FileName;
}
}
}
}