可以的CKEditor为(X)HTML编辑WinForms应用程序中使用? [英] Can the CKEditor be used in a WinForms application for (X)HTML editing?

查看:320
本文介绍了可以的CKEditor为(X)HTML编辑WinForms应用程序中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经适应从的WinForms HTML编辑器代码以C#(见下文)。 ?是否有可能使用的CKEditor而不是

 使用系统; 
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;

命名空间WebEditorTest
{
///<总结>
/// http://stackoverflow.com/questions/214124/winforms-html-editor
///< /总结>
公共部分Form1类:表格
{
公共Form1中()
{
的InitializeComponent();
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
WebBrowser1.Navigate时(关于:空白);
Application.DoEvents();
webBrowser1.Document.OpenNew(假).WRITE(< HTML><身体GT;< DIV ID = \editable\>编辑这个文字< / DIV>< / BODY> < / HTML>中);
的foreach(EL的HtmlElement在webBrowser1.Document.All)
{
el.SetAttribute(关于不可选);
el.SetAttribute(CONTENTEDITABLE,假);
}
的foreach(EL的HtmlElement在webBrowser1.Document.All.GetElementsByName(编辑))
{
el.SetAttribute(宽度,webBrowser1.Width +PX );
el.SetAttribute(高度,100%);
el.SetAttribute(CONTENTEDITABLE,真);
}
webBrowser1.Document.DomDocument.GetType()的getProperty(的designMode)的SetValue(webBrowser1.Document.DomDocument,上,NULL);
webBrowser1.IsWebBrowserContextMenuEnabled = FALSE;
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
textBoxMarkup.Text = webBrowser1.DocumentText;
}
}
}


解决方案

是的。既然你已经在使用WebBrowser控件,没有什么从装载包含CKEditor的,并通过DOM和InvokeScript与它交互的HTML页面阻止你。



更新 - 这里是一个交互工作的例子:



form.cs

 公共部分类Form1中:形态
{
公共Form1中()
{
的InitializeComponent();
}

私人无效Form1_Load的(对象发件人,EventArgs五)
{
WebBrowser1.Navigate时(C:\\blank.htm);
Application.DoEvents();
}

私人无效的button1_Click(对象发件人,EventArgs五)
{
webBrowser1.Document.InvokeScript(InitEditor);
}
}



blank.htm

 < HTML和GT; 
< HEAD>
<脚本的src =HTTP://ckeditor.com/apps/ckeditor/4.2/ckeditor.js mriyyd'>< / SCRIPT>
<脚本类型=文/ JavaScript的'>
功能InitEditor(){CKEDITOR.replace('editor1'); }
< / SCRIPT>
< /头>
<身体GT;
< textarea的COLS = '80'ID ='editor1'名称='editor1'行='10'>
<跨度> Lorem存有< / SPAN>
< / textarea的>
< /身体GT;
< / HTML>


I have adapted the code from winforms html editor to C# (see below). Is it possible to use the CKEditor instead?

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>
/// http://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;
    }
}
}

解决方案

Yes. Since you are already using the WebBrowser control, there is nothing stopping you from loading an HTML page containing CKEditor and interacting with it through the DOM and InvokeScript.

UPDATE - Here's a working example of interaction:

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>

这篇关于可以的CKEditor为(X)HTML编辑WinForms应用程序中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆