• WTL IWebBrowser2调用网页中javascript函数


    转自:http://blog.csdn.net/tujiaw/article/details/6556880

    1.简单的含有js的网页:

    [javascript] view plaincopy
     
    1. <html>  
    2.     <head>  
    3.     <mce:script type = "text/javascript"><!--  
    4.     function test(){  
    5.     document.write("hello world!");  
    6.     }  
    7.     function test2(){  
    8.     alert("hello, world");  
    9.     }  
    10.     function link(){  
    11.     window.location.href = "http://www.baidu.com";  
    12.     }  
    13.     test();  
    14.       
    15. // --></mce:script>  
    16.     </head>  
    17.     <body>  
    18.         <p><a href = "javascript:link()">this is js link</a></p>  
    19.     </body>  
    20. </html>  

    2.WebBrowser ActiveX控件成员变量:

    [c-sharp] view plaincopy
     
    1. CComPtr<IWebBrowser2> m_pWb2;   

    3.在OnInitDialog中

    [c-sharp] view plaincopy
     
    1. CAxWindow wndIE = GetDlgItem(IDC_IE);     
    2. HRESULT hr;     
    3. hr = wndIE.QueryControl(&m_pWb2);     
    4. if (m_pWb2)     
    5. {     
    6.     CComVariant v;     
    7.     m_pWb2->Navigate(CComBSTR(_T("...//test.html")), &v, &v, &v, &v);   // 这里地址省略了,注意要用//  
    8. }   

    4.在OnOk函数里测试

    [c-sharp] view plaincopy
     
    1. LRESULT OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)  
    2.     {  
    3.         // TODO: Add validation code   
    4.         HRESULT hr;  
    5.         CComPtr<IDispatch> spdispDoc;     
    6.         hr = m_pWb2->get_Document(&spdispDoc);  
    7.         if (FAILED(hr))     
    8.         {     
    9.             return hr;     
    10.         }     
    11.         CComQIPtr<IHTMLDocument2> spDoc = spdispDoc;  
    12.           
    13.         VARIANT ret;  
    14.         ret.vt = VT_EMPTY;  
    15.         return WtlCallJavascript(spDoc, CComBSTR("test2()"), &ret);  
    16.         //CloseDialog(wID); // 屏蔽掉回车关闭对话框  
    17.     }  

    上面需要的函数

    [c-sharp] view plaincopy
     
    1. LRESULT WtlCallJavascript(CComQIPtr<IHTMLDocument2> htmlDoc, BSTR strCode, VARIANT *pvarRet)  
    2. {  
    3.     HRESULT hr = -1;  
    4.     if (!(htmlDoc && strCode))  
    5.     {  
    6.         return hr;  
    7.     }  
    8.     IHTMLWindow2 *pHtmlWnd;  
    9.     hr = htmlDoc->get_parentWindow(&pHtmlWnd);  
    10.     if (SUCCEEDED(hr))  
    11.     {  
    12.         hr = pHtmlWnd->execScript(strCode, CComBSTR(_T("javascript")), pvarRet);  
    13.     }  
    14.     return hr;  
    15. }  

    运行程序后它会打开我们的html网页, 然后按Enter键会弹出hello, world小的提示窗口说明js里的那个函数执行了。

    插入WebBrowser控件的方法可以看这里:WTL 通过IWebBrowser2接口使WebBrowser控件在自己的窗口打开网页

  • 相关阅读:
    PCI-DSS-术语小结
    Visio快捷键-小结(Microsoft Visio绘图工具)
    vs2019快捷键-小结(C#开发工具Visio studio 2019)
    消息及时推送技术websocket
    requests爬虫get请求三部曲(快速编码)-小结
    cnblogs_client博客园客户端——雏形
    重庆购房资料
    比较2个时刻日期字串的时间差:距离现在的时间距离(不同时间格式)
    比较2个时刻日期字串的时间差
    时间戳转为日期字串
  • 原文地址:https://www.cnblogs.com/dps001/p/4381531.html
Copyright © 2020-2023  润新知