C# WindowsForm编程:文档阅读器

        该文档阅读器可打开\保存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;
            }
        }
    }
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏轼轼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值