私は axWebBrowser を使用しており、リストボックスの選択された項目が変更されたときに機能するスクリプトを作成する必要があります。
デフォルトの webBrowser コントロールには、次のようなメソッドがあります。
WebBrowserEx1.Document.InvokeScript("script")
しかし、axWebBrowser ではスクリプトを実行できません。そして、このコントロールに関するドキュメントはありません。
誰でも方法を知っていますか?
私は axWebBrowser を使用しており、リストボックスの選択された項目が変更されたときに機能するスクリプトを作成する必要があります。
デフォルトの webBrowser コントロールには、次のようなメソッドがあります。
WebBrowserEx1.Document.InvokeScript("script")
しかし、axWebBrowser ではスクリプトを実行できません。そして、このコントロールに関するドキュメントはありません。
誰でも方法を知っていますか?
遅い答えですが、うまくいけば誰かを助けるかもしれません。WebBrowser ActiveX コントロールを使用する場合、スクリプトを呼び出す方法はいくつかあります。WebBrowser コントロールの WinForms バージョン ( webBrowser.HtmlDocument.DomDocument経由) および WPF バージョン ( 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);
}
JavaScript が機能するには、少なくとも 1 つ<script>
のタグが必要でeval
あり、上記のように挿入できます。
webBrowser.Document.InvokeScript("setTimer", new[] { "window.external.notifyScript()", "1" })
あるいは、JavaScript エンジンをまたは などで非同期に初期化することもできますwebBrowser.Navigate("javascript:(window.external.notifyScript(), void(0))")
。