WebBrowser中调用加载页面的Javascript方法

using AxSHDocVw;
using Microsoft.VisualBasic;
using mshtml;

AxWebBrowser awb 
= (AxWebBrowser)page.Controls[0];
SHDocVw.IWebBrowser2 iwb 
= (SHDocVw.IWebBrowser2)awb.Application;
object app = awb.Application;
string name = Information.TypeName(app);

HTMLDocumentClass doc 
= (HTMLDocumentClass)awb.Document;
if (doc != null)
{
    HTMLBodyClass body 
= (HTMLBodyClass)doc.getElementsByTagName("body").item(null0);
    HTMLScriptElementClass script 
= (HTMLScriptElementClass)doc.createElement("script");
    script.setAttribute(
"type""text/javascript"0);
    script.text 
= "closeControl();";
    body.appendChild((IHTMLDOMNode)script);
    doc.close();
}
awb.Dispose();
awb 
= null;

tab.TabPages.Remove(page);

page.Dispose();
GC.Collect();

#import "C:\WINDOWS\system32\mshtml.tlb"

/* 动态修改网页中的内容
IHTMLDocument2* pHTMLFocument2;
LPDISPATCH lpDispatch;
lpDispatch = m_wb.get_Document();
if(lpDispatch)
{
    HRESULT hr;
    hr = lpDispatch->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pHTMLFocument2);
    lpDispatch->Release();
    IHTMLElement* pBody;
    hr = pHTMLFocument2->get_body(&pBody);
    UpdateData(TRUE);
    BSTR bstr = L"hxh";
    // pBody->get_innerHTML(bstr);
    pBody->put_innerHTML(bstr);  
    SysFreeString(bstr);
    pBody->Release();
*/

// 动态执行网页中的方法
MSHTML::IHTMLDocument2Ptr spDoc(m_wb.get_Document());
if (spDoc)
{
    IDispatchPtr spDisp(spDoc
->GetScript());
    
if (spDisp)
    {
        OLECHAR FAR
* szMember = L"closeControl";
        DISPID dispid;
        HRESULT hr 
= spDisp->GetIDsOfNames(IID_NULL, &szMember, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
        
if (SUCCEEDED(hr))
        {
            COleVariant vtResult;
            
static BYTE parms[] = VTS_BSTR;
            COleDispatchDriver dispDriver(spDisp, FALSE);
            dispDriver.InvokeHelper(dispid, DISPATCH_METHOD, VT_VARIANT, (
void*)&vtResult, parms, "");
        }
    }
}
posted @ 2009-10-16 17:34  angushine  阅读(1178)  评论(0编辑  收藏  举报