htmleditorcontrol.cs
来自「一个很好用的html编辑器。带源码」· CS 代码 · 共 1,178 行 · 第 1/5 页
CS
1,178 行
private Color _defaultBackColor;
private Color _defaultForeColor;
private HtmlFontProperty _defaultFont;
// internal property values
private bool _readOnly;
private bool _toolbarVisible;
private bool _enableVisualStyles;
private DockStyle _toolbarDock;
private string _bodyText;
private string _bodyHtml;
private string _bodyUrl;
// internal body property values
private Color _bodyBackColor;
private Color _bodyForeColor;
private HtmlFontProperty _bodyFont;
private int[] _customColors;
private string _imageDirectory;
private string _htmlDirectory;
private NavigateActionOption _navigateWindow;
private DisplayScrollBarOption _scrollBars;
private string _baseHref;
private bool _autoWordWrap;
// find and replace internal text range
private HtmlTextRange _findRange;
// winform generated code
private AxSHDocVw.AxWebBrowser editorWebBrowser;
private System.Windows.Forms.ToolBar editorToolbar;
private System.Windows.Forms.ImageList toolbarImageList;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ToolBarButton toolBarCopy;
private System.Windows.Forms.ToolBarButton toolBarPaste;
private System.Windows.Forms.ToolBarButton toolBarBold;
private System.Windows.Forms.ToolBarButton toolBarUnderline;
private System.Windows.Forms.ToolBarButton toolBarItalic;
private System.Windows.Forms.ToolBarButton toolBarUndo;
private System.Windows.Forms.ToolBarButton toolBarRedo;
private System.Windows.Forms.ToolBarButton toolBarFont;
private System.Windows.Forms.ToolBarButton toolBarCut;
private System.Windows.Forms.ToolBarButton toolBarNormal;
private System.Windows.Forms.ToolBarButton toolBarJustLeft;
private System.Windows.Forms.ToolBarButton toolBarJustCenter;
private System.Windows.Forms.ToolBarButton toolBarJustRight;
private System.Windows.Forms.ToolBarButton toolBarIndent;
private System.Windows.Forms.ToolBarButton toolBarOutdent;
private System.Windows.Forms.ToolBarButton toolBarListUnordered;
private System.Windows.Forms.ToolBarButton toolBarFontUp;
private System.Windows.Forms.ToolBarButton toolBarColor;
private System.Windows.Forms.ToolBarButton toolBarInsertImage;
private System.Windows.Forms.ToolBarButton toolBarInsertLink;
private System.Windows.Forms.ToolBarButton toolBarFindReplace;
private System.Windows.Forms.ToolBarButton toolBarPrint;
private System.Windows.Forms.ToolBarButton toolBarEditSep1;
private System.Windows.Forms.ToolBarButton toolBarEditSep2;
private System.Windows.Forms.ToolBarButton toolBarEditSep3;
private System.Windows.Forms.ToolBarButton toolBarEditSep4;
private System.Windows.Forms.ToolBarButton toolBarEditSep5;
private System.Windows.Forms.ToolBarButton toolBarEditSep6;
private System.Windows.Forms.ToolBarButton toolBarInsertLine;
private System.Windows.Forms.ToolBarButton toolBarListOrdered;
private System.Windows.Forms.MenuItem menuTextUndo;
private System.Windows.Forms.MenuItem menuTextRedo;
private System.Windows.Forms.MenuItem menuTextSep1;
private System.Windows.Forms.MenuItem menuTextCut;
private System.Windows.Forms.MenuItem menuTextCopy;
private System.Windows.Forms.MenuItem menuTextPaste;
private System.Windows.Forms.MenuItem menuTextDelete;
private System.Windows.Forms.MenuItem menuTextFont;
private System.Windows.Forms.MenuItem menuTextFontIncrease;
private System.Windows.Forms.MenuItem menuTextFontDecrease;
private System.Windows.Forms.MenuItem menuTextFontBold;
private System.Windows.Forms.MenuItem menuTextFontItalic;
private System.Windows.Forms.MenuItem menuTextFontUnderline;
private System.Windows.Forms.MenuItem menuTextFontIndent;
private System.Windows.Forms.MenuItem menuTextFontOutdent;
private System.Windows.Forms.MenuItem menuTextFontDialog;
private System.Windows.Forms.MenuItem menuTextFontColor;
private System.Windows.Forms.MenuItem menuTextSelectAll;
private System.Windows.Forms.MenuItem menuJustify;
private System.Windows.Forms.MenuItem menuJustifyLeft;
private System.Windows.Forms.MenuItem menuJustifyCenter;
private System.Windows.Forms.MenuItem menuJustifyRight;
private System.Windows.Forms.MenuItem menuTextSelectNone;
private System.Windows.Forms.MenuItem menuFormatting;
private System.Windows.Forms.MenuItem menuTextFontSep1;
private System.Windows.Forms.MenuItem menuTextFontSep2;
private System.Windows.Forms.MenuItem menuTextFontSep3;
private System.Windows.Forms.MenuItem menuTextFontSuperscript;
private System.Windows.Forms.MenuItem menuTextFontSubscript;
private System.Windows.Forms.MenuItem menuInsert;
private System.Windows.Forms.MenuItem menuInsertLine;
private System.Windows.Forms.MenuItem menuInsertLink;
private System.Windows.Forms.MenuItem menuInsertImage;
private System.Windows.Forms.MenuItem menuInsertText;
private System.Windows.Forms.MenuItem menuInsertHtml;
private System.Windows.Forms.MenuItem menuInsertTable;
private System.Windows.Forms.ToolBarButton toolBarInsertTable;
private System.Windows.Forms.MenuItem menuMainSep1;
private System.Windows.Forms.MenuItem menuMainSep2;
private System.Windows.Forms.MenuItem menuText;
private System.Windows.Forms.MenuItem menuTableModify;
private System.Windows.Forms.ContextMenu contextMenuMain;
private System.Windows.Forms.MenuItem menuTextSep3;
private System.Windows.Forms.MenuItem menuTextSep2;
private System.Windows.Forms.MenuItem menuTextFindReplace;
private System.Windows.Forms.MenuItem menuDocument;
private System.Windows.Forms.MenuItem menuDocumentOpen;
private System.Windows.Forms.MenuItem menuDocumentSave;
private System.Windows.Forms.MenuItem menuDocumentSep1;
private System.Windows.Forms.MenuItem menuDocumentPrint;
private System.Windows.Forms.MenuItem menuTextFontNormal;
private System.Windows.Forms.MenuItem menuTextFontStrikeout;
private System.Windows.Forms.MenuItem menuTextFontListOrdered;
private System.Windows.Forms.MenuItem menuTextFontListUnordered;
private System.Windows.Forms.MenuItem menuDocumentSep2;
private System.Windows.Forms.MenuItem menuDocumentToolbar;
private System.Windows.Forms.MenuItem menuDocumentScrollbar;
private System.Windows.Forms.MenuItem menuDocumentWordwrap;
private System.Windows.Forms.MenuItem menuDocumentOverwrite;
private System.Windows.Forms.MenuItem menuTableProperties;
private System.Windows.Forms.MenuItem menuTableInsertRow;
private System.Windows.Forms.MenuItem menuTableDeleteRow;
private System.Windows.Forms.ToolBarButton toolBarFontDown;
public HtmlEditorControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// define the context menu for format commands
DefineFormatBlockMenu();
// define the default values
// browser constants and commands
EMPTY_PARAMETER = System.Reflection.Missing.Value;
// default values used to reset values
_defaultBackColor = Color.White;
_defaultForeColor = Color.Black;
_defaultFont = new HtmlFontProperty(this.Font);
// set browser default values to hide IE items
this.editorWebBrowser.AddressBar = false;
this.editorWebBrowser.MenuBar = false;
this.editorWebBrowser.StatusBar = false;
// obtain the underlying web browser and set options
// SHDocVw.WebBrowser webBrowser = (SHDocVw.WebBrowser)this.editorWebBrowser.GetOcx();
// define the default values of the properties
_readOnly = false;
_toolbarVisible = false;
_enableVisualStyles = false;
_toolbarDock = DockStyle.Bottom;
_bodyText = DEFAULT_HTML_TEXT;
_bodyHtml = DEFAULT_HTML_TEXT;
_bodyBackColor = _defaultBackColor;
_bodyForeColor = _defaultForeColor;
_bodyFont = _defaultFont;
_scrollBars = DisplayScrollBarOption.Auto;
_imageDirectory = string.Empty;
_htmlDirectory = string.Empty;
_navigateWindow = NavigateActionOption.Default;
_baseHref = string.Empty;
_autoWordWrap = true;
stylesheet = null;
script = null;
// define context menu state
this.menuDocumentToolbar.Checked = true;
this.menuDocumentScrollbar.Checked = true;
this.menuDocumentWordwrap.Checked = true;
// load the blank Html page to load the MsHtml object model
BrowserCodeNavigate(BLANK_HTML_PAGE);
// after load ensure document marked as editable
this.ReadOnly = _readOnly;
} //HTMLEditorControl
// once an html docuemnt has been loaded define the internal values
private void DefineBodyAttributes()
{
// define the body colors based on the new body html
if (body.bgColor == null)
{
_bodyBackColor = _defaultBackColor;
}
else
{
_bodyBackColor = ColorTranslator.FromHtml((string)body.bgColor);
}
if (body.text == null)
{
_bodyForeColor = _defaultForeColor;
}
else
{
_bodyForeColor = ColorTranslator.FromHtml((string)body.text);
}
// define the font object based on current font of new document
// deafult used unless a style on the body modifies the value
HtmlStyle bodyStyle = body.style;
if (bodyStyle != null)
{
string fontName = _bodyFont.Name;
HtmlFontSize fontSize = _bodyFont.Size;
bool fontBold = _bodyFont.Bold;
bool fontItalic = _bodyFont.Italic;
bool fontUnderline = _bodyFont.Underline;
// define the font name if defined in the style
if (bodyStyle.fontFamily != null) fontName = bodyStyle.fontFamily;
if (bodyStyle.fontSize != null) fontSize = HtmlFontConversion.StyleSizeToHtml(bodyStyle.fontSize.ToString());
if (bodyStyle.fontWeight != null) fontBold = HtmlFontConversion.IsStyleBold(bodyStyle.fontWeight);
if (bodyStyle.fontStyle != null) fontItalic = HtmlFontConversion.IsStyleItalic(bodyStyle.fontStyle);
fontUnderline = bodyStyle.textDecorationUnderline;
// define the new font object and set the property
_bodyFont = new HtmlFontProperty(fontName, fontSize, fontBold, fontItalic, fontUnderline);
this.BodyFont = _bodyFont;
}
// define the content based on the current value
this.ReadOnly = _readOnly;
this.ScrollBars = _scrollBars;
this.AutoWordWrap = _autoWordWrap;
} //DefineBodyAttributes
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?