?? mainform.cs
字號:
this.MdiChildren[this.CurrentFormIndexOf()].Activate(); //激活該子窗口
this.CheckActiveMdiChild(); //設置控件的可見性
}
#endregion
#region 主窗口標題欄事件處理的方法
#region 文件--文件操作
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
newfileForm nf = new newfileForm(this);
nf.ShowDialog(this);
if (this.newfileType != "null")
{
// 創建一個子窗口對象,用來實現新建一個文檔
childForm childForm = new childForm(this);
childForm.fileType = this.newfileType;
childForm.fileName = "文檔" + (++count).ToString() + childForm.fileType;
childForm.isNewDoc = true;
childForm.Name = childForm.fileName;
childForm.Text = childForm.fileName;
childForm.MdiParent = this;
childForm.Show();
if (childForm.fileType == ".rtf")
childForm.Icon = Properties.Resources.RtfIcon;
else
childForm.Icon = Properties.Resources.TxtIcon;
this.currentForm = childForm; //設置當前子窗口為活動窗口
this.NewTabPage(); //新建標簽頁
this.MdiChildren[this.CurrentFormIndexOf()].Activate(); //激活該子窗口
this.CheckActiveMdiChild(); //設置控件的可見性
}
}
private void 打開ToolStripMenuItem_Click(object sender, EventArgs e)
{
// 彈出打開文件的對話框
this.openFileDialog1.Filter = "RTF 文檔(*.rtf)|*.rtf|文本文檔(*.txt)|*.txt|所有文件(*.*)|*.*";
this.openFileDialog1.FileName = null;
if (this.openFileDialog1.ShowDialog() == DialogResult.OK && this.openFileDialog1.FileName.Length > 0)
{
// 創建一個子窗口對象,用來實現打開一個文檔,并設置該子窗口為活動窗口
childForm childForm = new childForm(this);
childForm.filePath = openFileDialog1.FileName;
FileInfo finfo = new FileInfo(childForm.filePath);
childForm.fileName = finfo.Name;
childForm.fileType = finfo.Extension;
childForm.isNewDoc = false;
switch (finfo.Extension)
{
case ".rtf":
childForm.richTextBox1.LoadFile(childForm.filePath, RichTextBoxStreamType.RichText);
childForm.Icon = Properties.Resources.RtfIcon;
break;
case ".txt":
childForm.richTextBox1.LoadFile(childForm.filePath, RichTextBoxStreamType.PlainText);
childForm.Icon = Properties.Resources.TxtIcon;
break;
default:
MessageBox.Show("不能打開此格式的文件,請檢查后再試!", "Notepad", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
childForm.Name = childForm.fileName;
childForm.Text = childForm.fileName;
childForm.MdiParent = this;
childForm.Show();
this.currentForm = childForm; //設置當前子窗口為活動窗口
this.NewTabPage(childForm.fileName);//新建標簽頁
this.MdiChildren[this.CurrentFormIndexOf()].Activate(); //激活該子窗口
this.CheckActiveMdiChild(); //設置控件的可見性
}
}
private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
{
//保存文件
if (this.MdiChildren.Length == 0)
return;
if (this.currentForm.isNewDoc == true || this.currentForm.filePath == "")
{
this.另存為ToolStripMenuItem.PerformClick();
}
else
{
if (this.currentForm.fileType == ".rtf")
{
this.currentForm.richTextBox1.SaveFile(this.currentForm.filePath, RichTextBoxStreamType.RichText);
this.currentForm.Icon = Properties.Resources.RtfIcon;
}
if (this.currentForm.fileType == ".txt")
{
this.currentForm.richTextBox1.SaveFile(this.currentForm.filePath, RichTextBoxStreamType.PlainText);
this.currentForm.Icon = Properties.Resources.TxtIcon;
}
this.currentForm.Text = this.currentForm.fileName.Replace("*", "");//去除標志未保存的*號
}
}
private void 另存為ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
// 彈出保存文件的對話框
this.saveFileDialog1.Filter = "RTF 文檔(*.rtf)|*.rtf|文本文檔(*.txt)|*.txt|所有文件(*.*)|*.*";
this.saveFileDialog1.FileName = null;
if (this.saveFileDialog1.ShowDialog() == DialogResult.OK && this.saveFileDialog1.FileName.Length > 0)
{
this.currentForm.filePath = saveFileDialog1.FileName;
FileInfo finfo = new FileInfo(this.currentForm.filePath);
this.currentForm.fileName = finfo.Name;
this.currentForm.fileType = finfo.Extension;
this.currentForm.isNewDoc = false;
switch (finfo.Extension)
{
case ".rtf":
this.currentForm.richTextBox1.SaveFile(this.currentForm.filePath, RichTextBoxStreamType.RichText);
this.currentForm.Icon = Properties.Resources.RtfIcon;
break;
case ".txt":
this.currentForm.richTextBox1.SaveFile(this.currentForm.filePath, RichTextBoxStreamType.PlainText);
this.currentForm.Icon = Properties.Resources.TxtIcon;
break;
default:
MessageBox.Show("不能保存為此格式的文件!", "Notepad", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
this.currentForm.Name = this.currentForm.fileName;
this.currentForm.Text = this.currentForm.fileName;
this.ReSetTabPageName(this.CurrentFormIndexOf(), this.currentForm.fileName); //重新設置標簽的名稱
}
}
private void 打印設置ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
this.printDialog1.Document = this.printDocument1;
this.printDialog1.ShowDialog();
}
private void 打印預覽ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
//設置Document屬性
this.printPreviewDialog1.Document = this.printDocument1;
try
{
//顯示打印預覽窗口
this.printPreviewDialog1.ShowDialog();
}
catch (Exception excep)
{
//顯示打印出錯消息
MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void 打印ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
this.printDialog1.Document = this.printDocument1;
StringReader reader = new StringReader(this.currentForm.richTextBox1.Text);
if (this.printDialog1.ShowDialog() == DialogResult.OK)
{
try
{
this.printDocument1.Print();
}
catch (Exception excep)
{
MessageBox.Show(excep.Message, "打印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.printDocument1.PrintController.OnEndPrint(this.printDocument1, new PrintEventArgs());
}
}
}
private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
//this.MainForm_FormClosing(sender, e);
}
#endregion
#region 編輯--文本內容
private void 撤消ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
currentForm.Undo();
}
private void 恢復ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
currentForm.Redo();
}
private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
currentForm.Cut();
}
private void 復制ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
currentForm.Copy();
}
private void 粘貼ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
currentForm.Paste();
}
private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
currentForm.Delete();
}
private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
searchAndreplaceForm sp = new searchAndreplaceForm(this, "tabPageSearch");
sp.ShowDialog(this);
}
private void 替換ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
searchAndreplaceForm sp = new searchAndreplaceForm(this, "tabPageReplace");
sp.ShowDialog(this);
}
private void 全選ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
currentForm.SelectAll();
}
private void 時間和日期ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
datetimeForm dt = new datetimeForm(this);
dt.ShowDialog(this);
}
#endregion
#region 格式--設置文本格式
private void 自動換行AToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
if (自動換行AToolStripMenuItem.Checked)
{
this.currentForm.richTextBox1.WordWrap = false;
自動換行AToolStripMenuItem.Checked = false;
}
else
{
this.currentForm.richTextBox1.WordWrap = true;
自動換行AToolStripMenuItem.Checked = true;
}
this.currentForm.richTextBox1_Click(sender, e);
}
private void fontDialog1_Apply(object sender, EventArgs e) //“應用”按鈕事件
{
FontDialog fontDialog1 = (FontDialog)sender;
this.currentForm.richTextBox1.SelectionFont = this.fontDialog1.Font;
}
private void 字體ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
this.fontDialog1.Apply += new EventHandler(this.fontDialog1_Apply);
if (this.fontDialog1.ShowDialog() == DialogResult.OK)
{
this.currentForm.richTextBox1.SelectionFont = this.fontDialog1.Font;
}
}
private void 顏色ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
if (this.colorDialog1.ShowDialog() == DialogResult.OK)
{
this.currentForm.richTextBox1.SelectionColor = this.colorDialog1.Color;
}
}
private void 項目符號樣式ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (項目符號樣式ToolStripMenuItem.Checked == true)
{
this.currentForm.richTextBox1.SelectionBullet = false;
項目符號樣式ToolStripMenuItem.Checked = false;
tsbProject.Checked = false;
}
else
{
this.currentForm.richTextBox1.SelectionBullet = true;
項目符號樣式ToolStripMenuItem.Checked = true;
tsbProject.Checked = true;
}
}
private void 段落ToolStripMenuItem_Click(object sender, EventArgs e)
{
paragraphForm pf = new paragraphForm(this);
pf.ShowDialog(this);
}
private void 左對齊ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
this.currentForm.SetLeftAlign();
左對齊ToolStripMenuItem.Checked = true;
居中ToolStripMenuItem.Checked = false;
右對齊ToolStripMenuItem.Checked = false;
}
private void 居中ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
this.currentForm.SetMiddleAlign();
左對齊ToolStripMenuItem.Checked = false;
居中ToolStripMenuItem.Checked = true;
右對齊ToolStripMenuItem.Checked = false;
}
private void 右對齊ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.MdiChildren.Length == 0)
return;
this.currentForm.SetRightAlign();
左對齊ToolStripMenuItem.Checked = false;
居中ToolStripMenuItem.Checked = false;
右對齊ToolStripMenuItem.Checked = true;
}
#region 填充顯示比例數據
private void GetDisplayRatio()
{
int[] displayRation = new int[] { 10, 25, 50, 75, 100, 150, 200, 500 };
for (int i = 0; i < displayRation.Length; i++)
tscbDisplayRatio.Items.Add(displayRation[i].ToString() + "%");
}
private void 顯示比例ToolStripMenuItem_Click(object sender, EventArgs e)
{
displayratioForm dr = new displayratioForm(this);
dr.ShowDialog(this);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -