<HTML>
<HEAD>
<SCRIPT>
function firebuttonclick()
{
var neweventobj = document.createEventObject();
neweventobj.clientX = 100;
neweventobj.clientY = 100;
button2.fireEvent("onclick", neweventobj);
}
function handleclick()
{
alert("Received " + window.event.type + " event on " + window.event.srcElement.id + " with clientX: " + window.event.clientX + " clientY: " + window.event.clientY);
}
</SCRIPT>
</HEAD>
<BODY bgcolor="#000000" text="#008080">
<INPUT type=button value="Button 1" name=button1 id=button1 onclick=firebuttonclick()><b> - Click here to fire a click event on the second button below. </b>
<BR><BR>
<INPUT type=button value="Button 2" name=button2 id=button2 onclick=handleclick()><b> - Or click this directly.
</BODY>
</HTML>
=========================
#include <atlbase.h>
#define CHECKPTR(ptr) if ((ptr) == NULL) goto cleanup;
void CMyView::OnFireEvent()
{
HRESULT hr = S_OK;
BSTR eventName = NULL;
VARIANT name, index, eventobj;
VARIANT_BOOL vBool;
CComPtr<IDispatch> pDisp = NULL;
CComPtr<IDispatch> pEltDisp = NULL;
CComPtr<IHTMLElementCollection> pEltColl = NULL;
CComPtr<IHTMLEventObj> pEvent = NULL;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComQIPtr<IHTMLDocument4, &IID_IHTMLDocument4> pDoc4;
CComQIPtr<IHTMLEventObj2, &IID_IHTMLEventObj2> pEvent2;
CComQIPtr<IHTMLElement3, &IID_IHTMLElement3> pElt3;
CComQIPtr<IDispatch, &IID_IDispatch> pEventDisp;
// m_webBrowser is the IWebBrowser2 ptr of
// the Web browser control you host.
CHECKPTR(pDisp = m_webBrowser.GetDocument());
CHECKPTR(pDoc2 = pDisp);
//Get the element you want to fire the event on.
hr = pDoc2->get_all(&pEltColl);
if (FAILED(hr) || !pEltColl)
goto cleanup;
VariantInit(&name);
V_VT(&name) = VT_BSTR;
V_BSTR(&name) = SysAllocString(L"button2");
VariantInit(&index);
V_VT(&index) = VT_I2;
V_I2(&index) = 0;
// We are assuming there's only one element with that name on the page,
// else you must expect a collection instead of a single element.
hr = pEltColl->item(name, index, &pEltDisp);
if (FAILED(hr) || !pEltDisp)
goto cleanup;
CHECKPTR(pElt3 = pEltDisp);
// Create the event object and populate it.
CHECKPTR(pDoc4 = pDoc2);
hr = pDoc4->createEventObject(NULL, &pEvent);
if (FAILED(hr) || !pEvent)
goto cleanup;
CHECKPTR(pEvent2 = pEvent);
CHECKPTR(pEventDisp = pEvent2);
pEvent2->put_clientX(100);
pEvent2->put_clientY(100);
// Fire the event.
eventName = SysAllocString(L"onclick");
VariantInit(&eventobj);
V_VT(&eventobj) = VT_DISPATCH;
V_DISPATCH(&eventobj) = pEventDisp;
hr = pElt3->fireEvent(eventName, &eventobj, &vBool);
cleanup:
if (V_BSTR(&name))
SysFreeString(V_BSTR(&name));
if (eventName)
SysFreeString(eventName);
}
Tidak ada komentar:
Posting Komentar