使用 WPF 实现所见即所得HTML编辑器
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.HtmlDocument
type.
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编辑器的更多相关文章
- 使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析
使用所见即所得文本编辑器编辑文本存入数据库后通过ajax获取服务器json_encode的数据到前台,文本内容上边的html标签不解析 因为我在前台使用了jquery的text()方法,而不是html ...
- WPF中嵌入Office编辑器(支持Word、Excel、PPT、Visio等)
现在有一个项目,需要使用wpf做一个简单的客户端,用来生成word.excel.ppt.visio等文档,这就需要能够在wpf中嵌入office的编辑器,并对office文档进行编辑. 在网上搜索了一 ...
- [译] 通过 contentEditable 属性创建一个所见即所得的编辑器(富文本编辑器)
译者注 这只是一篇入门教程,介绍了一些基础知识,仅供参考,切不可因此觉得富文本编辑器很简单. 创建富文本编辑器是一个非常复杂的工程,需要考虑到方方面面,也有很多坑(请参考原文第一条评论). 为免误导大 ...
- GNU TeXmacs 1.99.8 发布,所见即所得科学编辑器(看看老实的GUI)
GNU TeXmacs 1.99.8 已发布,这是一个支持各种数学公式的所见即所得编辑器,可以用来编辑文本.图形.数学.交互内容,它的界面非常友好,并且内置高质量的排版引擎. 更新内容: bug 修复 ...
- 学习使用Wpf开源的文本编辑器—smithhtmleditor
前言 本文主要介绍使用Wpf文本编辑器--smithhtmleditor. 编辑器使用 首先新建一个项目WpfEditor. 然后到Codeplex下载smithhtmleditor. 下载地址:ht ...
- NanUI for Winform 使用示例【第二集】——做一个所见即所得的Markdown编辑器
经过了这一个多星期的调整与修复,NanUI for .NET Winform的稳定版已经发布.应广大群友的要求,现已将NanUI的全部代码开源. GitHub: https://github.com/ ...
- ContentTools – 所见即所得(WYSIWYG)编辑器
Content Tools是一个用于构建所见即所得编辑器(WYSIWYG)的 JavaScript 库.ContentTools 所见即所得的编辑器只需要几个简单的步骤就可以加入到任何的 HTML 页 ...
- 22个所见即所得在线 Web 编辑器
前言: 关于编辑器,适合的才是最好的,接下来,我会写一些关于日志编辑器的文章,今天就写写,可能内容会比较多. --------------------------------------------- ...
- 可视化HTML编辑器
[荐] 可视化HTML编辑器 CKEditor CKEditor是新一代的FCKeditor,是一个重新开发的版本.CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛 ...
随机推荐
- java Process在windows的使用汇总(转)
最常用的是ant(java工程中流行),maven,及通用的exec(只要有shell脚本如.sh,.bat,.exe,.cmd等).而其实前两者不容易出错,后者却遇到了以下问题:Caused by: ...
- ABP中动态WebAPI原理解析
ABP中动态WebAPI原理解析 动态WebAPI应该算是ABP中最Magic的功能之一了吧.开发人员无须定义继承自ApiController的类,只须重用Application Service中的类 ...
- 关于matlab矩阵卷积conv2和傅里叶变换求卷积ifft2的关系
先定义两个矩阵 a = [1 2 3 5 ; 4 7 9 5;1 4 6 7;5 4 3 7;8 7 5 1] %a矩阵取5*4 b = [1 5 4; 3 6 8; 1 5 7] %b矩阵如多数 ...
- android 项目中使用对话框统一封装
近期在做拼车项目中使用到了一些对话框,而且在非常多地方都使用到了,既然非常多地方使用到,那么肯定要封装一下,
- extern int *a与extern int a[]
extern int *a与int a[] Table of Contents 1. 问题: 2. 解答: 1 问题: 以下的声明取自某个源文件: int a[10]; int *b=a; 但在还有一 ...
- 网格最短路径算法(Dijkstra & Fast Marching)(转)
Dijkstra算法是计算图中节点之间最短路径的经典算法,网上关于Dijkstra算法原理介绍比较多,这里不再多讲.值得一提的是,当图中节点之间的权重都为1时,Dijkstra算法就变化为一般意义上的 ...
- SVN的svnlook命令
svnlook命令集(zhuanzai) 2011-12-08 17:00:30| 分类: System and CVS|字号 订阅 svnlook 名称 svnlook author — ...
- unity中的MonoBehaviour.OnMouseDown()
在官网的api文档中仅说明了 Description OnMouseDown is called when the user has pressed the mouse button while ov ...
- 使用NFS安装oracle软件
昨天.使用openfiler创建nas存储系统,安装oracle软件,在所有正面最好,要创建一个数据库时报ora错,原来使用nfs安装oracle数据库,mount选项有特殊要求,如以下.以备查: R ...
- embedded dylibs/frameworks are only supported on iOS 8.0 and later 错误解决
ld: warning: embedded dylibs/frameworks only run on iOS 8 or later ld: embedded dylibs/frameworks ar ...