0

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

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

wbEmailText.Navigate( "about:blank" );
( (HTMLDocument)wbEmailText.Document.DomDocument ).designMode = "On";

因此用户可以修改 WebBrowser 的内容。现在我需要检测用户何时修改内容,因为我必须对其进行验证。我曾尝试使用一些 WebBrowser 的事件,如 DocumentCompleted、Navigated 等,但没有一个有效。有人可以给我建议吗?提前致谢!

4

1 回答 1

0

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

您可以捕获许多事件,然后挂钩以了解是否已进行更改。可在此处找到事件列表:http: //msdn.microsoft.com/en-us/library/ms535862 (v=vs.85).aspx

我们所做的是捕捉关键事件(他们正在打字)和点击事件(他们已经移动焦点或拖放等)并处理它。

一些示例代码,注意一些是伪代码,因为我不记得实际代码。

// 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
}
于 2013-03-14T09:59:58.803 回答