2015-06-29 24 views
2

我想在Winform應用程序中使用CKEditor來加載,處理和存儲HTML內容到Sql Server數據庫中。在C#中獲取和設置CKEditor HTML Winform應用程序

我發現如何得到的CKEditor到一個winform這個偉大的職位:

Can the CKEditor be used in a WinForms application for (X)HTML editing?

但是它沒有詳細說明如何加載HTML內容或提取它一旦用戶操縱它。

我曾嘗試:

webBrowser1.Document.GetElementById("editor1").InnerText;

然而一個返回加載到編輯器中的初始數據,它是由用戶操縱之後,它仍然返回相同的值。

如果有人可以在上面的鏈接中加入一些代碼來加載編輯器的一些內容和一些在WinForm應用程序中顯示消息框的代碼以及它的當前內容,我真的很感激它。我懷疑它需要在最初打開的html文件中使用更多的JavaScript,但我對JavaScript一無所知,並且我花了好幾天的時間試圖通過它而沒有成功。

在此先感謝。

回答

2

在一些更多的研究,今天,我能回答我的問題...... 空白HTML設置文件我加載到WebBrowser控件看起來是這樣的:

<html> 
    <head> 
     <script src="http://cdn.ckeditor.com/4.4.7/full/ckeditor.js"></script> 
    </head> 
    <script> 
     var editor1, html = ''; 

     function createEditor() { 
      if (editor1) 
       return; 

      // Create a new editor instance inside the <div id="editor1"> element, 
      // setting its value to html. 
      var config = {}; 
      editor1 = CKEDITOR.appendTo('editor1', config, html); 
     } 

     function getHtml() { 
      if (!editor1) 
       return; 
      html = editor1.getData(); 
      return html 

     } 

     function setHtml(vsHtml) { 
      if (!editor1) 
       return; 
      editor1.setData(vsHtml) 
     } 
    </script> 
    <div id="editor1"></div> 
</html> 

的C#代碼看起來像這個:

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

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

     private void Form1_Load_1(object sender, EventArgs e) 
     { 
      webBrowser1.Navigate("C:\\AMResearch\\HTMLEditor\\blank.html"); 
      Application.DoEvents(); 
     } 

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

     private void button2_Click(object sender, EventArgs e) 
     { 
      string sHtml; 
      sHtml = (string)webBrowser1.Document.InvokeScript("getHtml"); 
      MessageBox.Show(sHtml); 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      Object[] objArray = new Object[1]; 
      objArray[0] = "<p>Hellow World!</p>"; 
      webBrowser1.Document.InvokeScript("setHtml", objArray); 
     } 

    } 
} 
相關問題