📄 richtextboxextended.cs
字号:
//
this.mi24.Index = 10;
this.mi24.Text = "24";
this.mi24.Click += new System.EventHandler(this.FontSize_Click);
//
// mi26
//
this.mi26.Index = 11;
this.mi26.Text = "26";
this.mi26.Click += new System.EventHandler(this.FontSize_Click);
//
// mi28
//
this.mi28.Index = 12;
this.mi28.Text = "28";
this.mi28.Click += new System.EventHandler(this.FontSize_Click);
//
// mi36
//
this.mi36.Index = 13;
this.mi36.Text = "36";
this.mi36.Click += new System.EventHandler(this.FontSize_Click);
//
// mi48
//
this.mi48.Index = 14;
this.mi48.Text = "48";
this.mi48.Click += new System.EventHandler(this.FontSize_Click);
//
// mi72
//
this.mi72.Index = 15;
this.mi72.Text = "72";
this.mi72.Click += new System.EventHandler(this.FontSize_Click);
//
// tbbColor
//
this.tbbColor.DropDownMenu = this.cmColors;
this.tbbColor.ImageIndex = 7;
this.tbbColor.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
this.tbbColor.Tag = "color";
//
// cmColors
//
this.cmColors.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.miBlack,
this.miBlue,
this.miRed,
this.miGreen});
//
// miBlack
//
this.miBlack.Index = 0;
this.miBlack.Text = "Black";
this.miBlack.Click += new System.EventHandler(this.Color_Click);
//
// miBlue
//
this.miBlue.Index = 1;
this.miBlue.Text = "Blue";
this.miBlue.Click += new System.EventHandler(this.Color_Click);
//
// miRed
//
this.miRed.Index = 2;
this.miRed.Text = "Red";
this.miRed.Click += new System.EventHandler(this.Color_Click);
//
// miGreen
//
this.miGreen.Index = 3;
this.miGreen.Text = "Green";
this.miGreen.Click += new System.EventHandler(this.Color_Click);
//
// tbbSeparator5
//
this.tbbSeparator5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// tbbBold
//
this.tbbBold.ImageIndex = 0;
this.tbbBold.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbbBold.Tag = "bold";
//
// tbbItalic
//
this.tbbItalic.ImageIndex = 1;
this.tbbItalic.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbbItalic.Tag = "italic";
//
// tbbUnderline
//
this.tbbUnderline.ImageIndex = 2;
this.tbbUnderline.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbbUnderline.Tag = "underline";
//
// tbbStrikeout
//
this.tbbStrikeout.ImageIndex = 3;
this.tbbStrikeout.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbbStrikeout.Tag = "strikeout";
//
// tbbSeparator1
//
this.tbbSeparator1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// tbbLeft
//
this.tbbLeft.ImageIndex = 4;
this.tbbLeft.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbbLeft.Tag = "left";
//
// tbbCenter
//
this.tbbCenter.ImageIndex = 5;
this.tbbCenter.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbbCenter.Tag = "center";
//
// tbbRight
//
this.tbbRight.ImageIndex = 6;
this.tbbRight.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
this.tbbRight.Tag = "right";
//
// tbbSeparator2
//
this.tbbSeparator2.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// tbbUndo
//
this.tbbUndo.ImageIndex = 12;
this.tbbUndo.Tag = "undo";
//
// tbbRedo
//
this.tbbRedo.ImageIndex = 13;
this.tbbRedo.Tag = "redo";
//
// tbbSeparator4
//
this.tbbSeparator4.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
//
// tbbCut
//
this.tbbCut.ImageIndex = 17;
this.tbbCut.Tag = "cut";
//
// tbbCopy
//
this.tbbCopy.ImageIndex = 18;
this.tbbCopy.Tag = "copy";
//
// tbbPaste
//
this.tbbPaste.ImageIndex = 19;
this.tbbPaste.Tag = "paste";
//
// tbbStamp
//
this.tbbStamp.ImageIndex = 8;
this.tbbStamp.Tag = "edit stamp";
//
// imgList1
//
this.imgList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
this.imgList1.ImageSize = new System.Drawing.Size(16, 16);
this.imgList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgList1.ImageStream")));
this.imgList1.TransparentColor = System.Drawing.Color.Transparent;
//
// rtb1
//
this.rtb1.AutoWordSelection = true;
this.rtb1.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtb1.Location = new System.Drawing.Point(0, 26);
this.rtb1.Name = "rtb1";
this.rtb1.Size = new System.Drawing.Size(504, 198);
this.rtb1.TabIndex = 1;
this.rtb1.Text = "";
this.rtb1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.rtb1_KeyDown);
this.rtb1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.rtb1_KeyPress);
this.rtb1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.rtb1_LinkClicked);
this.rtb1.SelectionChanged += new System.EventHandler(this.rtb1_SelectionChanged);
//
// ofd1
//
this.ofd1.DefaultExt = "rtf";
this.ofd1.Filter = "Rich Text Files|*.rtf|Plain Text File|*.txt";
this.ofd1.Title = "Open File";
//
// sfd1
//
this.sfd1.DefaultExt = "rtf";
this.sfd1.Filter = "Rich Text File|*.rtf|Plain Text File|*.txt";
this.sfd1.Title = "Save As";
//
// RichTextBoxExtended
//
this.Controls.Add(this.rtb1);
this.Controls.Add(this.tb1);
this.Name = "RichTextBoxExtended";
this.Size = new System.Drawing.Size(504, 224);
this.ResumeLayout(false);
}
#endregion
#region Selection Change event
[Description("Occurs when the selection is changed"),
Category("Behavior")]
// Raised in tb1 SelectionChanged event so that user can do useful things
public event System.EventHandler SelChanged;
#endregion
#region Stamp Event Stuff
[Description("Occurs when the stamp button is clicked"),
Category("Behavior")]
public event System.EventHandler Stamp;
/// <summary>
/// OnStamp event
/// </summary>
protected virtual void OnStamp(EventArgs e)
{
if(Stamp != null)
Stamp(this, e);
switch(StampAction)
{
case StampActions.EditedBy:
{
StringBuilder stamp = new StringBuilder(""); //holds our stamp text
if(rtb1.Text.Length > 0) stamp.Append("\r\n\r\n"); //add two lines for space
stamp.Append("Edited by ");
//use the CurrentPrincipal name if one exsist else use windows logon username
if(Thread.CurrentPrincipal == null || Thread.CurrentPrincipal.Identity == null || Thread.CurrentPrincipal.Identity.Name.Length <= 0)
stamp.Append(Environment.UserName);
else
stamp.Append(Thread.CurrentPrincipal.Identity.Name);
stamp.Append(" on " + DateTime.Now.ToLongDateString() + "\r\n");
rtb1.SelectionLength = 0; //unselect everything basicly
rtb1.SelectionStart = rtb1.Text.Length; //start new selection at the end of the text
rtb1.SelectionColor = this.StampColor; //make the selection blue
rtb1.SelectionFont = new Font(rtb1.SelectionFont, FontStyle.Bold); //set the selection font and style
rtb1.AppendText(stamp.ToString()); //add the stamp to the richtextbox
rtb1.Focus(); //set focus back on the richtextbox
} break; //end edited by stamp
case StampActions.DateTime:
{
StringBuilder stamp = new StringBuilder(""); //holds our stamp text
if(rtb1.Text.Length > 0) stamp.Append("\r\n\r\n"); //add two lines for space
stamp.Append(DateTime.Now.ToLongDateString() + "\r\n");
rtb1.SelectionLength = 0; //unselect everything basicly
rtb1.SelectionStart = rtb1.Text.Length; //start new selection at the end of the text
rtb1.SelectionColor = this.StampColor; //make the selection blue
rtb1.SelectionFont = new Font(rtb1.SelectionFont, FontStyle.Bold); //set the selection font and style
rtb1.AppendText(stamp.ToString()); //add the stamp to the richtextbox
rtb1.Focus(); //set focus back on the richtextbox
} break;
} //end select
}
#endregion
#region Toolbar button click
/// <summary>
/// Handler for the toolbar button click event
/// </summary>
private void tb1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
// true if style to be added
// false to remove style
bool add = e.Button.Pushed;
//Switch based on the tag of the button pressed
switch(e.Button.Tag.ToString().ToLower())
{
case "bold":
ChangeFontStyle(FontStyle.Bold,add);
break;
case "italic":
ChangeFontStyle(FontStyle.Italic,add);
break;
case "underline":
ChangeFontStyle(FontStyle.Underline,add);
break;
case "strikeout":
ChangeFontStyle(FontStyle.Strikeout,add);
break;
case "left":
//change horizontal alignment to left
rtb1.SelectionAlignment = HorizontalAlignment.Left;
tbbCenter.Pushed = false;
tbbRight.Pushed = false;
break;
case "center":
//change horizontal alignment to center
tbbLeft.Pushed = false;
rtb1.SelectionAlignment = HorizontalAlignment.Center;
tbbRight.Pushed = false;
break;
case "right":
//change horizontal alignment to right
tbbLeft.Pushed = false;
tbbCenter.Pushed = false;
rtb1.SelectionAlignment = HorizontalAlignment.Right;
break;
case "edit stamp":
OnStamp(new EventArgs()); //send stamp event
break;
case "color":
rtb1.SelectionColor = Color.Black;
break;
case "undo":
rtb1.Undo();
break;
case "redo":
rtb1.Redo();
break;
case "open":
try
{
if (ofd1.ShowDialog() == DialogResult.OK && ofd1.FileName.Length > 0)
if(System.IO.Path.GetExtension(ofd1.FileName).ToLower().Equals(".rtf"))
rtb1.LoadFile(ofd1.FileName, RichTextBoxStreamType.RichText);
else
rtb1.LoadFile(ofd1.FileName, RichTextBoxStreamType.PlainText);
}
catch (ArgumentException ae)
{
if(ae.Message == "Invalid file format.")
MessageBox.Show("There was an error loading the file: " + ofd1.FileName);
}
break;
case "save":
if(sfd1.ShowDialog() == DialogResult.OK && sfd1.FileName.Length > 0)
if(System.IO.Path.GetExtension(sfd1.FileName).ToLower().Equals(".rtf"))
rtb1.SaveFile(sfd1.FileName);
else
rtb1.SaveFile(sfd1.FileName, RichTextBoxStreamType.PlainText);
break;
case "cut":
{
if(rtb1.SelectedText.Length <= 0) break;
rtb1.Cut();
break;
}
case "copy":
{
if(rtb1.SelectedText.Length <= 0) break;
rtb1.Copy();
break;
}
case "paste":
{
try
{
rtb1.Paste();
}
catch
{
MessageBox.Show("Paste Failed");
}
break;
}
} //end select
}
#endregion
#region Update Toolbar
/// <summary>
/// Update the toolbar button statuses
/// </summary>
public void UpdateToolbar()
{
// Get the font, fontsize and style to apply to the toolbar buttons
Font fnt = GetFontDetails();
// Set font style buttons to the styles applying to the entire selection
FontStyle style = fnt.Style;
//Set all the style buttons using the gathered style
tbbBold.Pushed = fnt.Bold; //bold button
tbbItalic.Pushed = fnt.Italic; //italic button
tbbUnderline.Pushed = fnt.Underline; //underline button
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -