// 找到树控件所在框架,调用相应脚本
CComPtr<IHTMLDocument2> pDocument;
if (SUCCEEDED(GetDHtmlDocument(&pDocument)) && pDocument)
{
CComPtr<IHTMLFramesCollection2> pFrames = NULL;
if (SUCCEEDED(pDocument->get_frames(&pFrames)) && pFrames)
{
long lLen = 0;
if (SUCCEEDED(pFrames->get_length(&lLen)))
{
for (long i = 0; i < lLen; ++i)
{
VARIANT vIndex, vResult;
vIndex.vt = VT_I4;
vIndex.lVal = i;
vResult.vt = VT_DISPATCH;
if (SUCCEEDED(pFrames->item(&vIndex, &vResult)) && vResult.ppdispVal)
{
CComPtr<IDispatch> lpDispatch = (LPDISPATCH)vResult.ppdispVal;
CComPtr<IHTMLWindow2> pHTMLWnd = NULL;
if (SUCCEEDED(lpDispatch->QueryInterface(IID_IHTMLWindow2, (LPVOID *)&pHTMLWnd)))
{
CComPtr<IHTMLDocument2> pFrameDoc = NULL;
if(SUCCEEDED(pHTMLWnd->get_document( &pFrameDoc)))
{
//work with the pFrameDoc...
CComPtr<IDispatch> pScript = NULL;
if (SUCCEEDED(pFrameDoc->get_Script(&pScript)) && pScript)
{
BSTR btrMethod = _bstr_t(_T("OnInsertTreeItem")).GetBSTR();
DISPID dispID = DISPID_UNKNOWN;
if (SUCCEEDED(pScript->GetIDsOfNames(IID_NULL, &btrMethod, 1, LOCALE_SYSTEM_DEFAULT, &dispID)))
{
// 调用方法
DISPPARAMS dispparams;
memset(&dispparams, 0, sizeof(DISPPARAMS));
dispparams.cArgs = 1;
dispparams.cNamedArgs = 0;
VARIANT varArgs[1];
varArgs[0].vt = VT_BSTR;
varArgs[0].bstrVal = (BSTR)l;
dispparams.rgvarg = varArgs;
EXCEPINFO excepInfo;
memset(&excepInfo, 0, sizeof(EXCEPINFO));
VARIANT* pVarResult = NULL;
UINT nArgErr = (UINT)-1;
pScript->Invoke(dispID, IID_NULL, 0, DISPATCH_METHOD, &dispparams, pVarResult, &excepInfo, &nArgErr);
}
}
return 0;
}
}
}
}
}
}
}