wpf 实现html编辑器,使用 WPF 实现所见即所得HTML编辑器

b110018050588b255eec6ed9787c1036.png

Introduction

In this tip, you will learn the use of WPF webbrowsercontrol 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.HtmlDocumentwith lots of interesting methods such as PointToClientandGetElementFromPoint.

In WPF webrowser, the document is a documentobject and you need to cast it to mshtml.HtmlDocumenttype.

The mshtmllibrary 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.mshtmllibrary, 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 webbrowsercontrol.

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("

");

r.pasteHTML(heading.outerHTML);

With the Appendchild webbrowsermethod, 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 webbrowseror 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.

80ae2fb435fd37c7bcd1020dd971543b.png

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 webbrowsermay generate errors, in the Winform version of the webbrowser, there is a ScriptErrorsSuppressedproperty 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 });

}

原文:http://www.cnblogs.com/mschen/p/4288142.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值