2013-03-07 3 views
2

axWebBrowser를 사용하고 있으며 목록 상자의 선택된 항목이 변경 될 때 작동하는 스크립트 작업을 만들어야합니다.msHTML에서 스크립트를 호출하는 방법

기본 webBrowser 컨트롤에는 다음과 같은 메서드가 있습니다.

WebBrowserEx1.Document.InvokeScript("script") 

하지만 axWebBrowser에서 어떤 스크립트도 작동하지 않습니다! 이 컨트롤에 대한 문서는 없습니다.

누구나 알고 계십니까?

답변

3

늦게 답변하지만, 아직 도움이 될 수 있습니다. WebBrowser ActiveX 컨트롤을 사용할 때 스크립트를 호출하는 데는 여러 가지 방법이 있습니다. 같은 기술은 (webBrowser.HtmlDocument.DomDocument를 통해) WebBrowser 컨트롤의 윈폼 버전 사용 (webBrowser.Document를 통해) WPF 버전 할 수 있습니다

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의 eval위한 적어도 하나 개의 <script> 태그이 있어야한다 위와 같이 주입하십시오.

또는 자바 스크립트 엔진은 webBrowser.Document.InvokeScript("setTimer", new[] { "window.external.notifyScript()", "1" }) 또는 webBrowser.Navigate("javascript:(window.external.notifyScript(), void(0))")과 같은 형식으로 비동기 적으로 초기화 할 수 있습니다.

관련 문제