我确实有一些工作的,真正的世界代码,但那个项目大约有 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
}