我已將winforms html editor的代碼改編爲C#(請參閱下文)。是否可以改用CKEditor?CKEditor是否可用於WinForms應用程序(X)HTML編輯?
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;
}
}
}
實際上,被拒絕的權限可能會阻止您將腳本加載到Web瀏覽器控件中。 – Constantin
如何?這裏沒有跨域或同源的問題,因爲他正在動態地創建頁面。關於SO的幾個例子:http://stackoverflow.com/questions/153748/how-to-inject-javascript-in-webbrowser-control。 – LouD
所有這些示例都顯示瞭如何將js代碼片段插入到頁面中。但是CKEditor不僅僅是js的一個片段,還有很多外部的js必須被包含,這裏是被拒絕的權限進入:) – Constantin