这是一个Rich Text Editor模式的编辑控件。正如我们所知,尽管微软已经在.NET Framework中提供了RichTextBox控件用于显示富文本,但在某些实际编辑工作中还是不尽人意。这个控件可以用于真正的编辑工作。界面如下:
HTMLTextBox控件内部构造
HTMLTextBox 是 UserControl 的子类,主要由两部分组成:一个ToolBar和一个WebBrowser 控件。
怎样使WebBrowser可编辑?
执行下面的代码后,WebBrowser将进入"Edit Mode"(编辑模式)。
webBrowserBody.Document.ExecCommand("EditMode", false, null);
怎样加粗字体?
执行下面的代码可使选定字符字体加粗。
webBrowserBody.Document.ExecCommand("Bold", false, null);
此控件中的所有操作都是调用WebBrowser.Document的ExecCommand函数。有关ExecCommand
的详细资料,参见Microsoft MSDN。
附注:Microsoft在Command Identifiers中提醒有些命令将不再得到支持,但实际上它们工作得很好,不要100%相信微软。
Command Identifiers
2D-PositionAllows absolutely positioned elements to be moved by dragging.
AbsolutePositionSets an element's position property to "absolute."
BackColorSets or retrieves the background color of the current selection.
BlockDirLTRNot currently supported.
BlockDirRTLNot currently supported.
BoldToggles the current selection between bold and nonbold.
BrowseModeNot currently supported.
ClearAuthenticationCacheClears all authentication credentials from the cache. Applies only to execCommand.
CopyCopies the current selection to the clipboard.
CreateBookmarkCreates a bookmark anchor or retrieves the name of a bookmark anchor for the current selection or insertion point.
CreateLinkInserts a hyperlink on the current selection, or displays a dialog box enabling the user to specify a URL to insert as a hyperlink on the current selection.
CutCopies the current selection to the clipboard and then deletes it.
DeleteDeletes the current selection.
DirLTRNot currently supported.
DirRTLNot currently supported.
EditModeNot currently supported.
FontNameSets or retrieves the font for the current selection.
FontSizeSets or retrieves the font size for the current selection.
ForeColorSets or retrieves the foreground (text) color of the current selection.
FormatBlockSets the current block format tag.
IndentIncreases the indent of the selected text by one indentation increment.
InlineDirLTRNot currently supported.
InlineDirRTLNot currently supported.
InsertButtonOverwrites a button control on the text selection.
InsertFieldsetOverwrites a box on the text selection.
InsertHorizontalRuleOverwrites a horizontal line on the text selection.
InsertIFrameOverwrites an inline frame on the text selection.
InsertImageOverwrites an image on the text selection.
InsertInputButtonOverwrites a button control on the text selection.
InsertInputCheckboxOverwrites a check box control on the text selection.
InsertInputFileUploadOverwrites a file upload control on the text selection.
InsertInputHiddenInserts a hidden control on the text selection.
InsertInputImageOverwrites an image control on the text selection.
InsertInputPasswordOverwrites a password control on the text selection.
InsertInputRadioOverwrites a radio control on the text selection.
InsertInputResetOverwrites a reset control on the text selection.
InsertInputSubmitOverwrites a submit control on the text selection.
InsertInputTextOverwrites a text control on the text selection.
InsertMarqueeOverwrites an empty marquee on the text selection.
InsertOrderedListToggles the text selection between an ordered list and a normal format block.
InsertParagraphOverwrites a line break on the text selection.
InsertSelectDropdownOverwrites a drop-down selection control on the text selection.
InsertSelectListboxOverwrites a list box selection control on the text selection.
InsertTextAreaOverwrites a multiline text input control on the text selection.
InsertUnorderedListConverts the text selection into an ordered list.
ItalicToggles the current selection between italic and nonitalic.
JustifyCenterCenters the format block in which the current selection is located.
JustifyFullNot currently supported.
JustifyLeftLeft-justifies the format block in which the current selection is located.
JustifyNoneNot currently supported.
JustifyRightRight-justifies the format block in which the current selection is located.
LiveResizeCauses the MSHTML Editor to update an element's appearance continuously during a resizing or moving operation, rather than updating only at the completion of the move or resize.
MultipleSelectionAllows for the selection of more than one site selectable element at a time when the user holds down the SHIFT or CTRL keys.
OpenNot currently supported.
OutdentDecreases by one increment the indentation of the format block in which the current selection is located.
OverWriteToggles the text-entry mode between insert and overwrite.
PasteOverwrites the contents of the clipboard on the current selection.
PlayImageNot currently supported.
PrintOpens the print dialog box so the user can print the current page.
RedoNot currently supported.
RefreshRefreshes the current document.
RemoveFormatRemoves the formatting tags from the current selection.
RemoveParaFormatNot currently supported.
SaveAsSaves the current Web page to a file.
SelectAllSelects the entire document.
SizeToControlNot currently supported.
SizeToControlHeightNot currently supported.
SizeToControlWidthNot currently supported.
StopNot currently supported.
StopImageNot currently supported.
StrikeThroughNot currently supported.
SubscriptNot currently supported.
SuperscriptNot currently supported.
UnBookmarkRemoves any bookmark from the current selection.
UnderlineToggles the current selection between underlined and not underlined.
UndoUndo the previous command.
UnlinkRemoves any hyperlink from the current selection.
UnselectClears the current selection.简介
关于可选字体
此控件中的所有可选字体将利用下面的代码获得。
foreach (FontFamily family in FontFamily.Families)
{
toolStripComboBoxName.Items.Add(family.Name);
}
关于字体大小
在HTML页面中共有7种字体尺寸。下面的表格列出了HTML字体尺寸与常用字体尺寸的对应关系:
HTML 字体尺寸 |
常用字体尺寸 |
1 |
8 |
2 |
10 |
3 |
12 |
4 |
14 |
5 |
18 |
6 |
24 |
7 |
36 |
为防止混乱,控件使用常用字体尺寸代替HTML字体尺寸。
作为一个UserControl
,HTMLTextBox
使用起来非常方便。没有重写或公开太多的属性和方法,只提供了两个公用属性:Text
和 Images
。你可以根据需要自由地添加。
Text
: 用于获取或设置更有意义的文本,已重写。
·get: 返回整个HTML 内容,包括 和 等。
·set: 设置任何文本值并显示在控件中。同时,“/r/n” 将自动转换为“
”(为了使段落间隔看起来不致于过大—译者注)
Images
: 设置所有附加图像的绝对路径
注意: HTMLTextBox将引用COM组件"Microsoft HTML Object Library"。
运行前,MailSender.cs中的第23-26行代码需做适当修改。
string host="192.168.22.12";
int port=25;
string userid="jay.liu";
string password="1111";
插入编辑区的图像无法调整大小,而且也没有异常抛出。目前不知道原因,也还没有解决方案。