C#界面设计之文本阅读器的设计

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chongshangyunxiao321/article/details/51209808

下面来个比较大的实例,通过它的学习,可以快速掌握各种控件的联合使用,先来效果图:
这里写图片描述

主要代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace MultiTextReader
{
    public partial class FrmReader : Form
    {
        public FrmReader(string fileName)
        {
            this._FileName = fileName;              //更新当前打开文件的文件名
            InitializeComponent( );            
        }
        //表示当前子窗体所打开文件的文件名
        private string _FileName;
        //窗体加载时显示加载并显示文件内容
        private void FrmReader_Load(object sender, EventArgs e)
        {
            this.Text = this._FileName;             //设置当前窗体的标题
            StreamReader sr;                        //文件流对象sr
            sr = new StreamReader(this._FileName, Encoding.Default);    //打开指定文件
            while (!sr.EndOfStream)                 //如果文件没有读取完成继续读取
            {
                string line = sr.ReadLine( );       //从文件读取一行
                this.tbTexts.AppendText(line);      //显示文件数据到文本框
                this.tbTexts.AppendText(System.Environment.NewLine);         
            }
            sr.Close( );                            //关闭文件
        }

        public void SetViewSettings(Color bkColor, Color freColor, Font fnt)
        {
            this.tbTexts.BackColor = bkColor;
            this.tbTexts.ForeColor = freColor;
            this.tbTexts.Font = fnt;
        }
    }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MultiTextReader
{
    public partial class FrmMain : Form
    {
        private int childFormNumber = 0;

        public FrmMain( )
        {
            InitializeComponent( );
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            Form childForm = new Form( );
            childForm.MdiParent = this;
            childForm.Text = "窗口 " + childFormNumber++;
            childForm.Show( );
        }

        private void OpenFile(object sender, EventArgs e)
        {
            OpenFileDialog ofdlg = new OpenFileDialog( );       //创建OpenFileDialog对象ofdlg
            //设置打开文件对话框的默认路径为“我的文档”
            ofdlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            ofdlg.Filter = "文本文件(*.txt)|*.txt";            //设置只接受txt为扩展名的文件
            if (ofdlg.ShowDialog(this) == DialogResult.OK)      //显示打开文件对话框
            {
                string fileName = ofdlg.FileName;               //获取要打开的文件名
                FrmReader frm = new FrmReader(fileName);        //创建查看文件的子窗体frm
                frm.MdiParent = this;                           //设置子窗体frm的父窗体为当前窗体
                frm.Show( );                                    //显示子窗体
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog( );
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close( );
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close( );
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox dlg = new AboutBox( );
            dlg.ShowDialog(this);
        }

        private Color _ViewBackColor = Color.White;             //阅读的背景色字段及默认值
        private Color _ViewForeColor = Color.Black;             //阅读的前景色字段及默认值
        private Font _ViewFont = SystemFonts.DefaultFont;       //阅读的字体字段及默认值
        //"选项"菜单处理函数,打开SettingDlg,并将新参数更新到已打开的子窗体
        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SettingDlg dlg = new SettingDlg(this._ViewBackColor,    //用当前阅读参数创建SettingDlg对象dlg
                                            this._ViewForeColor, 
                                            this._ViewFont);
            if (dlg.ShowDialog(this) == DialogResult.OK)            //显示参数设置对话框,并等待用户设置
            {
                this._ViewBackColor = dlg.GetBackColor( );          //获取用户最新的阅读参数
                this._ViewFont = dlg.GetFont( );
                this._ViewForeColor = dlg.GetForeColor( );
                foreach (Form frm in this.MdiChildren)              //遍历所有子窗体
                {
                    FrmReader frmRd = frm as FrmReader;             //如果子窗体是FrmReader类型
                    if (frmRd != null)                              //则更新最新参数到子窗体
                    {
                        frmRd.SetViewSettings(this._ViewBackColor, this._ViewForeColor, this._ViewFont);
                    }
                }
            }
        }

    }
}

源代码已上传:http://download.csdn.net/detail/chongshangyunxiao321/9498206

展开阅读全文

没有更多推荐了,返回首页