运用C#编写一个miniword
首先布局好相关的前端界面
相关的菜单栏的添加设置可以参考网上的其他文章。
然后设计子窗体
这些实现也比较简单。
再就是需要一些C#里自带的工具
主页面form1的代码块
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace miniword
{
public delegate void ClickPanel(string s);
public partial class Form1 : Form
{
public static int panel_count = 0;
public static Panel panel_active = null;
public static bool ishavefindpanel = false;
public static Form2 _findpanel = null;
public static string cookie_s = null;
public static bool cookie_isup = false;
public void Click_Panel(string s)
{
foreach (ToolStripItem con in this.窗口ToolStripMenuItem.DropDownItems)
{
if (con is ToolStripMenuItem)
{
if ((string)con.Tag == s)
{
con.Image = miniword.Properties.Resources.PENCIL03;
}
else
{
con.Image = null;
}
}
}
}
public void Update_Panel(string s, bool isdrop)
{
int p_c = this.MdiChildren.Length;
if(isdrop)
{
p_c = p_c - 1;
}
while(this.窗口ToolStripMenuItem.DropDownItems.Count >= 3)
{
this.窗口ToolStripMenuItem.DropDownItems.RemoveAt(2);
}
if (p_c >= 1)
{
System.Windows.Forms.ToolStripSeparator toolStripSeparator = new System.Windows.Forms.ToolStripSeparator();
this.窗口ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
toolStripSeparator});
}
int c_i = 0;
foreach (Form child in this.MdiChildren)
{
if (child.Text == s && isdrop) continue;
System.Windows.Forms.ToolStripMenuItem PanelToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
c_i++;
this.窗口ToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
PanelToolStripMenuItem});
PanelToolStripMenuItem.Name = child.Text + "ToolStripMenuItem";
PanelToolStripMenuItem.Size = new System.Drawing.Size(122, 26);
PanelToolStripMenuItem.Text = c_i + " " + child.Text;
PanelToolStripMenuItem.Click += new System.EventHandler(FileToolStripMenuItem_Click);
PanelToolStripMenuItem.Tag = child.Text;
if (child.Text != s)
{
PanelToolStripMenuItem.Image = null;
}
else
{
PanelToolStripMenuItem.Image = miniword.Properties.Resources.PENCIL03;
}
}
}
public void Create_Panel()
{
Panel newPanel = new Panel();
newPanel.TopLevel = false;
newPanel.Show();
newPanel.BringToFront();
newPanel.MdiParent = this;
newPanel.Tag = true;
newPanel.Focus();
Form1.panel_count++;
newPanel.Text = "新建文档" + Form1.panel_count;
newPanel.updatepanel += new UpdatePanel(Update_Panel);
newPanel.Activated += new System.EventHandler(PanelGetFocus);
this.Update_Panel(newPanel.Text, false);
Form1.panel_active = newPanel;
this.自动换行ToolStripMenuItem.Image = null;
}
public Form1()
{
InitializeComponent();
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
Create_Panel();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
Create_Panel();
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
{
MessageBox.Show("窗体为空", "关闭提示");
}
else
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
if ((bool)pt.Tag == true)
另存为ToolStripMenuItem_Click(sender, e);
else
{
pt.SaveOnly();
}
}
}
}
public static Panel GetActivePanel()
{
if (Form1.panel_count > 0)
{
return Form1.panel_active;
}
return null;
}
private void FileToolStripMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem tm = (ToolStripMenuItem)sender;
string s = (string)tm.Tag;
foreach (Form child in this.MdiChildren)
{
if (child.Text == s)
{
child.Focus();
}
}
this.Click_Panel(s);
}
private void PanelGetFocus(object sender, EventArgs e)
{
Panel pn = (Panel)sender;
string s = (string)pn.Text;
Form1.panel_active = pn;
this.Click_Panel(s);
if (pn.GetWordWrap())
{
this.自动换行ToolStripMenuItem.Checked = true;
}
else
{
this.自动换行ToolStripMenuItem.Checked = false;
}
}
private void 平铺ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
private void 层叠ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.Cascade);
}
private void 另存为ToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
if ((bool)pt.Tag == true)
pt.SaveNew();
}
}
private void 关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
pt.CloseOnly();
}
}
private void 全部关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Panel child in this.MdiChildren)
{
child.CloseOnly();
}
Form1.panel_active = null;
}
private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel newPanel = new Panel();
newPanel.TopLevel = false;
newPanel.MdiParent = this;
newPanel.Tag = true;
newPanel.OpenLocate();
newPanel.updatepanel += new UpdatePanel(Update_Panel);
newPanel.Activated += new System.EventHandler(PanelGetFocus);
this.Update_Panel(newPanel.Text, false);
this.自动换行ToolStripMenuItem.Image = null;
}
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (!Form1.ishavefindpanel)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
Form2 findpanel = new Form2(pt);
Form1.ishavefindpanel = true;
Form1._findpanel = findpanel;
//by brj
findpanel.Show();
}
}
else
{
if(Form1._findpanel != null)
{
Form1._findpanel.Focus();
}
}
}
private void 字体ToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
pt.SetFont();
}
}
private void 颜色ToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
pt.SetColor();
}
}
private void 自动换行ToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
if(this.自动换行ToolStripMenuItem.Image != null)
{
this.自动换行ToolStripMenuItem.Image = null;
}
else
{
this.自动换行ToolStripMenuItem.Image = miniword.Properties.Resources.KEY05;
}
pt.SetWordWrap();
}
}
private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
pt.ToSelectAll();
}
}
private void 插入日期时间ToolStripMenuItem_Click(object sender, EventArgs e)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
pt.ToInsert();
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
全部关闭ToolStripMenuItem_Click(sender, e);
Application.Exit();
}
private void 查找下一个ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (Form1.ishavefindpanel)
{
string ss = Form1._findpanel.GettextBox().Text.Trim();
bool type = false;
if (Form1._findpanel.GetcheckBox().Checked)
{
type = true;
}
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
if (!pt.FindWords(ss, type))
{
MessageBox.Show(this, "没有找到", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
else
{
if(Form1.cookie_s != null)
{
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
if (!pt.FindWords(Form1.cookie_s, Form1.cookie_isup))
{
MessageBox.Show(this, "没有找到", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
else
{
查找ToolStripMenuItem_Click(sender, e);
}
}
}
}
}
子页面panel的代码块
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 miniword
{
public delegate void UpdatePanel(string s, bool isdrop);
public partial class Panel : Form
{
int searchPos = -1;
public Panel()
{
InitializeComponent();
searchPos = 0;
}
public event UpdatePanel updatepanel;
private void Panel_Load(object sender, EventArgs e)
{
this.Focus();
richTextBox1.WordWrap = false;
}
private void Panel_FormClosed(object sender, FormClosedEventArgs e)
{
if (richTextBox1.Modified)
{
if (MessageBox.Show(this, "是否保存文档(" + this.Text + ")?", "提示信息", MessageBoxButtons.YesNo) == DialogResult.Yes)
SaveNew();
richTextBox1.Modified = false;
}
Form1.panel_active = null;
updatepanel(this.Text, true);
}
public void SaveNew()
{
if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
{
this.Text = saveFileDialog1.FileName;
string path = saveFileDialog1.FileName;
switch (saveFileDialog1.FilterIndex)
{
case 1:
richTextBox1.SaveFile(path, RichTextBoxStreamType.RichText);
break;
case 2:
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
break;
case 3:
richTextBox1.SaveFile(path, RichTextBoxStreamType.UnicodePlainText);
break;
default:
richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
break;
}
richTextBox1.Modified = false;
this.Tag = false;
updatepanel(this.Text, false);
}
}
public void SaveOnly()
{
string ext = this.Text.Substring(this.Text.LastIndexOf(".") + 1).ToLower();
if (ext == "rtf")
richTextBox1.SaveFile(this.Text, RichTextBoxStreamType.RichText);
richTextBox1.Modified = false;
}
public void CloseOnly()
{
this.Close();
}
public void OpenLocate()
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = openFileDialog1.FileName;
this.Text = path;
string ext = path.Substring(path.LastIndexOf('.') + 1).ToLower();
StreamReader sr = new StreamReader(path);
if (ext == "rtf")
richTextBox1.LoadFile(path, RichTextBoxStreamType.RichText);
sr.Close();
this.Show();
this.BringToFront();
this.Focus();
this.Tag = false;
richTextBox1.Modified = false;
Form1.panel_active = this;
}
}
public void SetFont()
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
richTextBox1.SelectionFont = fontDialog1.Font;
}
public void SetColor()
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
richTextBox1.SelectionColor = colorDialog1.Color;
}
public void SetWordWrap()
{
richTextBox1.WordWrap = !richTextBox1.WordWrap;
}
public bool GetWordWrap()
{
return richTextBox1.WordWrap;
}
public void ToSelectAll()
{
richTextBox1.SelectAll();
}
public void ToInsert()
{
string t = (string)System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
richTextBox1.SelectedText = t;
}
public RichTextBox GetRichBox()
{
return this.richTextBox1;
}
public bool FindWords(string ss, bool isuper)
{
if(richTextBox1.TextLength < searchPos)
{
searchPos = 0;
}
if(richTextBox1.TextLength == 0 || richTextBox1.TextLength < ss.Length)
{
return false;
}
RichTextBoxFinds rtbf = RichTextBoxFinds.None;
if (isuper)
{
rtbf |= RichTextBoxFinds.MatchCase;
}
if (ss == "") return false;
searchPos = richTextBox1.Find(ss, searchPos, -1, rtbf);
if (searchPos >= 0)
{
searchPos += ss.Length;
richTextBox1.Focus();
return true;
}
else
{
searchPos = 0;
}
searchPos = richTextBox1.Find(ss, searchPos, -1, rtbf);
if (searchPos >= 0)
{
searchPos += ss.Length;
richTextBox1.Focus();
//by brj
return true;
}
else
{
searchPos = 0;
return false;
}
}
}
}
当然,还需要查询的界面,可以再设置一个form2
该部分form2的代码块
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace miniword
{
public partial class Form2 : Form
{
Panel pn;
public Form2(Panel p)
{
InitializeComponent();
pn = p;
checkBox1.Checked = false;
if (Form1.cookie_s != null)
{
if (Form1.cookie_isup)
{
checkBox1.Checked = true;
textBox1.Text = Form1.cookie_s;
}
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1.ishavefindpanel = false;
Form1._findpanel = null;
if (checkBox1.Checked)
{
Form1.cookie_isup = true;
}
else
{
Form1.cookie_isup = false;
}
Form1.cookie_s = textBox1.Text;
//by brj
}
public TextBox GettextBox()
{
return textBox1;
}
public CheckBox GetcheckBox()
{
return checkBox1;
}
public Button Getbutton()
{
return button2;
}
private void button1_Click(object sender, EventArgs e)
{
string ss = Form1._findpanel.GettextBox().Text.Trim();
bool type = false;
if (Form1._findpanel.GetcheckBox().Checked)
{
type = true;
}
Panel pt = Form1.GetActivePanel();
if (pt != null)
{
if (!pt.FindWords(ss, type))
{
MessageBox.Show(this, "没有找到", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
然后就基本上可以做出来了,由于比较懒,部分的代码自行修改,还有一些控件的名称也可以对照代码研究一下,此篇以记录为主,提供一定的思路。
下载包文件点此处前往