在Windows窗体中使用C#在编辑模式下使用MSHTML WebBrowser时,如何检测文档更改?

在Windows窗体中使用C#在编辑模式下使用MSHTML WebBrowser时,如何检测文档更改?,c#,.net,webbrowser-control,axwebbrowser,C#,.net,Webbrowser Control,Axwebbrowser,我正在WinForms应用程序中使用.NET WebBrowser控件来实现一个非常基本的电子邮件模板编辑器 我已通过以下代码将其设置为编辑模式: wbEmailText.Navigate( "about:blank" ); ( (HTMLDocument)wbEmailText.Document.DomDocument ).designMode = "On"; 因此,用户可以修改WebBrowser内容。 现在我需要检测用户何时修改内容,因为我必须验证它。 我曾尝试使用WebBrowser的

我正在WinForms应用程序中使用.NET WebBrowser控件来实现一个非常基本的电子邮件模板编辑器

我已通过以下代码将其设置为编辑模式:

wbEmailText.Navigate( "about:blank" );
( (HTMLDocument)wbEmailText.Document.DomDocument ).designMode = "On";
因此,用户可以修改WebBrowser内容。 现在我需要检测用户何时修改内容,因为我必须验证它。 我曾尝试使用WebBrowser的一些事件,如DocumentCompleted、Navigated等,但没有一个成功。 有人能给我一些建议吗?
提前谢谢

我确实有一些实用的、真正世界级的代码,但那个项目已经有5年的历史了,我已经离开了公司。我已经搜索了我的备份,但找不到它,所以我试图从内存工作,并给你一些指针

你可以捕捉到很多事件,然后通过这些事件了解是否已经做出了改变。可在此处找到事件列表:

我们所做的是捕捉关键事件(他们正在键入)并单击事件(他们移动了焦点或拖放了等等),然后处理这些事件

一些示例代码,注意一些位是伪代码,因为我记不清实际的代码

// Pseudo code
private string _content = string.empty;

private void frmMain_Load(object sender, EventArgs e)
{
    // This tells the browser that any javascript requests that call "window.external..." to use this form, useful if you want to hook up events so the browser can notify us of things via JavaScript
    webBrowser1.ObjectForScripting = this;
    webBrowser1.Url = new Uri("yourUrlHere");
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    // Store original content
    _content = webBrowser1.Content; // Pseudo code
    webBrowser1.Document.Click += new HtmlElementEventHandler(Document_Click);
    webBrowser1.Document.PreviewKeyDown +=new PreviewKeyDownEventHandler(Document_PreviewKeyDown);
}

protected void Document_Click(object sender, EventArgs e)
{
    DocumentChanged();
}

protected void Document_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    DocumentChanged();
}

private void DocumentChanged()
{
    // Compare old content with new content
    if (_content == webBrowser1.Content)    // Pseudo code
    {
        // No changes made...
        return;
    }

    // Add code to handle the change
    // ...

    // Store current content so can compare on next event etc
    _content = webBrowser1.Content; // Pseudo code
}

我有一些示例代码,完全符合您的要求。不幸的是,我要到明天(24小时)才能访问它,所以如果你在这之前没有得到答案,我将发布我的示例代码。谢谢Belogix,这将对我和社区非常有帮助,因此如果你能尽快发布它,我将非常感激。嗨Belogix,很遗憾,我还没有想出解决方案。您找到您的示例代码了吗?抱歉,仍然没有机会找到代码,但请签出: