📄 mdichild.cs
字号:
this.menuItem13.Text = "-";
//
// Del
//
this.Del.Index = 5;
this.Del.Text = "删除(&D)";
this.Del.Click += new System.EventHandler(this.Del_Click);
//
// undo
//
this.undo.Index = 6;
this.undo.Text = "撤销(&U)";
this.undo.Click += new System.EventHandler(this.undo_Click);
//
// MDIChild
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(424, 421);
this.Controls.Add(this.printPreviewControl1);
this.Controls.Add(this.richTextBox1);
this.Menu = this.mainMenu1;
this.Name = "MDIChild";
this.Text = "MDIChild";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Closing += new System.ComponentModel.CancelEventHandler(this.MDIChild_Closing);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MDIChild_FormClosing);
this.Load += new System.EventHandler(this.MDIChild_Load);
this.ResumeLayout(false);
}
#endregion
public void CheckSave()
{
if(this.richTextBox1.Text!=null)
{
if(MessageBox.Show("是否保存当前文档","确认保存",MessageBoxButtons.YesNoCancel)==DialogResult.Yes)
{
menuItem2_Click(null,null);
}
}
}
private void menuItem2_Click(object sender, System.EventArgs e)
{
SaveFileDialog sfDlg=new SaveFileDialog();
sfDlg.Filter="Rich Text format files(*.rtf)|*.rtf|All files(*.*)|*.*";
sfDlg.FilterIndex=1;
sfDlg.DefaultExt="rtf";
sfDlg.AddExtension=true;
sfDlg.RestoreDirectory=true;
if(sfDlg.ShowDialog()==DialogResult.OK)
{
richTextBox1.SaveFile(sfDlg.FileName);
this.Text=sfDlg.FileName;
}
}
private void menuItem3_Click(object sender, System.EventArgs e)
{
if(this.pageSetupDialog1.ShowDialog()==DialogResult.OK)
try
{
this.pageSetupDialog1.ShowDialog();
}
catch
{
}
}
private void MDIChild_Load(object sender, System.EventArgs e)
{
this.printPreviewControl1.Hide();
// Class1.RTBox=this.richTextBox1.SaveFile(Class1.file);
}
// 剪切的操作
private void menuItem8_Click(object sender, System.EventArgs e)
{
if(this.richTextBox1.SelectedText!="")
{
this.richTextBox1.Cut();
}
}
//复制所选的文本
private void menuItem9_Click(object sender, System.EventArgs e)
{
if(this.richTextBox1.SelectionLength>0)
{
this.richTextBox1.Copy();
}
}
//粘贴操作
private void menuItem10_Click(object sender, System.EventArgs e)
{
if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)==true)
{
if(this.richTextBox1.SelectionLength>0)
{
if(MessageBox.Show("你想覆盖掉选择的文本吗?","确认覆盖",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.Cancel)
{
this.richTextBox1.SelectionStart=this.richTextBox1.SelectionStart+this.richTextBox1.SelectionLength;
}
}
this.richTextBox1.Paste();
}
}
//删除文本
private void menuItem11_Click(object sender, System.EventArgs e)
{
try
{
this.richTextBox1.SelectedText.Remove(1,this.richTextBox1.SelectionLength);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// 全选操作
private void menuItem12_Click_1(object sender, System.EventArgs e)
{
this.richTextBox1.SelectAll();
}
// 撤销操作
private void Un_Click(object sender, System.EventArgs e)
{
if(this.richTextBox1.CanUndo==true)
{
this.richTextBox1.Undo();
this.richTextBox1.ClearUndo();
}
}
//打印
private void menuItem6_Click(object sender, System.EventArgs e)
{
MySReader=new StringReader(this.richTextBox1.Text);
if(this.printDialog1.ShowDialog()==DialogResult.OK)
{
try
{
this.printDocument1.Print();
}
catch
{
this.printDocument1.PrintController.OnEndPrint(this.printDocument1,new System.Drawing.Printing.PrintEventArgs());
}
}
}
private void menuItem5_Click(object sender, System.EventArgs e)
{
StringReader lineReader=new StringReader(this.richTextBox1.Text);
if(this.printPreviewDialog1.ShowDialog()==DialogResult.OK)
{
try
{
this.printPreviewControl1.Show();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"打印出错",MessageBoxButtons.OK,MessageBoxIcon.Error);
this.printDocument1.PrintController.OnEndPrint(this.printDocument1,new System.Drawing.Printing.PrintEventArgs());
}
}
}
private void MyPrintDC_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Graphics MyGraphics = e.Graphics ;
Font MyPrintFont = this.richTextBox1.Font ;
//每一页的行数
float iLinePerPage = e.MarginBounds.Height / MyPrintFont.GetHeight (MyGraphics);
int iLineNumber = 0; //打印时的行计数器
float fYPosition = 0;//打印时的纵坐标
float fMarginLeft = e.MarginBounds .Left ;
float fMarginTop = e.MarginBounds .Top;
string strLine = ""; //每一行要打印的文本
while ((iLineNumber < iLinePerPage) &&
((strLine = MySReader.ReadLine ())!=null))
{
fYPosition = fMarginTop + iLineNumber * MyPrintFont.GetHeight(MyGraphics);
MyGraphics.DrawString (strLine, MyPrintFont, new SolidBrush (Color.Black ),fMarginLeft ,fYPosition,new StringFormat ());
iLineNumber ++;
}
if (strLine!= null)
{
e.HasMorePages = true;//没有打印完,出发下一次PrintPage事件
}
else
{
e.HasMorePages = false;
}
}
// 上下文菜单的操作
private void menuItem12_Click(object sender, System.EventArgs e)
{
this.menuItem9_Click(null,null);
}
private void paste_Click(object sender, System.EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
richTextBox1.SelectedText = Clipboard.GetData(DataFormats.Text).ToString();
}
private void cut_Click(object sender, System.EventArgs e)
{
this.menuItem8_Click(null,null);
}
private void seleAll_Click(object sender, System.EventArgs e)
{
this.menuItem12_Click_1(null,null);
}
private void undo_Click(object sender, System.EventArgs e)
{
this.undo_Click(null,null);
}
private void Del_Click(object sender, System.EventArgs e)
{
this.menuItem11_Click(null,null);
}
private void menuItem4_Click(object sender, System.EventArgs e)
{
PrintDialog printDialog=new PrintDialog();
printDialog.Document=this.printDocument1;
printDialog.ShowDialog();
}
//关闭窗口的提示
private void MDIChild_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if(MessageBox.Show("要关闭窗体吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information)==DialogResult.OK)
{
}
else
{
e.Cancel=true;
}
}
private void menuItemEdit_Click(object sender, System.EventArgs e)
{
}
// 菜单的Popu 事件
private void menuItemEdit_Popup(object sender, System.EventArgs e)
{
if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
this.menuItem10.Enabled=true;
}
else
{
this.menuItem10.Enabled=false;
}
if(this.richTextBox1.SelectedText.Length>0)
{
this.menuItem11.Enabled=true;
this.menuItem8.Enabled=true;
this.menuItem9.Enabled=true;
}
else
{
this.menuItem11.Enabled=false;
this.menuItem8.Enabled=false;
this.menuItem9.Enabled=false;
}
// 根据文本框是否可执行撤销操作,决定撤销是否可用
if(this.richTextBox1.CanUndo==true)
{
this.Un.Enabled=true;
}
else
{
this.Un.Enabled=false;
}
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
}
private void richTextBox1_MouseUp(object sender, MouseEventArgs e)
{
//只处理右键
Point p=new Point(e.X,e.Y);
switch (e.Button)
{
case MouseButtons.Right:
contextMenu1.Show(this, p);
break;
default:
break;
}
}
private void MDIChild_FormClosing(object sender, FormClosingEventArgs e)
{
this.menuItem2_Click(null, null);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -