可以在WinForms应用程序中使用CKEditor进行(X)HTML编辑吗?

时间:2011-05-13 05:03:12

标签: c# html winforms ckeditor webbrowser-control

我已将winforms html editor的代码改编为C#(见下文)。是否可以使用CKEditor?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WebEditorTest
{
/// <summary>
/// https://stackoverflow.com/questions/214124/winforms-html-editor
/// </summary>
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate("about:blank");
        Application.DoEvents();
        webBrowser1.Document.OpenNew(false).Write("<html><body><div id=\"editable\">Edit this text</div></body></html>");
        foreach (HtmlElement el in webBrowser1.Document.All)
        {
            el.SetAttribute("unselectable", "on");
            el.SetAttribute("contenteditable", "false");
        }
        foreach (HtmlElement el in webBrowser1.Document.All.GetElementsByName("editable"))
        {
            el.SetAttribute("width", webBrowser1.Width + "px");
            el.SetAttribute("height", "100%");
            el.SetAttribute("contenteditable", "true");
        }
        webBrowser1.Document.DomDocument.GetType().GetProperty("designMode").SetValue(webBrowser1.Document.DomDocument, "on", null);
        webBrowser1.IsWebBrowserContextMenuEnabled = false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        textBoxMarkup.Text = webBrowser1.DocumentText;
    }
}
}

2 个答案:

答案 0 :(得分:3)

是。由于您已经在使用WebBrowser控件,因此没有什么能阻止您加载包含CKEditor的HTML页面并通过DOM和InvokeScript与它进行交互。

更新 - 这是一个互动的工作示例:

form.cs

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate("C:\\blank.htm");
        Application.DoEvents();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Document.InvokeScript("InitEditor");
    }
}

blank.htm

<html>
    <head>
        <script src='http://ckeditor.com/apps/ckeditor/4.2/ckeditor.js?mriyyd'></script>
        <script type='text/javascript'>
            function InitEditor() { CKEDITOR.replace('editor1'); }
        </script>
    </head>
    <body>
        <textarea cols='80' id='editor1' name='editor1' rows='10'>
            <span>Lorem Ipsum</span>
        </textarea>
    </body>
</html>

答案 1 :(得分:1)

根据经验,您肯定可以在WinForms应用程序中使用CKEditor。我详细介绍了我在http://www.sheldmandu.com/programming/wysiwyg-html-editor-in-dotnet-wpf-windows-forms-vb6-in-web-browser中所做的事情。我实际上现在正处于为其编写控件的地步,因为在每个项目中手动执行操作或复制代码都有点烦人。