小弟现在在VC6.0下新建了一个对话框程序,在里面加了一个WebBrowser2控件,现在窗口上有两个按钮:button1,button2
在单击button1后WebBrowser2这个控件会打开一个网页,例如www.baidu.com,现在我想实现,在单击button2时,在不移动鼠标的情况下,模拟鼠标在网页上单击了"百度"的"MP3"这个文字超级链接?请问要怎么做?
我原来想通过m_web.SendMessage(WM_LBUTTONDOWN, x, y)这种方法来实现,但好像不行,不知道为什么;网上有人也说用IHtmlElement这种方法,但都说得不够清楚,不知道有哪位高手能帮忙解决一下,小弟跪谢!
在单击button1后WebBrowser2这个控件会打开一个网页,例如www.baidu.com,现在我想实现,在单击button2时,在不移动鼠标的情况下,模拟鼠标在网页上单击了"百度"的"MP3"这个文字超级链接?请问要怎么做?
我原来想通过m_web.SendMessage(WM_LBUTTONDOWN, x, y)这种方法来实现,但好像不行,不知道为什么;网上有人也说用IHtmlElement这种方法,但都说得不够清楚,不知道有哪位高手能帮忙解决一下,小弟跪谢!
解决方案 »
- 关于输入法
- VC.net下使用DDK开发USB应用程序的问题
- 花了我一上午也没能解决这个编译错误
- 怎么获取这个COM接口的这个属性
- 基于多文档结构的图像处理程序,如果想将改变后的图像新开一个窗口,这样每一部操作后的图像都能出现在框架中,应该怎么做啊!谢谢!
- 为什么程序在Release版本和Debug版本在调试中有不同的表现,一个整型变量获得一个函数返回的整型值,明明返回1,可就是赋不上,在Degbug
- 一个程序员的迷惘和困惑
- 在Win9x下,没有CreateRemoteThread和VirtualAllocEx函数,但如何用CreateKernelThread和Alloc函数代替
- 线程通讯问题
- 请问高手: VC6下如何打开另一个共享PC的文件? (加100分再问)
- 问一个字符串输出函数TextOut的问题
- 在WebBrowser模拟鼠标单击,要怎么实现?
{
if(pDisp != NULL)
{
IHTMLDocument2* pHTMLDocument2=NULL;
HRESULT hr;
hr = pDisp->QueryInterface(IID_IHTMLDocument2,
(void**)&pHTMLDocument2); if(hr == S_OK)
{
IHTMLElementCollection* pColl = NULL;
hr = pHTMLDocument2->get_all(&pColl);
if(hr==S_OK && pColl!=NULL)
{
LONG celem;
hr = pColl->get_length(&celem);
if(hr == S_OK)
{
for(int i=0;i<celem;i++)
{
VARIANT varIndex;
varIndex.vt = VT_UINT;
varIndex.lVal = i;
VARIANT var2;
VariantInit(&var2);
IDispatch* pDisp;
hr = pColl->item(varIndex,var2,&pDisp); if(hr == S_OK)
{
IHTMLElement* pElem;
hr = pDisp->QueryInterface(IID_IHTMLElement,
(void**)&pElem);
if(hr == S_OK)
{ //BSTR bstr;
// hr=pElem->get_tagName(&bstr);
// WideString strTag;
// strTag = bstr;
//--------------------------------
IHTMLAnchorElement* pLink;
hr = pDisp->QueryInterface(
IID_IHTMLAnchorElement,
(void **)&pLink ); if ( hr == S_OK )
{
BSTR bstr;
hr = pLink->get_href(&bstr);
if(hr == S_OK)
{
WideString mStr = bstr;
if(mStr.Pos(strLinkName)>=0)
{
//Is this a User Id frield
pElem->click();
i=celem;
//Paste the User Id
}
}
pLink->Release();
}
//---------------------------
}
} }
pColl->Release();
}
pHTMLDocument2->Release();
} } pDisp->Release(); }
return true;
}
BOOL bFound = FALSE;_bstr_t bStrWindowName;void ProcessElement(IHTMLElement * pElt)
{
CComPtr<IHTMLElement> pElement;
pElement = pElt;
_bstr_t bStrName, bStrTitle;
CComBSTR cbName, cbTitle; pElement->get_tagName(&cbName);//获取tagName
bStrName = cbName;
pElement->get_title(&cbTitle);
bStrTitle = cbTitle;
LogStr("d:\\Logstr.log", " IHTMLElement: get_tagName: %s get_title: %s",
(CHAR *)bStrName, (CHAR *)bStrTitle); if(bFound)
{
pElement->click();
bFound = FALSE;
LogStr("d:\\Logstr.log", "click: get_tagName: %s get_title: %s",
(CHAR *)bStrName, (CHAR *)bStrTitle); }}
void ProcessInputElement(IHTMLInputElement * pInput)
{
CComPtr<IHTMLInputElement> pInputElement;
pInputElement = pInput; _bstr_t bStrName, bStrType;
CComBSTR cbName, cbType; pInputElement->get_name(&cbName);
bStrName = cbName;
pInputElement->get_type(&cbType);
bStrType = cbType; LogStr("d:\\Logstr.log", " IHTMLInputElement: get_name: %s get_type: %s",
(CHAR *)bStrName, (CHAR *)bStrType); if(bStrName.length() == 0)
return;
if(bStrType.length() == 0)
return; if(stricmp("B2", bStrName) == 0
&& stricmp("submit", bStrType) == 0)
{
bFound = TRUE;
LogStr("d:\\Logstr.log", "Found: IHTMLInputElement: get_name: %s get_type: %s",
(CHAR *)bStrName, (CHAR *)bStrType);
}}void CAutoRefreshDlg::OnButtonRefresh()
{
VARIANT id, index;
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElement> pElement;
CComPtr<IHTMLInputElement> pInput;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLFormElement> pFormElement;
CComPtr<IHTMLInputTextElement> pInputElement;
_bstr_t bStrText; // TODO: Add your control notification handler code here
int n = m_ctrlIE.GetItemCount();//GetCount(); for (int i = 0; i < n; i ++)
{
IWebBrowser2 *pBrowser = (IWebBrowser2 *)m_ctrlIE.GetItemData(i);
if (pBrowser)
{
pBrowser->Release();
}
} m_ctrlIE.DeleteAllItems();
LogStr("d:\\Logstr.log", (CHAR *)NULL); if(m_spSHWinds == NULL)
return;
n = m_spSHWinds->GetCount();
for (int i = 0; i < n; i++)
{
_variant_t v = (long)i;
IDispatchPtr spDisp = m_spSHWinds->Item(v); SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); //生成一个IE窗口的智能指针
if (spBrowser == NULL)
continue; spDispatch.Release();
if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
pDoc2 = spDispatch;
if(pDoc2==NULL)
continue;
CComBSTR value;
pDoc2->get_cookie(&value);
bStrText = "cookie: ";
bStrText += (BSTR)value;
m_ctrlIE.InsertItem(0, bStrText);
pElementCol.Release();
if (FAILED(pDoc2->get_forms(&pElementCol)))
continue; // AfxMessageBox("IHTMLElementCollection");
long p=0;
if(FAILED(pElementCol->get_length(&p)))
continue;
if(p == 0)
continue;#if 1
_bstr_t bStrWindowName = spBrowser->GetLocationName(); //窗口名称 LogStr("d:\\Logstr.log", "Window: %s", (CHAR *)bStrWindowName); int nPos = m_ctrlIE.InsertItem(0, bStrWindowName);
// spBrowser->AddRef();
// void * pData = spBrowser;
// m_ctrlIE.SetItemData(nPos, (DWORD)(pData));
#endif // AfxMessageBox("1");
for(long i=0;i<=(p-1);i++)
{ V_VT(&id) = VT_I4;
V_I4(&id) = i;
V_VT(&index) = VT_I4;
// V_I4(&index) = 0;
V_I4(&index) = i;
spDispatch.Release();
if(FAILED(pElementCol->item(id,index, &spDispatch)))
continue; // AfxMessageBox("2");
pFormElement.Release();
if(FAILED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement)))
continue;
// AfxMessageBox("IHTMLFormElement");
long q=0;
if(FAILED(pFormElement->get_length(&q)))
continue;
for(long j=0;j<=(q-1);j++)
{
V_VT(&id) = VT_I4;
V_I4(&id) = j;
V_VT(&index) = VT_I4;
V_I4(&index) = 0;
spDispatch.Release();
if(FAILED(pFormElement->item(id,index, &spDispatch)))
continue; pInput.Release();
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputElement,(void**)&pInput)))
{
ProcessInputElement(pInput);
} pElement.Release();
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLElement,(void**)&pElement)))
{
ProcessElement(pElement);
} pInputElement.Release();
if(FAILED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
continue; //AfxMessageBox("IHTMLInputTextElement");
CComBSTR value;
CComBSTR type;
CComBSTR name; pInputElement->get_type(&type); CString strtype(type);
strtype.MakeUpper();
if(strtype.Find("TEXT")!=-1)
{
CComPtr<IHTMLFormElement> pDict;
pInputElement->get_value(&value); pInputElement->get_form(&pDict); CString str(value);
if(!str.IsEmpty())
m_ctrlIE.InsertItem(0, _bstr_t(value)+_bstr_t(" 【strtype:TEXT】")); }
else if(strtype.Find("PASSWORD")!=-1)
{
pInputElement->get_value(&value);
CString str(value);
if(!str.IsEmpty())
m_ctrlIE.InsertItem(0, _bstr_t(value) + _bstr_t(" 【strtype:PASSWORD】"));
}
}
} }
}
http://www.codeguru.com/forum/showthread.php?t=377394