Implementing the WYSIWYG HTML editor with WPF

Source: Internet
Author: User
Tags wysiwyg html editor

Introduction

In this tip, you'll learn the use of the WPF control and the use of the webbrowser  Library with MHTML for editing. This simple example would also help you understand how the toolbar works in WPF.

Background

In the development of aWPF application, I needed to edit HTML documents. After extensive the already existing solutions inWPF without finding my happiness, I finally decided to write my own HTML editor.
After analyzing Winform solutions, I found this Microsoft made significant changes in theWpfWebbrowser.
It's always possible to useWpfWindowsFormsHost, but you lose the benefits ofWPF.
in The wpf version of the  WebBrowser , there are no more   iswebbrowsercontextmenuenabled , activexinstance .
Ownership document has also been changed, the Winform version contains a document of type System.Windows.Forms.Ht mldocument  with lots of interesting methods such as  pointtoclient  and Getelementfrompoint .
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 this offers great possibilities for editing and a Nalyzing an HTML document. The
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 have the potential to create many advanced interfaces.
A Future article'll is devoted to the 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 is 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 ("

  

Appendchild webbrowser with the 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-commands in the webbrowser .

public static void Insertorderedlist (HTMLDocument doc) {    if (doc! = null)    {        Doc.execcommand (" Insertorderedlist ", false, NULL);}    }

  

Color DialogBox inWPF

The dialog box Winform is incompatible with WPF.
WPF uses while System.Windows.Media.Color and System.Windows.Media.Brush Winform uses System.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 could webbrowser  generate errors, in the Winform version webbrowser of the, there are a ScriptErrorsSuppressed  property that have 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});

  

Implementing the WYSIWYG HTML editor with WPF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.