使用 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.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 });
}

  

时间: 2024-08-09 06:21:37

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

Web中所见即所得文字编辑器

1. CKEditor FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器.它志于轻量化,不需要太复杂的安装步骤即可使用.它可和PHP.JavaScript.ASP.ASP.NET.ColdFusion.Java.以及ABAP等不同的编程语言相结合.“FCKeditor”名称中的“FCK” 是这个编辑器的作者的名字Frederico Caldeira Knabben的缩写. FCKeditor 相容于绝大部分的网页浏览器,像是 : Internet Explorer 5

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

原文:WPF中嵌入Office编辑器(支持Word.Excel.PPT.Visio等) 现在有一个项目,需要使用wpf做一个简单的客户端,用来生成word.excel.ppt.visio等文档,这就需要能够在wpf中嵌入office的编辑器,并对office文档进行编辑. 在网上搜索了一下,发现了一个很好的示例:通过在wpf中嵌入DSOFramer控件来完成对office的编辑功能.效果图如下: 闲话不多说,直接上传源代码(别人的源码,我在百度网盘里面共享了),下载地址:http://pan.b

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

Content Tools是一个用于构建所见即所得编辑器(WYSIWYG)的 JavaScript 库.ContentTools 所见即所得的编辑器只需要几个简单的步骤就可以加入到任何的 HTML 页面.有一步一步的指南,常见的使用场景以及更高级的主题.ContentTools 是免费开源的,在 GitHub 上托管,开发和维护. 在线演示      源码下载 您可能感兴趣的相关文章 网站开发中很有用的 jQuery 效果[附源码] 分享35个让人惊讶的 CSS3 动画效果演示 十分惊艳的8个

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

经过了这一个多星期的调整与修复,NanUI for .NET Winform的稳定版已经发布.应广大群友的要求,现已将NanUI的全部代码开源. GitHub: https://github.com/NetDimension/NanUI Release: https://github.com/NetDimension/NanUI/releases 这次发布的是一个相对稳定的版本,解决和改善了如下问题: 页面随机白屏问题(主要原因是GC自动回收后,造成内存地址不可读) NanUI编译版本改为.NE

可视化HTML编辑器

[荐] 可视化HTML编辑器 CKEditor CKEditor是新一代的FCKeditor,是一个重新开发的版本.CKEditor是全球最优秀的网页在线文字编辑器之一,因其惊人的性能与可扩展性而广泛的被运用于各大网站. 可配合使用的扩展有 文件管理器KCFinder 在线演示:h...更多CKEditor信息 最新新闻: CKEditor 4.5.6 发布,可视化 HTML 编辑器 2015年12月10日 [荐] 可视化HTML编辑器 KindEditor KindEditor 是一套开源的在

百度编辑器UEditor PHP版下载

Ueditor是由百度web前端研发部开发所见即所得的编辑器,具有轻量,可定制,注重用户体验等特点.Ueditor基于BSD开源协议,除了具有代码精简.加载迅速的轻量级特质外,还采用了分层理念,使开发者可以根据实际应用和需求自由定制. Ueditor编辑器划分为了三层架构.其中,核心层为开发者提供了诸如range.selection.domUtils类的底层API接口,中间的命令插件层不仅提供了大量的基础command,还允许开发者基于核心层进行command命令的开发,而面向用户端的界面层则可

在线文本编辑器cheditor应用实例

CKEditor 即 FCKEDITOR . FCKeditor是目前最优秀的可见即可得网页编辑器之一,它采用JavaScript编写.具备功能强大.配置容易.跨浏览器.支持多种编程语言.开源等特点.它非常流行,互联网上很容易找到相关技术文档,国内许多WEB项目和大型网站均采用了FCKeditor. FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器.它志于轻量化,不需要太复杂的安装步骤即可使用.它可和PHP.JavaScript.ASP.ASP.NET.ColdFus

UEditor文本编辑器

Ueditor是由百度web前端研发部开发所见即所得的编辑器,具有轻量,可定制,注重用户体验等特点.Ueditor基于BSD开源协议,除了具有代码精简.加载迅速的轻量级特质 外,还采用了分层理念,使开发者可以根据实际应用和需求自由定制. Ueditor编辑器划分为了三层架构.其中,核心层为开发者提供了诸如range.selection.domUtils类的底层API接口,中间的命令插件层不仅提供了大量的基础command,还允许开发者基于核心层进行command命令的开发,而面向用户端的界面层则

很好用的在线markdown编辑器

# 欢迎使用 Cmd Markdown 编辑阅读器 基本符号 *,-,+ 3个符号效果都一样,这3个符号被称为 Markdown符号 空白行表示另起一个段落 `是表示inline代码,tab是用来标记 代码段,分别对应html的code,pre标签 换行 单一段落( <p>) 用一个空白行 连续两个空格 会变成一个 <br> 连续3个符号,然后是空行,表示 hr横线 标题 生成h1--h6,在文字前面加上 1--6个# 来实现 文字加粗是通过 文字左右各两个符号 引用 在第一行加上