zoukankan      html  css  js  c++  java
  • c&c++MFC 调用 js 函数代码

    调用函数代码和示例
     
    Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 首先 .h 文件中定义:
     
    CComPtr<IHTMLWindow2> m_sphtmlWnd;
    CComPtr<IHTMLDocument2> m_spHtmlDoc;
     
     
     
    //方法1 可以取得返回值。。。。。
     
    //调用方法示例:stringArray为传入参数列表,getSum为js函数,pVarResult 为返回值
     
    /* CStringArray stringArray;
    stringArray.RemoveAll();
    stringArray.SetSize(5);
    stringArray[0].Format(L"%d",m_poiStart.x);
    stringArray[1].Format(L"%d",m_poiStart.y);
    stringArray[2].Format(L"%d",m_poiEnd.x);
    stringArray[3].Format(L"%d",m_poiEnd.y);
    stringArray[4].Format(L"%d",a + 1);
    CComVariant pVarResult = CallJScript(L"getSum", stringArray);*/
     
    bool Cems_mysql_appView::GetJScript(CComPtr<IDispatch>& spDisp)
    {
    m_spHtmlDoc = (IHTMLDocument2*)(GetHtmlDocument());
    if(m_spHtmlDoc == NULL)
    {
    return false;
    }
    HRESULT hr = m_spHtmlDoc->get_Script(&spDisp);
    ATLASSERT(SUCCEEDED(hr));
    return SUCCEEDED(hr);
    }
     
    CComVariant Cems_mysql_appView::CallJScript(const CString strFunc,const CStringArray& paramArray)
    {
    //Getting IDispatch for Java Script objects
    CComPtr<IDispatch> spScript;
    if(!GetJScript(spScript))
    {
    return false;
    }
    //Find dispid for given function in the object
    CComBSTR bstrMember(strFunc);
    DISPID dispid = NULL;
    HRESULT hr = spScript->GetIDsOfNames(IID_NULL,&bstrMember,1,
    LOCALE_SYSTEM_DEFAULT,&dispid);
    if(FAILED(hr))
    {
    return false;
    }
     
    const int arraySize = paramArray.GetSize();
    //Putting parameters
    DISPPARAMS dispparams;
    memset(&dispparams, 0, sizeof dispparams);
    dispparams.cArgs = arraySize;
    dispparams.rgvarg = new VARIANT[dispparams.cArgs];
    dispparams.cNamedArgs = 0;
     
    for( int i = 0; i < arraySize; i++)
    {
    CComBSTR bstr = paramArray.GetAt(arraySize - 1 - i);
    // back reading
    bstr.CopyTo(&dispparams.rgvarg[i].bstrVal);
    dispparams.rgvarg[i].vt = VT_BSTR;
    }
    EXCEPINFO excepInfo;
    memset(&excepInfo, 0, sizeof excepInfo);
    CComVariant vaResult;
    UINT nArgErr = (UINT)-1; // initialize to invalid arg
    //Call JavaScript function
    hr = spScript->Invoke(dispid,IID_NULL,0,DISPATCH_METHOD,&dispparams,&vaResult,&excepInfo,&nArgErr);
    delete [] dispparams.rgvarg;
    if(FAILED(hr))
    {
    return false;
    }
    return vaResult;
    }
     
     
     
    //方法2
     
    /*调用方法,不必多说。
     
    CString str;
    str.Format(L"getSum(%d,%d,%d,%d,%d)",m_poiStart.x,m_poiStart.y,m_poiEnd.x,m_poiEnd.y,a+1);
    ExecScript(str);
     
    */
    HRESULT Cems_mysql_appView::ExecScript(CString strjs)
    {
    HRESULT hr;
    m_spHtmlDoc = (IHTMLDocument2*)(GetHtmlDocument());
    if(m_sphtmlWnd == NULL)
    {
    hr = m_spHtmlDoc->get_parentWindow(&m_sphtmlWnd);//获取脚本指针
    }
    CComBSTR bstrjs = strjs.AllocSysString();
    CComBSTR bstrlan = SysAllocString(L"javascript");
    VARIANT varRet;
    hr = m_sphtmlWnd->execScript(bstrjs, bstrlan, &varRet);
    //m_sphtmlWnd = NULL;
    return 1;
    }
     
  • 相关阅读:
    生成组合算法
    今天复习C++的const,又学到了很多内容,总结一下[原创]
    文字和图片在一行,文字靠左,图片靠右(div)[转]
    联合查询
    排序查询
    分页查询 ★
    分组查询
    基础查询
    条件查询
    连接查询 (二)sql99语法
  • 原文地址:https://www.cnblogs.com/blogpro/p/11426706.html
Copyright © 2011-2022 走看看