C# WinForm WebBrowser (三) 编辑模式

 
一、启用编辑模式、 浏览模式 及 自动换行

///<summary>
/// 编辑模式
///</summary>
publicvoid EditMode()
{
if (this.webBrowser.Document !=null)
{
mshtml.IHTMLDocument2 doc
=this.webBrowser.Document.DomDocument as mshtml.IHTMLDocument2;
if (doc !=null)
{
doc.designMode
="on";
}
}
}

///<summary>
/// 启用浏览模式
///</summary>
publicvoid BrowseMode()
{
if (this.webBrowser.Document !=null)
{
mshtml.IHTMLDocument2 doc
=this.webBrowser.Document.DomDocument as mshtml.IHTMLDocument2;
if (doc !=null)
{
doc.designMode
="off";
}
}
}

///<summary>
/// 设置自动换行
///</summary>
///<param name="value"></param>
publicvoid SetAutoWrap(bool value)
{
mshtml.HTMLDocument doc
=this.webBrowser.Document.DomDocument as mshtml.HTMLDocument;
if (doc !=null)
{
mshtml.HTMLBody body
= doc.body as mshtml.HTMLBody;
if (body !=null)
{
body.noWrap
=!value;
}
}
}
 
在编辑模式下,可以使用:
this.webBrowser.Document.ExecCommand([string],[bool],[object]);
方法来操作WebBrowser中的HTML。
其中第一个字符串类型的参数为:要执行的命令的名称 
第二个布尔类型的参数为: 是否向用户显示命令特定的对话框或消息框
第三个Object类型的参数为:要使用该命令分配的值。并非适用于所有命令
常见的命令有:
privateconststring HTML_COMMAND_BOLD ="Bold"; //加粗
privateconststring HTML_COMMAND_UNDERLINE ="Underline"; //下划线
privateconststring HTML_COMMAND_ITALIC ="Italic"; //斜体
privateconststring HTML_COMMAND_SUBSCRIPT ="Subscript"; //下标
privateconststring HTML_COMMAND_SUPERSCRIPT ="Superscript"; //上标
privateconststring HTML_COMMAND_STRIKE_THROUGH ="StrikeThrough"; //删除线
privateconststring HTML_COMMAND_FONT_NAME ="FontName"; //字体
privateconststring HTML_COMMAND_FONT_SIZE ="FontSize"; //字号
privateconststring HTML_COMMAND_FORE_COLOR ="ForeColor"; //字体前景色
privateconststring HTML_COMMAND_BACK_COLOR ="BackColor"; //字体背景色
privateconststring HTML_COMMAND_INSERT_FORMAT_BLOCK ="FormatBlock"; //加粗
privateconststring HTML_COMMAND_REMOVE_FORMAT ="RemoveFormat"; //清楚样式
privateconststring HTML_COMMAND_JUSTIFY_LEFT ="JustifyLeft"; //文本左对齐
privateconststring HTML_COMMAND_JUSTIFY_CENTER ="JustifyCenter"; //文本中间对齐
privateconststring HTML_COMMAND_JUSTIFY_RIGHT ="JustifyRight"; //文本右对齐
privateconststring HTML_COMMAND_JUSTIFY_FULL ="JustifyFull"; //文本两端对齐
privateconststring HTML_COMMAND_INDENT ="Indent"; //增大缩进量
privateconststring HTML_COMMAND_OUTDENT ="Outdent"; //减小缩进量
privateconststring HTML_COMMAND_INSERT_LINE ="InsertHorizontalRule";//插入分割符
privateconststring HTML_COMMAND_INSERT_LIST ="Insert{0}List"; // replace with (Un)Ordered 插入项目符号或项目编号
privateconststring HTML_COMMAND_INSERT_IMAGE ="InsertImage"; //插入图像
privateconststring HTML_COMMAND_INSERT_LINK ="CreateLink"; //插入链接
privateconststring HTML_COMMAND_REMOVE_LINK ="Unlink"; //移除链接
privateconststring HTML_COMMAND_TEXT_CUT ="Cut"; //剪切
privateconststring HTML_COMMAND_TEXT_COPY ="Copy"; //复制
privateconststring HTML_COMMAND_TEXT_PASTE ="Paste"; //粘贴
privateconststring HTML_COMMAND_TEXT_DELETE ="Delete"; //删除
privateconststring HTML_COMMAND_TEXT_UNDO ="Undo"; //撤销
privateconststring HTML_COMMAND_TEXT_REDO ="Redo"; //恢复
privateconststring HTML_COMMAND_TEXT_SELECT_ALL ="SelectAll"; //全选
privateconststring HTML_COMMAND_TEXT_UNSELECT ="Unselect"; //取消选择
privateconststring HTML_COMMAND_TEXT_PRINT ="Print"; // 打印
privateconststring HTML_COMMAND_EDITMODE ="EditMode"; // 编辑模式
privateconststring HTML_COMMAND_BROWSEMODE ="BrowseMode"; // 浏览模式
privateconststring HTML_COMMAND_OVERWRITE ="OverWrite"; //转换插入、覆写模式
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2011-04-23 02:26  把爱延续  阅读(...)  评论(...编辑  收藏