CKEditor 可以在 WinForms 应用程序中用于 (X)HTML 编辑吗?
Posted
技术标签:
【中文标题】CKEditor 可以在 WinForms 应用程序中用于 (X)HTML 编辑吗?【英文标题】:Can the CKEditor be used in a WinForms application for (X)HTML editing? 【发布时间】:2011-08-24 15:38:31 【问题描述】:我已将代码从 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://***.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;
【问题讨论】:
【参考方案1】:根据经验,您肯定可以在 WinForms 应用程序中使用 CKEditor。我详细说明了我在http://www.sheldmandu.com/programming/wysiwyg-html-editor-in-dotnet-wpf-windows-forms-vb6-in-web-browser 中所做的事情,实际上我现在正处于为它编写控件的地步,因为在每个项目中手动执行操作或复制代码有点烦人。
【讨论】:
【参考方案2】:是的。由于您已经在使用 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");
空白.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>
【讨论】:
实际上权限被拒绝,这可能会阻止您将脚本加载到您的网络浏览器控件中。 怎么样?这里没有跨域或同源问题,因为他正在动态创建页面。 SO上的几个例子:***.com/questions/153748/…. 所有这些示例都展示了如何将 js 代码的 sn-p 注入您的页面。但是CKEditor不只是js的sn-p,还有很多外部的js是必须要包含的,这里就是permission denied进来的地方:) 无需注入所有CKEditor,您可以注入一个脚本标签来加载所有这些。与从外部 CDN 拉取它或其他库(如 jquery)没有什么不同。相同的原始策略不适用于脚本加载:***.com/questions/10530554/… 您是根据自己的经验说话还是继续谷歌搜索?因为我在动态 html 生成的内容中加载外部 js 文件时遇到了这个问题,我告诉你 IE 不会让你加载该脚本,从而引发权限被拒绝错误。以上是关于CKEditor 可以在 WinForms 应用程序中用于 (X)HTML 编辑吗?的主要内容,如果未能解决你的问题,请参考以下文章