Beispiel #1
0
HRESULT STDMETHODCALLTYPE CElementEvents::Invoke(DISPID dispidMember,
                                                REFIID riid,
                                                LCID lcid,
                                                WORD wFlags,
                                                DISPPARAMS* pdispparams,
                                                VARIANT* pvarResult,
                                                EXCEPINFO* pexcepinfo,
                                                UINT* puArgErr)
{
    UNREFERENCED_PARAMETER(puArgErr);
    UNREFERENCED_PARAMETER(pexcepinfo);
    UNREFERENCED_PARAMETER(pvarResult);
    UNREFERENCED_PARAMETER(riid);
    UNREFERENCED_PARAMETER(lcid);
    UNREFERENCED_PARAMETER(wFlags);
    // try to get IHTMLEventObj
    IDispatch* param1=NULL; 
    if (pdispparams->cArgs==1 && (pdispparams->rgvarg[0].vt==VT_DISPATCH))
        param1=pdispparams->rgvarg[0].pdispVal;

    IHTMLEventObj* pEvtObj=NULL;
    if (param1) 
        param1->QueryInterface(IID_IHTMLEventObj, (void**)&pEvtObj);


    // see HTMLElementEvents2 Dispinterface for available values (include mshtmdid.h)

    // disable context menu by default (but for INPUT)
    switch (dispidMember)
    {
    case DISPID_HTMLELEMENTEVENTS2_ONCONTEXTMENU:
        if (this->bEnableContextMenu)
            break;
        // else
        {
            IHTMLElement* pHTMLElement;
            BSTR String;
            // get element source of event
            HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement);
            if (SUCCEEDED(hr) && pHTMLElement)
            {
                // get element name
                pHTMLElement->get_tagName(&String);
                if (String==NULL)
                    String=SysAllocString(L"");

                // apply for all but input
                if (wcsicmp(String,L"INPUT")!=0)
                {
                    VARIANT v;
                    v.vt=VT_BOOL;
                    v.boolVal=VARIANT_FALSE;
                    pEvtObj->put_returnValue(v);
                }

                // Free memory and release objects
                SysFreeString(String);
                pHTMLElement->Release();
            }
        }
        break;

    // disable selection but for input
    case DISPID_HTMLELEMENTEVENTS2_ONSELECTSTART:
        if (this->bEnableSelection)
            break;
        // else
        {
            IHTMLElement* pHTMLElement;
            BSTR String;
            // get element source of event
            HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement);
            if (SUCCEEDED(hr) && pHTMLElement)
            {
                // get element name
                pHTMLElement->get_tagName(&String);
                if (String==NULL)
                    String=SysAllocString(L"");

                // apply for all but input
                if (wcsicmp(String,L"INPUT")!=0)
                {
                    VARIANT v;
                    v.vt=VT_BOOL;
                    v.boolVal=VARIANT_FALSE;
                    pEvtObj->put_returnValue(v);
                }

                // Free memory and release objects
                SysFreeString(String);
                pHTMLElement->Release();
            }
        }
        break;
    // disable selection but for input
    case DISPID_HTMLELEMENTEVENTS2_ONMOUSEOVER:
        if (this->bEnableSelection || (this->pCmdTarget==NULL))
            break;
        // else
        {
            VARIANT var;
            var.vt=VT_BOOL;
            IHTMLElement* pHTMLElement;
            BSTR String;

            // get element source of event
            HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement);
            if (SUCCEEDED(hr) && pHTMLElement)
            {
                // get element name
                pHTMLElement->get_tagName(&String);
                if (String==NULL)
                    String=SysAllocString(L"");

                // apply for all but input, link and img
                if ( (wcsicmp(String,L"INPUT")!=0)
                    && (wcsicmp(String,L"A")!=0)
                    && (wcsicmp(String,L"IMG")!=0)
                    )
                {
                    // stop cursor automatic mode
                    var.boolVal = VARIANT_TRUE;
                    this->pCmdTarget->Exec(&CGID_MSHTML, 
                                            IDM_OVERRIDE_CURSOR,
                                            OLECMDEXECOPT_DODEFAULT,
                                            &var,
                                            NULL);
                    // Set cursor to arrow
                    this->hCursorOld=::SetCursor(this->hCursorArrow);
                }

                // Free memory and release objects
                SysFreeString(String);
                pHTMLElement->Release();
            }
        }
        break;
    case DISPID_HTMLELEMENTEVENTS2_ONMOUSEOUT:
        if (this->bEnableSelection || (this->pCmdTarget==NULL))
            break;
        // else
        {
            VARIANT var;
            var.vt=VT_BOOL;
            IHTMLElement* pHTMLElement;
            BSTR String;

            // get element source of event
            HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement);
            if (SUCCEEDED(hr) && pHTMLElement)
            {
                // get element name
                pHTMLElement->get_tagName(&String);
                if (String==NULL)
                    String=SysAllocString(L"");

                // apply for all but input, link and img
                if ( (wcsicmp(String,L"INPUT")!=0)
                    && (wcsicmp(String,L"A")!=0)
                    && (wcsicmp(String,L"IMG")!=0)
                    )
                {
                    // restore cursor automatic mode
                    var.boolVal = VARIANT_FALSE;
                    ::SetCursor(this->hCursorOld);
                    this->pCmdTarget->Exec(&CGID_MSHTML, 
                                            IDM_OVERRIDE_CURSOR, 
                                            OLECMDEXECOPT_DODEFAULT, 
                                            &var, 
                                            NULL);
                }
                // Free memory and release objects
                SysFreeString(String);
                pHTMLElement->Release();
            }
        }
        break;
    case DISPID_HTMLELEMENTEVENTS2_ONDRAGSTART:
        if (this->bEnableSelection)
            break;
        {
            VARIANT v;
            v.vt=VT_BOOL;
            v.boolVal=VARIANT_FALSE;
            pEvtObj->put_returnValue(v);
        }
        break;
    }


    // call callback
    if (this->ElementEventsCallBack)
    {
        IHTMLElement* pHTMLElement;
        BSTR String;
        // get element source of event
        HRESULT hr=pEvtObj->get_srcElement(&pHTMLElement);
        if (SUCCEEDED(hr) && pHTMLElement)
        {
            // get element id
            pHTMLElement->get_id(&String);
            if (String==NULL)
                String=SysAllocString(L"");

            // call callback
            this->ElementEventsCallBack(dispidMember,String,this->ElementEventsCallBackUserParam);

            // Free memory and release objects
            SysFreeString(String);
            pHTMLElement->Release();
        }
    }

    if (this->ElementEventsCallBackEx)
    {
        this->ElementEventsCallBackEx(dispidMember,pEvtObj,this->ElementEventsCallBackExUserParam);
    }

    // release IHTMLEventObj
    if (pEvtObj)
        pEvtObj->Release();

    return S_OK;
}
Beispiel #2
0
IHTMLElement* FindElement(IHTMLDocument2* pDoc2, LPCTSTR elementId, LPCTSTR elementName)
{
	USES_CONVERSION;
	HRESULT hr;

	//Enumerate the HTML elements
    IHTMLElementCollection* pColl = NULL;
    hr = pDoc2->get_all( &pColl );
    if (hr == S_OK && pColl != NULL)
    {
		LONG celem;
        pColl->get_length( &celem );

		 //Loop through each elment
        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 );//Get an element

			if ( hr == S_OK )
            {
                IHTMLElement* pElem;
				//Ask for an HTMLElemnt interface
                hr = pDisp->QueryInterface(IID_IHTMLElement, (void **)&pElem);

				if ( hr == S_OK )
                {
					if(elementId != NULL) //find element by Id
					{
						BSTR bstr;
						//Get the id of the element
						pElem->get_id(&bstr);
	                   					
						LPCTSTR id = OLE2T(bstr);

						if(_tcscmp(id, elementId))
						{
							return pElem;						
						}
					}
					else if(elementName != NULL) //find element by Name
					{
						IHTMLInputButtonElement* pButton;
                        hr = pDisp->QueryInterface(IID_IHTMLInputButtonElement,(void **)&pButton);
                        if ( hr == S_OK )
                        {
							BSTR bstr;
							//Get the name of the element
							pButton->get_name(&bstr);
		                   					
							LPCTSTR name = OLE2T(bstr);

							if(_tcscmp(name, elementName))
							{
								return pElem;						
							}
						}
					}
					
					pElem->Release();
				}
			}			
		}

		pColl->Release();
	}

	return NULL;
}