2011-05-13 36 views
4

我已將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; 
    } 
} 
} 

回答

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> 
+0

實際上,被拒絕的權限可能會阻止您將腳本加載到Web瀏覽器控件中。 – Constantin

+0

如何?這裏沒有跨域或同源的問題,因爲他正在動態地創建頁面。關於SO的幾個例子:http://stackoverflow.com/questions/153748/how-to-inject-javascript-in-webbrowser-control。 – LouD

+0

所有這些示例都顯示瞭如何將js代碼片段插入到頁面中。但是CKEditor不僅僅是js的一個片段,還有很多外部的js必須被包含,這裏是被拒絕的權限進入:) – Constantin

相關問題