📄 htmlrichtextbox.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Text.RegularExpressions;
using System.ComponentModel;
namespace GPCore.GibphoneControls
{
/// <summary>
/// A RichTextBox that supports Pictures and HTML
/// </summary>
public class HtmlRichTextBox: RichTextBox
{
/// <summary>
/// This converts between RTF to HTML and either returns it
/// or puts it in the textbox
/// </summary>
[System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Html
{
get
{
RtfText rt = new RtfText(this.Rtf);
return rt.Html;
}
set
{
HtmlText ht = new HtmlText(value);
this.Rtf = ht.Rtf;
}
}
/// <summary>
/// Gets or Sets the SelectedHtml
/// </summary>
[System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string SelectedHtml
{
get
{
RtfText rt = new RtfText(SelectedRtf);
return rt.Html;
}
set
{
HtmlText ht = new HtmlText(value);
this.SelectedRtf = ht.Rtf;
}
}
/// <summary>
/// This appends <paramref name="html"/>to the end of the
/// text box.
/// </summary>
/// <param name="html">The HTML to append</param>
/// <returns><paramref name="html"/> in Rtf</returns>
public string AppendHTML(string html)
{
HtmlText ht = new HtmlText(html);
this.Select(this.TextLength, 0);
return this.SelectedRtf = ht.Rtf;
//return this.Rtf = ht.Rtf;
}
/// <summary>
/// Adds an Image to the end of the textbox
/// </summary>
/// <param name="filename">The filename of the Image</param>
public void AddImage(string filename)
{
AddImage(Image.FromFile(filename));
}
/// <summary>
/// Adds an Image to the end of the textbox
/// </summary>
/// <param name="img">The Image to insert</param>
public void AddImage(Image img)
{
IDataObject obj = Clipboard.GetDataObject();
Clipboard.SetImage(img);
this.Select(this.TextLength, 0);
bool ro = ReadOnly;
ReadOnly = false;
this.Paste();
ReadOnly = ro;
Clipboard.SetDataObject(obj);
}
public void AddImageAtSelection(string filename)
{
AddImageAtSelection(Image.FromFile(filename));
}
public void AddImageAtSelection(Image img)
{
IDataObject obj = Clipboard.GetDataObject();
Clipboard.SetImage(img);
bool ro = this.ReadOnly;
ReadOnly = false;
this.Paste();
ReadOnly = ro;
//Clipboard.SetDataObject(obj);
}
/// <summary>
/// Applies <paramref name="fnt"/> to the selected text
/// </summary>
/// <param name="fnt"></param>
public void ApplyFont(Font fnt)
{
SelectionFont = fnt;
}
/// <summary>
/// Applys a color to the selected text
/// </summary>
/// <param name="col">The color to apply</param>
/// <param name="Foreground">Set to true if this color applies to the foreground and false if it applies to the background</param>
public void ApplyColor(Color col, bool Foreground)
{
if (Foreground)
{
SelectionColor = col;
} else
{
SelectionBackColor = col;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -