实例介绍
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Printing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //Download by http://www.codefans.net namespace 多文档编辑器 { public partial class MainForm : Form { private int wCount = 0; private RichTextBoxStreamType oldFileType; private DocForm doc; private static DocForm docSend; private string fileName = string.Empty; private string filePath = string.Empty; List<DocForm> listDocForm = new List<DocForm>(); string _connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}\FileData.mdb", Environment.CurrentDirectory); public MainForm() { InitializeComponent(); } public static DocForm GetDocTrun() { return docSend; } private void tsmi_Format_Wrap_Click(object sender, EventArgs e) //自动换行 { try { if (tsmi_Format_Wrap.CheckState == CheckState.Checked) { tssl_FormCount.Text = string.Format(" 成功关闭自动换行功能 ......"); tsmi_Format_Wrap.CheckState = CheckState.Unchecked; DocForm df = (DocForm)this.ActiveMdiChild; df.Sourse.WordWrap = false; } else { tssl_FormCount.Text = string.Format(" 成功启动自动换行功能 ......"); tsmi_Format_Wrap.CheckState = CheckState.Checked; DocForm df = (DocForm)this.ActiveMdiChild; df.Sourse.WordWrap = true; } } catch (Exception ex) { return; } } private void tsmi_Format_Font_Click(object sender, EventArgs e) //字体大小设置 { tssl_FormCount.Text = string.Format(" 启动字体大小设置功能 ......"); try { if (fontDialog1.ShowDialog() == DialogResult.OK && doc != null) { DocForm df = (DocForm)this.ActiveMdiChild; df.Sourse.SelectionFont = fontDialog1.Font; } } catch (Exception ex) { return; } tssl_FormCount.Text = string.Format(" 关闭字体大小设置功能 ......"); } private void tsmi_Format_Colour_Click(object sender, EventArgs e) // 字体颜色设置 { tssl_FormCount.Text = string.Format(" 启动字体颜色设置功能 ......"); try { if (colorDialog1.ShowDialog() == DialogResult.OK && doc != null) { DocForm df = (DocForm)this.ActiveMdiChild; df.Sourse.SelectionColor = colorDialog1.Color; } } catch (Exception ex) { return; } } private void tsmi_File_NewCreate_Click(object sender, EventArgs e) //新建文件 { wCount ; doc = new DocForm(); listDocForm.Add(doc); doc.MdiParent = this; doc.Text = "文档" wCount; doc.Show(); if (wCount == 1) { doc.WindowState = FormWindowState.Maximized; } tssl_FormCount.Text = string.Format(" 成功创建第 {0} 个文件......当前文件名为: {1} ", this.MdiChildren.Length,doc.Text); } private void tsmi_File_Open_Click(object sender, EventArgs e) //打开文件 { tssl_FormCount.Text = string.Format(" 正在打开文件 ......"); try { if (openFileDialog1.ShowDialog() == DialogResult.OK) { RichTextBoxStreamType fileType = TrunFileType(openFileDialog1.FilterIndex); wCount ; doc = new DocForm(fileType, openFileDialog1.FileName, openFileDialog1.FilterIndex); doc.MdiParent = this; doc.Show(); listDocForm.Add(doc); } } catch (Exception ex) { return; } string str = openFileDialog1.FileName; string[] sArray = str.Split('\\'); doc.Text = sArray[sArray.Length - 1]; tssl_FormCount.Text = string.Format(" 成功打开文件,当前文件名为: {0} ", doc.Text); doc.WindowState = FormWindowState.Maximized; } private void tsmi_File_Save_Click(object sender, EventArgs e) //保存文件 { DocForm df = (DocForm)this.ActiveMdiChild; try { if (df.GetFilePath() == "") { tssl_FormCount.Text = string.Format(" 请选择保存路径"); SaveFile(df); //SaveDateToDateBase(df); MessageBox.Show("保存成功", "温馨提示"); tssl_FormCount.Text = string.Format(" {0} 保存成功 ", doc.Text); } else { RichTextBoxStreamType fileType = TrunFileType(df.GetFileTypeIndex()); df.Sourse.SaveFile(df.GetFilePath(), fileType); MessageBox.Show("保存成功", "温馨提示"); tssl_FormCount.Text = string.Format(" {0} 保存成功 ", doc.Text); } } catch(Exception ex) { MessageBox.Show("保存失败,请重新保存!","温馨提示"); } } private void tsmi_File_OtherSave_Click(object sender, EventArgs e) //另存为 { tssl_FormCount.Text = string.Format(" 请选择保存路径"); DocForm df = (DocForm)this.ActiveMdiChild; try { SaveFile(df); filePath = df.GetFilePath(); //SaveDateToDateBase(df); MessageBox.Show("保存成功","温馨提示"); } catch (Exception ex) { return; } tssl_FormCount.Text = string.Format(" {0} 保存成功 ", doc.Text); } private void tsmi_File_SaveToDateBase_Click(object sender, EventArgs e)//保存到数据库 { try { DocForm df = (DocForm)this.ActiveMdiChild; fileName = df.Text; if (filePath == "") { filePath = Environment.CommandLine; } SaveDateToDateBase(df); } catch (Exception ex) { return; } } private void tsmi_File_PrintPage_Click(object sender, EventArgs e) //打印设置 { tssl_FormCount.Text = string.Format(" 进行打印设置...... "); PrintDialog pd = new PrintDialog(); pd.ShowDialog(); } private void tsmi_File_PageSet_Click(object sender, EventArgs e) //页面设置 { tssl_FormCount.Text = string.Format(" 进行页面设置...... "); pageSetupDialog1.Document = new PrintDocument(); this.pageSetupDialog1.AllowMargins = true; this.pageSetupDialog1.AllowOrientation = true; this.pageSetupDialog1.AllowPaper = true; this.pageSetupDialog1.AllowPrinter = true; this.pageSetupDialog1.ShowDialog(); } private void tsmi_File_Exit_Click(object sender, EventArgs e) //退出程序 { Application.Exit(); } private void tsmi_Edit_Copy_Click(object sender, EventArgs e) //复制 { DocForm df = (DocForm)this.ActiveMdiChild; try { if (df.Sourse.SelectionLength > 0) { df.Sourse.Copy(); } } catch { return; } tssl_FormCount.Text = string.Format(" 复制成功 "); } private void tsmi_Edit_Stick_Click(object sender, EventArgs e) //粘贴 { DocForm df = (DocForm)this.ActiveMdiChild; try { df.Sourse.Paste(); tssl_FormCount.Text = string.Format(" 粘贴成功 "); } catch (Exception ex) { return; } } private void tsmi_Edit_Cut_Click(object sender, EventArgs e) //剪接 { DocForm df = (DocForm)this.ActiveMdiChild; try { if (df.Sourse.SelectionLength > 0) { df.Sourse.Cut(); } } catch (Exception ex) { return; } tssl_FormCount.Text = string.Format(" 剪接成功 "); } private void tsmi_Edit_Delete_Click(object sender, EventArgs e) //删除 { DocForm df = (DocForm)this.ActiveMdiChild; try { if (df.Sourse.SelectionLength > 0) { df.Sourse.SelectedText = ""; } } catch(Exception ex) { return; } tssl_FormCount.Text = string.Format(" 删除成功 "); } private void tsmi_Edit_Cancel_Click(object sender, EventArgs e) //撤销 { DocForm df = (DocForm)this.ActiveMdiChild; try { if (df.Sourse.CanUndo == true) { df.Sourse.Undo(); } } catch(Exception ex) { return; } tssl_FormCount.Text = string.Format(" 撤销成功 "); } private void tsmi_Edit_Find_Click(object sender, EventArgs e) //查找 { docSend = (DocForm)this.ActiveMdiChild; FindForm find = new FindForm(); find.Show(); tssl_FormCount.Text = string.Format(" 正在进行查找或替换 "); } private void tsmi_Edit_AllSelect_Click(object sender, EventArgs e) //全选 { try { DocForm df = (DocForm)this.ActiveMdiChild; df.Sourse.SelectAll(); tssl_FormCount.Text = string.Format(" 已全选...... "); } catch (Exception ex) { return; } } private void tsmi_Edit_Date_Click(object sender, EventArgs e) //日期时间 { try { DocForm df = (DocForm)this.ActiveMdiChild; Clipboard.SetText(DateTime.Now.ToString()); df.Sourse.Paste(); tssl_FormCount.Text = string.Format(" 输出日期和时间 "); } catch (Exception ex) { return; } } private void tsmi_Help_LookForHelp_Click(object sender, EventArgs e) //帮助信息 { tssl_FormCount.Text = string.Format(" 打开帮助信息......"); MessageBox.Show("感谢你的使用,详情请联系开发者!", "温馨提示"); } private void tsmi_Help_AboutTheSoft_Click(object sender, EventArgs e) //关于软件信息 { tssl_FormCount.Text = string.Format(" 查询软件信息......"); MessageBox.Show("软件版本:1.0正式版\n\r开发者:伍伟劲\n\r联系方式:fish_yuxi@hotmail.com", "温馨提示"); } private void tsmi_Check_Statue_Click(object sender, EventArgs e) //状态栏 { try { if (tsmi_Check_Statue.CheckState == CheckState.Checked) { tsmi_Check_Statue.CheckState = CheckState.Unchecked; stasp_StatueBar.Visible = false; } else { tsmi_Check_Statue.CheckState = CheckState.Checked; stasp_StatueBar.Visible = true; tssl_FormCount.Text = string.Format(" 启动状态栏功能"); } } catch (Exception ex) { return; } } public void ExecuteSql(string sql) //执行sql语句 { using (OleDbConnection conn = new OleDbConnection(_connectionString)) { using (OleDbCommand command = new OleDbCommand()) { try { conn.Open(); command.Connection = conn; command.CommandType = System.Data.CommandType.Text; command.CommandText = sql; command.ExecuteNonQuery(); } catch (Exception ex) { conn.Close(); } } } } public DataTable ReadDataToDataTable(string sql) //输出数据库数据 { using (OleDbConnection conn = new OleDbConnection(_connectionString)) { using (OleDbCommand command = new OleDbCommand()) { conn.Open(); command.Connection = conn; command.CommandType = System.Data.CommandType.Text; DataTable dt = new DataTable(); OleDbDataAdapter adapter = new OleDbDataAdapter(); command.CommandText = sql; adapter.SelectCommand = command; adapter.Fill(dt); return dt; } } } public void SaveFile(DocForm df) //保存文件 { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "文本文件(*.txt)|*.txt|RTF文件|*.rtf|所有文件(*.*)|*.*"; if (sfd.ShowDialog() == DialogResult.OK) { RichTextBoxStreamType fileType = TrunFileType(sfd.FilterIndex); doc.SetFileTypeIndex(sfd.FilterIndex); doc.SetFilePath(saveFileDialog1.InitialDirectory); df.Sourse.SaveFile(sfd.FileName, fileType); df.SetFilePath(sfd.FileName); oldFileType = fileType; } string str = df.GetFilePath(); string[] sArray = str.Split('\\'); fileName = sArray[sArray.Length - 1]; for (int i = 0; i < sArray.Length - 1; i ) { filePath = sArray[i]; } } public void SaveDateToDateBase(DocForm df) //保存到数据库 { try { DataTable dtPath = new DataTable(); dtPath = ReadDataToDataTable("select fNumber,fName from tFilePath"); DataTable dtContent = new DataTable(); dtContent = ReadDataToDataTable("select fNumber,fName from tFileContent"); if (dtPath.Rows.Count != 0) { for (int i = 0; i < dtPath.Rows.Count; i ) { if (fileName == (Convert.ToString(dtPath.Rows[i]["fName"]))) { int idPath = Convert.ToInt32(dtPath.Rows[i]["fNumber"]); ExecuteSql(string.Format("update tFilePath set fPath='{0}', where fNumber='{1}'", filePath, idPath)); } else ExecuteSql(string.Format("insert into tFilePath(fName,fPath) values('{0}','{1}')", fileName, filePath)); } } else ExecuteSql(string.Format("insert into tFilePath(fName,fPath) values('{0}','{1}')", fileName, filePath)); if (dtContent.Rows.Count != 0) { for (int i = 0; i < dtContent.Rows.Count; i ) { if (fileName == (Convert.ToString(dtContent.Rows[i]["fName"]))) { int idContent = Convert.ToInt32(dtContent.Rows[i]["fNumber"]); ExecuteSql(string.Format("update tFileContent set fContent='{0}',fDate='{1}', where fNumber='{2}'", df.Sourse.Text, DateTime.Now, idContent)); } else ExecuteSql(string.Format("insert into tFileContent(fName,fContent,fDate) values('{0}','{1}','{2}')", fileName, df.Sourse.Text, DateTime.Now)); } } else ExecuteSql(string.Format("insert into tFileContent(fName,fContent,fDate) values('{0}','{1}','{2}')", fileName, df.Sourse.Text, DateTime.Now)); MessageBox.Show("保存到数据库成功!", "温馨提示"); } catch (Exception ex) { MessageBox.Show("保存到数据库失败失败,请重新保存!","温馨提示"); } } public RichTextBoxStreamType TrunFileType(int i) //数据转换 { RichTextBoxStreamType fileType; switch (i) { case 1: fileType = RichTextBoxStreamType.PlainText; break; case 2: fileType = RichTextBoxStreamType.RichText; break; default: fileType = RichTextBoxStreamType.UnicodePlainText; break; } return fileType; } private void tsmi_File_History_Click(object sender, EventArgs e) { } private void MainForm_Load(object sender, EventArgs e) { } } }
标签: 图标
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论