如何调用脚本MSHTML工作 [英] How to invoke scripts work in msHTML
问题描述
我使用axWebBrowser,我需要做一个脚本的工作时,列表框选择的项目更改它的工作原理。
I'm using axWebBrowser and I need to make a script work which works when selected item of a listbox is changed.
在默认web浏览器控制存在的方法等;
In default webBrowser control there is a method like;
WebBrowserEx1.Document.InvokeScript("script")
但在axWebBrowser我不能工作的任何脚本!而且也没有这个控制文件。
But in axWebBrowser I can not work any script! And there is no documentation about this control.
有谁知道怎么样?
推荐答案
一个迟到的答案,但希望仍然可以帮助别人。有许多方法使用web浏览器的ActiveX控件时调用的脚本。相同的技术也可以用的WinForms版本web浏览器控件的使用(通过 webBrowser.HtmlDocument.DomDocument )和WPF的版本(通过的 webBrowser.Document ):
A late answer, but hopefully still may help someone. There is a number of ways to invoke a script when using WebBrowser ActiveX control. The same techniques can also be used with WinForms version of WebBrowser control (via webBrowser.HtmlDocument.DomDocument) and with WPF version (via webBrowser.Document):
void CallScript(SHDocVw.WebBrowser axWebBrowser)
{
//
// Using C# dynamics, which maps to COM's IDispatch::GetIDsOfNames,
// IDispatch::Invoke
//
dynamic htmlDocument = axWebBrowser.Document;
dynamic htmlWindow = htmlDocument.parentWindow;
// make sure the web page has at least one <script> tag for eval to work
htmlDocument.body.appendChild(htmlDocument.createElement("script"));
// can call any DOM window method
htmlWindow.alert("hello from web page!");
// call a global JavaScript function, e.g.:
// <script>function TestFunc(arg) { alert(arg); }</script>
htmlWindow.TestFunc("Hello again!");
// call any JavaScript via "eval"
var result = (bool)htmlWindow.eval("(function() { return confirm('Continue?'); })()");
MessageBox.Show(result.ToString());
//
// Using .NET reflection:
//
object htmlWindowObject = GetProperty(axWebBrowser.Document, "parentWindow");
// call a global JavaScript function
InvokeScript(htmlWindowObject, "TestFunc", "Hello again!");
// call any JavaScript via "eval"
result = (bool)InvokeScript(htmlWindowObject, "eval", "(function() { return confirm('Continue?'); })()");
MessageBox.Show(result.ToString());
}
static object GetProperty(object callee, string property)
{
return callee.GetType().InvokeMember(property,
BindingFlags.Instance | BindingFlags.GetProperty | BindingFlags.Public,
null, callee, new Object[] { });
}
static object InvokeScript(object callee, string method, params object[] args)
{
return callee.GetType().InvokeMember(method,
BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
null, callee, args);
}
必须有至少一个&LT;脚本&GT;
标记JavaScript的评估
来工作,这可注入如上所示。
There has to be at least one <script>
tag for JavaScript's eval
to work, which can be injected as shown above.
另外,JavaScript引擎可以异步初始化的东西,如 webBrowser.Document.InvokeScript(SetTimer的,新的[] {window.external.notifyScript(),1})
或 webBrowser.Navigate(JavaScript的:(window.external.notifyScript(),空(0)))。
Alternatively, JavaScript engine can be initialized asynchronously with something like webBrowser.Document.InvokeScript("setTimer", new[] { "window.external.notifyScript()", "1" })
or webBrowser.Navigate("javascript:(window.external.notifyScript(), void(0))")
.
这篇关于如何调用脚本MSHTML工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!