C# WinForm WebBrowser (三) 编辑模式

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

        /// <summary>

        /// 编辑模式

        /// </summary>

        public void EditMode()

        {

            if (this.webBrowser.Document != null)

            {

                mshtml.IHTMLDocument2 doc = this.webBrowser.Document.DomDocument as mshtml.IHTMLDocument2;

                if (doc != null)

                {

                    doc.designMode = "on";

                }

            }

        }



        /// <summary>

        /// 启用浏览模式

        /// </summary>

        public void 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>

        public void 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类型的参数为:要使用该命令分配的值。并非适用于所有命令。

常见的命令有:

        private const string HTML_COMMAND_BOLD = "Bold";                       //加粗

        private const string HTML_COMMAND_UNDERLINE = "Underline";             //下划线

        private const string HTML_COMMAND_ITALIC = "Italic";                   //斜体

        private const string HTML_COMMAND_SUBSCRIPT = "Subscript";             //下标

        private const string HTML_COMMAND_SUPERSCRIPT = "Superscript";         //上标

        private const string HTML_COMMAND_STRIKE_THROUGH = "StrikeThrough";    //删除线

        private const string HTML_COMMAND_FONT_NAME = "FontName";              //字体

        private const string HTML_COMMAND_FONT_SIZE = "FontSize";              //字号

        private const string HTML_COMMAND_FORE_COLOR = "ForeColor";            //字体前景色

        private const string HTML_COMMAND_BACK_COLOR = "BackColor";            //字体背景色

        private const string HTML_COMMAND_INSERT_FORMAT_BLOCK = "FormatBlock"; //加粗

        private const string HTML_COMMAND_REMOVE_FORMAT = "RemoveFormat";      //清楚样式

        private const string HTML_COMMAND_JUSTIFY_LEFT = "JustifyLeft";        //文本左对齐

        private const string HTML_COMMAND_JUSTIFY_CENTER = "JustifyCenter";    //文本中间对齐

        private const string HTML_COMMAND_JUSTIFY_RIGHT = "JustifyRight";      //文本右对齐

        private const string HTML_COMMAND_JUSTIFY_FULL = "JustifyFull";        //文本两端对齐

        private const string HTML_COMMAND_INDENT = "Indent";                   //增大缩进量

        private const string HTML_COMMAND_OUTDENT = "Outdent";                 //减小缩进量

        private const string HTML_COMMAND_INSERT_LINE = "InsertHorizontalRule";//插入分割符

        private const string HTML_COMMAND_INSERT_LIST = "Insert{0}List"; // replace with (Un)Ordered 插入项目符号或项目编号

        private const string HTML_COMMAND_INSERT_IMAGE = "InsertImage";         //插入图像

        private const string HTML_COMMAND_INSERT_LINK = "CreateLink";           //插入链接

        private const string HTML_COMMAND_REMOVE_LINK = "Unlink";               //移除链接

        private const string HTML_COMMAND_TEXT_CUT = "Cut";                     //剪切

        private const string HTML_COMMAND_TEXT_COPY = "Copy";                   //复制

        private const string HTML_COMMAND_TEXT_PASTE = "Paste";                 //粘贴

        private const string HTML_COMMAND_TEXT_DELETE = "Delete";               //删除

        private const string HTML_COMMAND_TEXT_UNDO = "Undo";                   //撤销

        private const string HTML_COMMAND_TEXT_REDO = "Redo";                   //恢复

        private const string HTML_COMMAND_TEXT_SELECT_ALL = "SelectAll";        //全选

        private const string HTML_COMMAND_TEXT_UNSELECT = "Unselect";           //取消选择

        private const string HTML_COMMAND_TEXT_PRINT = "Print";                 // 打印

        private const string HTML_COMMAND_EDITMODE = "EditMode";                // 编辑模式

        private const string HTML_COMMAND_BROWSEMODE = "BrowseMode";            // 浏览模式

        private const string HTML_COMMAND_OVERWRITE = "OverWrite";             //转换插入、覆写模式

你可能感兴趣的:(WebBrowser)