有如下HTML,如何用程序触发点击按钮呢?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<input type="button" id="btnTest" value="Test" onclick="alert('点击了我');"/>
</BODY>
</HTML>
首先看JS的写法
var oEvent = document.createEventObject();
document.getElementById("btnTest").fireEvent("onclick", oEvent);
把这段在翻译到C++里面就是:
IHTMLDocument * pDoc = (IHTMLDocument *)GetHtmlDocument();
CComQIPtr<IHTMLDocument3> pDoc3(pDoc);
CComQIPtr<IHTMLDocument4> pDoc4(pDoc);
if( pDoc3 && pDoc4 )
{
IHTMLElement * pElem = NULL;
pDoc3->getElementById(_bstr_t(_T("btnTest")), &pElem);
CComQIPtr<IHTMLElement3> pElem3(pElem);
if( pElem3 )
{
CComPtr<IHTMLEventObj> pEvent;
pDoc4->createEventObject( NULL, &pEvent);
if( pEvent )
{
_variant_t vEvent(pEvent);
VARIANT_BOOL vbSuccess = VARIANT_FALSE;
pElem3->fireEvent( _bstr_t(_T("onclick"))
, &vEvent
, &vbSuccess
);
}
}
}