编辑HTML与WYSIWYG编辑器 [英] Editing HTML with a WYSIWYG Editor

查看:180
本文介绍了编辑HTML与WYSIWYG编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML字符串一个DataGridView。使用CellDoubleClick事件,我展示了WebBrowser控件的HTML字符串。



在Form1



 私人无效dataGridView1_CellDoubleClick(对象发件人,DataGridViewCellEventArgs E)
{

{
如果(e.ColumnIndex!= 0安培;&安培;!e.RowIndex = -1)
{
字符串s = dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex] .Value.ToString();
this.f2 =新的窗体2(S);
f2.ShowDialog();
}
}
赶上(异常前)
{
MessageBox.Show(ex.Message,ERROR,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}



在窗体2

 私人的IHTMLDocument2文档; 
串回复;

公共窗体2(字符串回复)
{
的InitializeComponent();
this.reply =答复;
}

私人无效Form2_Load(对象发件人,EventArgs五)
{
webBrowser1.DocumentText =回复; < ---从DataGridView

IHTMLTxtRange范围字符串= doc.selection.createRange()作为IHTMLTxtRange;
range.pasteHTML(webBrowser1.DocumentText);
range.collapse(假);
range.select();

DOC = webBrowser1.Document.DomDocument为的IHTMLDocument2;
doc.designMode =开;
}



使用上面的代码,我可以成功显示HTML字符串作为明文,然而,我无法对其进行编辑。另外,如果我用这个代码:

 私人的IHTMLDocument2文档; 
私人无效Form2_Load(对象发件人,EventArgs五)
{
webBrowser1.DocumentText =回复; < ---从DataGridView

DOC = webBrowser1.Document.DomDocument作为字符串的IHTMLDocument2;
doc.designMode =开;

IHTMLTxtRange范围= doc.selection.createRange()作为IHTMLTxtRange;
range.pasteHTML(webBrowser1.DocumentText);
range.collapse(假);
range.select();
}



这将是一个空白表格,但我能写它。



我有一种感觉它与 range.pasteHTML(webBrowser1.DocumentText)做; 在Form2_Load是方法,但我不知道任何其他的方法,让我来显示在打开窗体2 DataGridView的HTML字符串的。



我想允许用户能够编辑HTML字符串作为纯文本(后,它会转换回HTML并显示在DataGridView)。


解决方案

其可能!您可以使用默认的编辑HTML web浏览器控制,




  1. 添加参考Microsoft.mshtml.dll文件,可在这里。


  2. 假设你的web浏览器被命名为浏览器,在Form.Load事件
    添加以下代码(如browser.Document.DomDocument mshtml.IHTMLDocument2).designMode =开;


  3. 调用以下函数所选文本格式为:







  browser.document.ExecCommand(大胆,假,空); 
browser.document.ExecCommand(下划线,假,空);
browser.document.ExecCommand(斜体,假,空);
browser.document.ExecCommand(删除线,假,空);
browser.document.ExecCommand(字体名,假宋体);
browser.document.ExecCommand(字体名,假宋体);
browser.document.ExecCommand(FONTNAME虚假等);
browser.document.ExecCommand(字号,假,1);
browser.document.ExecCommand(字号,假,2);
browser.document.ExecCommand(字号,假,3);
browser.document.ExecCommand(InsertUnorderedList,假,空);
browser.document.ExecCommand(InsertOrderedList,假,空);
browser.document.ExecCommand(剪切,假,空);
browser.document.ExecCommand(复制,假,空);
browser.document.ExecCommand(粘贴,假,空);
browser.document.ExecCommand(建立连结,真实,NULL);






<罢工>的 web浏览器控件不允许编辑,并且被设计为只查看网页。它实际上Internet浏览器/三叉戟渲染引擎,解析HTML和呈现的最后一页完整的DOM / JS支持在幕后操作。没有流行的浏览器支持HTML页面,IMO的编辑,而且也不IE浏览器。


I have a datagridview with HTML strings. Using a CellDoubleClick event, I am displaying the html string in a WebBrowser control.

In 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

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. 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); 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.

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).

解决方案

Its possible! You can edit HTML using the default WebBrowser control,

  1. Add a reference to the "Microsoft.mshtml.dll" file, available here.

  2. Assuming your WebBrowser is named "browser", add this code in the Form.Load event (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. 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. No popular browser supports editing of HTML pages, IMO, and neither does IE.

这篇关于编辑HTML与WYSIWYG编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆