How to: access the properties of ActiveX controls in the document through the HTML Document Object Model.

Source: Internet
Author: User
How to: access the properties of ActiveX controls in a document through the HTML Document Object Model

 

ThisArticleInformation applied:

    • Microsoft Internet Explorer (programming) Version 4.0, 4.01, 4.01 SP1, 4.01 sp2, 5, 5.01, 5.5, 6.0

Summary

Csdn document center article

Use mshtml's experience 111222 (original)

Describes how to access the elements and content of a webpage in the HTML Document Object Model. However, sometimes developers actually need to access the properties, methods, and events of ActiveX controls on webpages. For example, after loading a webpage, You need to modify/obtain the media source of the mediaplayer and control the playback of the mediaplayer.

More information

To obtain the ActiveX control interface, we need to access the Document Object Model. There are many methods to obtain the document interface, such as chtmlview: gethtmldocument, iwebbrowser2: get_document, ihtmlwindow2: get_document, and so on. For more information, see the documentation in section 111222. Here, I directly use the getdhtmldocument function to obtain the function of this interface. You can implement this function by yourself.

Generally, we give the control a unique ID in the document for easy access. First, we need to find this element in the document and use ID as the parameter.

ExampleCode: (Refer to the mfc7.0Source code)

// Document modified at: Sunday, August 18,200 2 11:04:50, by user: jiangsheng, from computer: KFB
// Access the element interface by name

Hresult cdhtmldialog: getelement (lpctstr szelementid, ihtmlelement ** pphtmlelement)
{
ReturnGetelementinterface (szelementid, _ uuidof (ihtmlelement ),(Void**) Pphtmlelement );
}
// Access the auxiliary functions of the element interface by name, used to return the interface of the specified type
Hresult cdhtmldialog: getelementinterface (lpctstr szelementid, refiid riid,Void** Ppvobj)
{
Hresult hR = e_nointerface;
* Ppvobj = NULL;
Ccomptr <idispatch> spdispelem;

HR = getelement (szelementid, & spdispelem );

If (Spdispelem)
HR = spdispelem-> QueryInterface (riid, ppvobj );
Return HR;
}
// An auxiliary function that accesses the element interface by name. It is used to access the element interface of the specified ID. If pbcollection returns true, an ihtmlelementcollection collection is returned, indicating that there are more than one webpage element with the specified ID/name.
Hresult cdhtmldialog: getelement (lpctstr szelementid, idispatch ** ppdisp,
Bool * pbcollection /* = NULL */ )
{
Ccomptr <ihtmlelementcollection> sphtmlall;
Ccomptr <ihtmlelementcollection> sphtmlcoll;
Ccomptr <idispatch> spdispelem;
Ccomvariant varname;
Ccomvariant varindex;
Hresult hR = s_ OK;
Ccomptr <ihtmldocument2> sphtmldoc;
Uses_conversion;

* Ppdisp = NULL;

If(Pbcollection)
* Pbcollection = false;

HR = getdhtmldocument (& sphtmldoc );
If(Sphtmldoc = NULL)
ReturnHR;

Varname. Vt = vt_bstr;
Varname. bstrval = t2bstr (szelementid );
If(! Varname. bstrval)
{
HR = e_outofmemory;
GotoError;
}

HR = sphtmldoc-> get_all (& sphtmlall );
If(Sphtmlall = NULL)
GotoError;
HR = sphtmlall-> item (varname, varindex, & spdispelem );
If(Spdispelem = NULL)
{
HR = e_nointerface;
GotoError;
}

spdispelem-> QueryInterface (_ uuidof (ihtmlelementcollection), ( void **) & sphtmlcoll );
If (sphtmlcoll)
{< br> If (pbcollection)
* pbcollection = true;
# ifdef _ debug
else
{< br> trace (tracehtml, 0, "Warning: duplicate IDs or names. /n ");
atlassert (false);
}< br> # endif

}
Error:
If(Succeeded (HR ))
{
* Ppdisp = spdispelem;
If(Spdispelem)
(* Ppdisp)-> addref ();
}
ReturnHR;
}
Then we need to access the attributes, methods, and events of the object. This requires the ihtmlelement interface to obtain the object interface, which is accessed through ihtmlobjectelement.
// Obtain the ActiveX control interface. Note that the ActiveX control interface and the html object element interface are not the same interface. You cannot directly use the ihtmlobjectelement interface to access the control.
Hresult cdhtmldialog: getcontroldispatch (lpctstr szid, idispatch ** ppdisp)
{
Hresult hR = s_ OK;
Ccomptr <idispatch> spdispelem;

HR = getelement (szid, & spdispelem );

If (spdispelem)
{< br> ccomptr sphtmlobj;

hR = spdispelem. queryInterface (& sphtmlobj);
If (sphtmlobj)
{< br> spdispelem. release ();
hR = sphtmlobj-> get_object (ppdisp);
}< BR >}< br> return hr;
}< br> with the active control interface, the following work is much simpler. For example, if you want to access the non-parameter attribute of the specified name of the control, you only need to call the getidsofnames of the idispatch interface to obtain the dispid of the attribute, and then call the invoke method to obtain the attribute
// obtain the control attribute, access by name
variant cdhtmldialog: getcontrolproperty (lpctstr szid, lpctstr szpropname)
{< br> ccomvariant varempty;
ccomptr spdispelem;

Getcontroldispatch (szid, & spdispelem );
If(! Spdispelem)
ReturnVarempty;

Dispid;
Uses_conversion;
Lpolestr ppropname = (lpolestr) t2cole (szpropname );
Hresult hR = spdispelem-> getidsofnames (iid_null, & ppropname, 1, locale_user_default, & dispid );
If (Succeeded (HR ))
Return Getcontrolproperty (spdispelem, dispid );
Return Varempty;
}
// Set control properties and access by name
Void Cdhtmldialog: setcontrolproperty (maid, maid, variant * pvar)
{
Ccomptr <idispatch> spdispelem;
Getcontroldispatch (szelementid, & spdispelem );
If (! Spdispelem)
Return ;
Dispid;
Uses_conversion;
Lpolestr ppropname = (lpolestr) t2cole (szpropname );
Hresult hR = spdispelem-> getidsofnames (iid_null, & ppropname, 1, locale_user_default, & dispid );
If (Succeeded (HR ))
Setcontrolproperty (spdispelem, dispid, pvar );
}
// Obtain the auxiliary function of the control property and access it through dispid
Variant cdhtmldialog: getcontrolproperty (lpctstr szid, dispid)
{
Ccomptr <idispatch> spdispelem;

Getcontroldispatch (szid, & spdispelem );
ReturnGetcontrolproperty (spdispelem, dispid );
}
// Sets the auxiliary function of the control property and accesses it through dispid.
VoidCdhtmldialog: setcontrolproperty (lpctstr szelementid, dispid, variant * pvar)
{
Ccomptr <idispatch> spdispelem;
Getcontroldispatch (szelementid, & spdispelem );

Setcontrolproperty (spdispelem, dispid, pvar );
}

// implementation function for obtaining control attributes
variant cdhtmldialog: getcontrolproperty (idispatch * pdispcontrol, dispid)
{< br> variant varret;
varret. vt = vt_empty;
If (pdispcontrol)
{< br> dispparams dispparamsnoargs = {null, null, 0, 0 };
pdispcontrol-> invoke (dispid, iid_null, locale_user_default,
dispatch_propertyget, & dispparamsnoargs, & varret, null, null );
}< br> return varret;
}

// control attribute implementation function
void cdhtmldialog: setcontrolproperty (idispatch * pdispcontrol, dispid, variant * pvar)
{< br> If (pdispcontrol! = NULL)
{< br> dispparams = {null, null, 1, 1 };
dispparams. rgvarg = pvar;
dispid dispidput = dispid_propertyput;
dispparams. rgdispidnamedargs = & dispidput;

Pdispcontrol-> invoke (dispid, iid_null,
Locale_user_default, dispatch_propertyput,
& Dispparams, null );
}
}
In fact, this method is less efficient because getidsofnames is called every time you access it, while getidsofnames is a very slow call. To optimizeProgramEfficiency: You can cache the obtained name-> dispid ing, but the recommended method is to use Class Wizard) add a class (New Class-> from a Type Library) to import the control to the project, and access the properties and methods through the coledispatchdriver derived class automatically generated by the Class Wizard. This method directly uses the dispid generated in the Type Library to access attributes, methods, and events, so the speed is much faster than the previous method of calling getidsofnames each time.

The following is a code example of the generated coledispatchdriver derived class:

Cstring csomeobject: gettext ()
{
Cstring result;
Invokehelper (0x18, dispatch_propertyget, vt_bstr, (void *) & result, null );
Return result;
}

Void csomeobject: settext (lpctstr lpsznewvalue)
{
Static byte parms [] =
Vts_bstr;
Invokehelper (0x18, dispatch_propertyput, vt_empty, null, parms,
Lpsznewvalue );
}

Lpdispatch csomeobject: createnode (const variant & type, lpctstr name, lpctstr namespaceuri)
{
Lpdispatch result;
Static byte parms [] =
Vts_variant vts_bstr;
Invokehelper (0x36, dispatch_method, vt_dispatch, (void *) & result, parms,
& Type, name, namespaceuri );
Return result;
}

Another advantage is that you can throw the troublesome work (look for dispid and call invoke) to the Class Wizard. You only need to use the class automatically generated by the Class Wizard.

If you want to handle control events, refer to this Article in msdn.

Handling HTML element events

The method for capturing ActiveX control events is basically the same as in the article, but the interface you need to capture event objects should be the control interface, not the element interface. The method for obtaining the control's idispatch interface has been mentioned earlier.

By the way, in HTML programming, the common mistake is to mix different types of interfaces, such

Ihtmlelement * pelem = NULL;
If (pallelem-> item (name, name, (lpdispatch *) & pelem) = s_ OK)
......
Note: although the Microsoft documentation says that ihtmlelement is derived from idispatch (inherits from idispatch), this does not mean that some methods that return idispatch return derived interfaces, the above Code makes this error. If you use the returned interface directly as the ihtmlelement interface, an error may occur. The correct access method should be to call the QueryInterface interface of the returned idispatch to obtain the interface pointer of the specified type. See the code of cdhtmldialog: getelement.

Reference

For more information, click the following link to view the articles in the csdn document library.

Use mshtml's experience 111222 (original)

How to: Obtain the top-level iwebbrowser2 interface in ActiveX ControlJiangsheng)

In the dialog box, use the webpage input data (jiangsheng original)

Click the connection below to view the article in The msdn Document Library

Handling HTML element events(English site)

For more information about developing web-based Internet Explorer solutions, visit the following sites:

Http://msdn.microsoft.com/workshop/entry.asp)

Http://msdn.microsoft.com/ie)

Http://support.microsoft.com/highlights/iep.asp? Fr = 0 & SD = msdn (English site)

Additional query keywords: MFC Internet Explorer mshtml ihtmlelement ihtmlelementcollection ihtmldocument2 ihtmlobjectelement

Keyword: kbactivex kbctrl kbie kbie400 kbgrpdsinet kbie500 kbdsupport kbie501 kbie550
Article type: kbhowto
Technology: kbiesearch kbauddeveloper kbsdkiesearch kbie500search kbsdkie400 kbsdkie401 kbsdkie401sp1 kbsdkie401sp2 kbsdkie500 kbsdkie501 kbsdkie550 kbie550search

 

 

From: http://blog.csdn.net/jiangsheng/article/details/3788

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.