Introduction

In this tip, you will learn the use of WPF webbrowser control and the use of the library mhtml for editing. This simple example will also help you understand how the toolbar works in WPF.

Background

In the development of a WPF application, I needed to edit HTML documents. After extensive research on already existing solutions in WPF without finding my happiness, I finally decided to write my own HTML editor.
After analyzing Winform solutions, I found that Microsoft made significant changes in the WPF Webbrowser.
It is always possible to use WPF WindowsFormsHost, but you lose the benefits of WPF.
In the WPF version of the webbrowser, there is no more IsWebBrowserContextMenuEnabled,ActiveXInstance.
Ownership document has also been changed, the Winform version contains a document of typeSystem.Windows.Forms.HtmlDocument with lots of interesting methods such as PointToClient andGetElementFromPoint.
In WPF webrowser, the document is a document object and you need to cast it to mshtml.HtmlDocumenttype.
The mshtml library is a very powerful library that offers great possibilities for editing and analyzing an HTML document.
The official documentation is very well stocked.
https://msdn.microsoft.com/en-us/library/aa753630%28v=vs.85%29.aspx

WPF Interface

The benefits of WPF technology are numerous and has the potential to create many advanced interfaces.
A future article will be devoted to Ribbon Toolbar in WPF.

Editing the HTML

To edit a document using Microsoft.mshtml library, it’s necessary to reference it in the project.

using mshtml;
public void newDocument()
{
webBrowser.NavigateToString(Properties.Resources.New);
doc = webBrowser.Document as HTMLDocument;
doc.designMode = "On";
}

  

Formatting the HTML

To change the heading of a selection, there are several ways to proceed.
This method uses FormatBlock webbrowser control.

public static void RibbonComboboxFormat(ComboBox RibbonComboboxFormat)
{
string ID = ((Items)(RibbonComboboxFormat.SelectedItem)).Value; webBrowser.doc = webBrowser.webBrowser.Document as HTMLDocument;
if (webBrowser.doc != null)
{
webBrowser.doc.execCommand("FormatBlock", false, ID);
}
}

  


Another solution is to use the library mshtml.

mshtml.IHTMLDocument2 doc;

doc = webBrowser1.Document.DomDocument as mshtml.IHTMLDocument2;

mshtml.IHTMLTxtRange r = doc.selection.createRange() as mshtml.IHTMLTxtRange;
mshtml.IHTMLElement parent = r.parentElement();
mshtml.IHTMLElement heading = doc.createElement("<h2>"); r.pasteHTML(heading.outerHTML);

  

With the Appendchild webbrowser method, you can add elements such as tables or any other HTML too.

HtmlElement tableElem = webBrowser1.Document.CreateElement("TABLE");
webBrowser1.Document.Body.AppendChild(tableElem);

  

To format the text, you can proceed in several different ways, either by using the webbrowser or the mshtml library.
In this example, I chose to use commands in the webbrowser.

public static void InsertOrderedList(HTMLDocument doc)
{
if (doc != null)
{
doc.execCommand("InsertOrderedList", false, null);
}
}

  

Color Dialogbox in WPF

The dialog box Winform is incompatible with WPF.
WPF uses System.Windows.Media.Color and System.Windows.Media.Brush while Winform usesSystem.Drawing.Color.
The workaround is to made color transfer from the four channels individually.

public static System.Windows.Media.Color Pick()
{
System.Windows.Media.Color col = new System.Windows.Media.Color(); using (System.Windows.Forms.ColorDialog colorDialog = new System.Windows.Forms.ColorDialog())
{
colorDialog.AllowFullOpen = true;
colorDialog.FullOpen = true;
System.Windows.Forms.DialogResult result = colorDialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK)
{
col.A = colorDialog.Color.A;
col.B = colorDialog.Color.B;
col.G = colorDialog.Color.G;
col.R = colorDialog.Color.R;
}
}
return col;
}

  

Supress the Script Error Message

For pages containing scripts, the webbrowser may generate errors, in the Winform version of the webbrowser, there is a ScriptErrorsSuppressed property that has unfortunately disappeared in the WPF webbrowser.

// Property in the Winform version.

webBrowser.ScriptErrorsSuppressed = true;

  

It is necessary to implement this functionality in WPF webbrowser.

using System.Reflection;

public static void HideScriptErrors(WebBrowser wb, bool Hide)
{
FieldInfo FieldInfoComWebBrowser = typeof(WebBrowser).GetField
("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic); if (FieldInfoComWebBrowser == null)
{
return;
} object ComWebBrowser = FieldInfoComWebBrowser.GetValue(wb); if (ComWebBrowser == null)
{
return;
} ComWebBrowser.GetType().InvokeMember("Silent",
BindingFlags.SetProperty, null, ComWebBrowser, new object[] { Hide });
}

  

使用 WPF 实现所见即所得HTML编辑器的更多相关文章

  1. 使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析

    使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析 因为我在前台使用了jquery的text()方法,而不是html ...

  2. WPF中嵌入Office编辑器(支持Word、Excel、PPT、Visio等)

    现在有一个项目,需要使用wpf做一个简单的客户端,用来生成word.excel.ppt.visio等文档,这就需要能够在wpf中嵌入office的编辑器,并对office文档进行编辑. 在网上搜索了一 ...

  3. [译] 通过 contentEditable 属性创建一个所见即所得的编辑器(富文本编辑器)

    译者注 这只是一篇入门教程,介绍了一些基础知识,仅供参考,切不可因此觉得富文本编辑器很简单. 创建富文本编辑器是一个非常复杂的工程,需要考虑到方方面面,也有很多坑(请参考原文第一条评论). 为免误导大 ...

  4. GNU TeXmacs 1.99.8 发布,所见即所得科学编辑器(看看老实的GUI)

    GNU TeXmacs 1.99.8 已发布,这是一个支持各种数学公式的所见即所得编辑器,可以用来编辑文本.图形.数学.交互内容,它的界面非常友好,并且内置高质量的排版引擎. 更新内容: bug 修复 ...

  5. 学习使用Wpf开源的文本编辑器—smithhtmleditor

    前言 本文主要介绍使用Wpf文本编辑器--smithhtmleditor. 编辑器使用 首先新建一个项目WpfEditor. 然后到Codeplex下载smithhtmleditor. 下载地址:ht ...

  6. NanUI for Winform 使用示例【第二集】——做一个所见即所得的Markdown编辑器

    经过了这一个多星期的调整与修复,NanUI for .NET Winform的稳定版已经发布.应广大群友的要求,现已将NanUI的全部代码开源. GitHub: https://github.com/ ...

  7. ContentTools – 所见即所得(WYSIWYG)编辑器

    Content Tools是一个用于构建所见即所得编辑器(WYSIWYG)的 JavaScript 库.ContentTools 所见即所得的编辑器只需要几个简单的步骤就可以加入到任何的 HTML 页 ...

  8. 22个所见即所得在线 Web 编辑器

    前言: 关于编辑器,适合的才是最好的,接下来,我会写一些关于日志编辑器的文章,今天就写写,可能内容会比较多. --------------------------------------------- ...

  9. 可视化HTML编辑器

    [荐] 可视化HTML编辑器 CKEditor CKEditor是新一代的FCKeditor,是一个重新开发的版本.CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛 ...

随机推荐

  1. OCP读书笔记(10) - 使用闪回技术I

    使用闪回技术查询数据 闪回查询:就是查询表在过去某个时间点的数据,所用到的技术就是undo数据 SQL> conn scott/tiger 创建测试表 SQL> create table ...

  2. 【大话QT之十七】Jenkins介绍及安装使用文档(与Git集成)

    文章文件夹结构例如以下: 1> Jenkins与Git相关介绍 2> Jenkins部署安装 3> Gitblit部署安装 4> Jenkins与Git集成使用 5> 项 ...

  3. cocos2d-x2.2.5 + cocos2d-x3.2鸟跳便宜源代码“开源”

    尊重开发人员的劳动成果,转载请注明From郝萌主 游戏简单介绍: 贱鸟跳跳,贱贱的小鸟这次遇上大问题了.被它整蛊过的同类都在找它的麻烦,如今我们赶紧到游戏中帮帮它吧!左右手互撸,合理操控.获得高分,打 ...

  4. HTML学习笔记——各种居中对齐

    0.前言     水平居中基本方法--指定块的宽度并设定块的左右外边距为auto,上下外边距可取0,那么该块能够在父元素中水平居中. 样式例如以下: 1:margin:0px auto 2:margi ...

  5. OpenCL 查看设备信息

    好久没搞OpenCL了.可是这是个好东西.不能不学,之前发了篇设置OpenCL的文章.看的人还真多,看来大家都知道这个好东西了,都想把OpenCL搞起.只是学习难度还是相当高的. 之前忙搞算法,所以非 ...

  6. Json,Gson,FastJson解析笔记

    Json,Gson,FastJson解析笔记 1.将JavaBean转换成Json对象: public static String CreatJsonFromObject(Object key,Obj ...

  7. HUNNU11354:Is the Name of This Problem

    http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11354&courseid=0 Problem des ...

  8. Akka边学边写(3)-- ByteString介绍

    Akka的IO层设计能够參考这篇文档,本文简介一下ByteString的设计. Immutable消息 Actor之间是通过消息沟通的.但为了避免同步问题,消息必须是Immutable. 因此.Akk ...

  9. hibernate之关于使用连接表实现多对一关联映射

    [Hibernate]之关于使用连接表实现多对一关联映射 在我们项目使用中採用中间表最多的一般就是多对一,或者是多对多,当然一对一使用中间表也是能够的,可是这样的几率通常少之又少!所以这里重点介绍多对 ...

  10. ACdream原创群赛(18)のAK's dream题解

    只做了4题水题ADGI A题需要注意的就是“[...]”的输出了,何时输出,何时不输出. #include <stdio.h> int main() { int n, cur, d; ; ...