简体   繁体   English

使用WYSIWYG编辑器编辑HTML

[英]Editing HTML with a WYSIWYG Editor

I have a datagridview with HTML strings. 我有一个带HTML字符串的datagridview。 Using a CellDoubleClick event, I am displaying the html string in a WebBrowser control. 使用CellDoubleClick事件,我在WebBrowser控件中显示html字符串。

In Form1 在Form1中

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        if (e.ColumnIndex != 0 && e.RowIndex != -1)
        {
            string s = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
            this.f2 = new Form2(s);
            f2.ShowDialog();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

In Form2 在Form2中

private IHTMLDocument2 doc;
string reply;

public Form2(string reply)
{
    InitializeComponent();
    this.reply = reply;
}

private void Form2_Load(object sender, EventArgs e)
{
    webBrowser1.DocumentText = reply; <--- string from DataGridView

    IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
    range.pasteHTML(webBrowser1.DocumentText);
    range.collapse(false);
    range.select();

    doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
    doc.designMode = "On";
}

Using the above code, I can successfully display the HTML string as a plaintext, however I am unable to edit it. 使用上面的代码,我可以成功地将HTML字符串显示为纯文本,但是我无法编辑它。 Alternatively, if I use this code: 或者,如果我使用此代码:

private IHTMLDocument2 doc;
private void Form2_Load(object sender, EventArgs e)
{
    webBrowser1.DocumentText = reply; <--- string from DataGridView

    doc = webBrowser1.Document.DomDocument as IHTMLDocument2;
    doc.designMode = "On";

    IHTMLTxtRange range = doc.selection.createRange() as IHTMLTxtRange;
    range.pasteHTML(webBrowser1.DocumentText);
    range.collapse(false);
    range.select();
}

It will be a blank form, but I will be able to write to it. 这将是一个空白表格,但我可以写信给它。

I have a feeling it has to do with range.pasteHTML(webBrowser1.DocumentText); 我觉得它与range.pasteHTML(webBrowser1.DocumentText); being in the Form2_Load method, but I'm unaware of any other method that will allow me to display the HTML string from the DataGridView upon opening Form2. 在Form2_Load方法中,但我不知道任何其他方法将允许我在打开Form2时显示DataGridView中的HTML字符串。

I want to allow users to be able to edit the HTML string as plaintext (after which it will be converted back to HTML and displayed in the datagridview). 我想允许用户能够将HTML字符串编辑为纯文本(之后它将被转换回HTML并显示在datagridview中)。

Its possible! 这是可能的! You can edit HTML using the default WebBrowser control, 您可以使用默认的WebBrowser控件编辑HTML,

  1. Add a reference to the "Microsoft.mshtml.dll" file, available here. 添加对此处提供的“Microsoft.mshtml.dll”文件的引用。

  2. Assuming your WebBrowser is named "browser", add this code in the Form.Load event (browser.Document.DomDocument as mshtml.IHTMLDocument2).designMode = "On"; 假设您的WebBrowser被命名为“browser”,请在Form.Load事件中添加此代码(browser.Document.DomDocument as mshtml.IHTMLDocument2).designMode = "On";

  3. Call the following functions to format the selected text: 调用以下函数以格式化所选文本:


browser.document.ExecCommand("Bold", false, null);
browser.document.ExecCommand("Underline", false, null);
browser.document.ExecCommand("Italics", false, null);
browser.document.ExecCommand("StrikeThrough", false, null);
browser.document.ExecCommand("FontName", false, "Times New Roman");
browser.document.ExecCommand("FontName", false, "Arial");
browser.document.ExecCommand("FontName", false, "etc.");
browser.document.ExecCommand("FontSize", false, "1");
browser.document.ExecCommand("FontSize", false, "2");
browser.document.ExecCommand("FontSize", false, "3");
browser.document.ExecCommand("InsertUnorderedList", false, null);
browser.document.ExecCommand("InsertOrderedList", false, null);
browser.document.ExecCommand("Cut", false, null);
browser.document.ExecCommand("Copy", false, null);
browser.document.ExecCommand("Paste", false, null);
browser.document.ExecCommand("CreateLink", true, null);

The WebBrowser control does not allow editing, and is designed to view web pages only. WebBrowser控件不允许编辑,仅用于查看网页。 Its actually the Internet Explorer/Trident rendering engine operating behind the scenes that parses the HTML and renders the final page complete with DOM/JS support. 它实际上是在幕后操作的Internet Explorer / Trident渲染引擎,它解析HTML并使用DOM / JS支持完成最终页面。 No popular browser supports editing of HTML pages, IMO, and neither does IE. 没有流行的浏览器支持编辑HTML页面,IMO,IE也不支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM