How to: Access the properties of the ActiveX control in the document via HTML Document Object Model
Information about this article is applied to:
Microsoft Internet Explorer (Programming) Version 4.0, 4.01, 4.01 SP1, 4.01 SP2, 5, 5.01, 5.5, 6.0
summary
9CBS Document Center Article
Little experience with MSHTML 111222 (original)
Explain how to access the elements of the web page in the HTML document object model, content. However, sometimes developers actually need access to the properties, methods, and events of the ActiveX control in the web page. For example, you need to modify / get MediaPlayer's media source after loading, and control the playback of MediaPlayer.
More information
For the interface of the ActiveX control, we need to access the document object model. Get a variety of methods for the document interface, such as Chtmlview :: gethmldocument, iWebBrowser2 :: get_document, htmlwindow2 :: get_document, etc., see the 111222 documentation. Here I use a function getDhtmldocument to represent the function of this interface. You can implement this function yourself.
Typically, we give controls a unique ID in the document for access. First we have to find this element in the document, use the ID as a parameter.
Sample code: (Refer to the source code of MFC7.0)
// Document Modified At: Sunday, August 18, 2002 11:04:50 AM, by user: jiangsheng, from computer: kfb // Access element interface through the name
HRESULT CDHtmlDialog :: GetElement (LPCTSTR szElementId, IHTMLElement ** pphtmlElement) {return GetElementInterface (szElementId, __uuidof (IHTMLElement), (void **) pphtmlElement);} // the name of the access interface via auxiliary elements function that returns the specified type interface HRESULT CDHtmlDialog :: GetElementInterface (LPCTSTR szElementId, REFIID riid, void ** ppvObj) {HRESULT hr = E_NOINTERFACE; spdispElem CComPtr
HRESULT CDHtmlDialog :: GetElement (LPCTSTR szElementId, IDispatch ** ppdisp, BOOL * pbCollection / * = NULL * /) {CComPtr
You can not directly use the control interface to access IHtmlObjectElement HRESULT CDHtmlDialog :: GetControlDispatch (LPCTSTR szId, IDispatch ** ppdisp) {HRESULT hr = S_OK; CComPtr
{; GetControlDispatch (szId, & spdispElem); return GetControlProperty (spdispElem, dispid); CComPtr
The following is the generated ColedisPatchDriver derived class code example: cstring csomeObject :: getText () {CString Result; InvokeHelper (0x18, dispatch_propertyget, vt_bstr, (void *) & results, null; return;}
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 VTS_BSTR; InvokeHelper (0x36, DISPATCH_METHOD, VT_DISPATCH, (void *) & result, parms, & type, name, Namespaceuri; Return Result;
Another benefit is obvious, you can throw trouble work (find Dispid and call Invoke) to the class wizard, you only need to use the class wizard to automatically generate the class.
If you have to handle the control of the control, you should refer to this article in MSDN.
Handling HTML Element Events
The method of capturing an event of an ActiveX control is the same as in the article, just that the interface you need to capture the event object should be a control interface, not an element interface. The method for obtaining the IDSPATCH interface of the control has been said.
By the way, the mistakes that are easy to commit in HTML are mixed with different types of interfaces, such as
Htmlelement * pelem = null; if (palelem-> item (name, name, (lpdispatch *) & pelem) == s_ok) ... Note, Although Microsoft's document says Ihtmlelement is derived from Idispatch (Inherits from IDispatch) But this does not mean that some ways to return iDispatch are the derived interface. The above code is to commit this error, and the returned interface is directly used as the IHTMLELEMENT interface, it may be wrong. The correct access method should be the QueryInterface interface that calls the returned iDispatch to obtain the interface pointer of the specified type. See CDHTMLDIALOG :: getElement code.
reference
To get more, click the following connection to view the article in the 9CBS document library.
Little experience with MSHTML 111222 (original)
How to: get the top IWebBrowser2 interface in the ActiveX control (translation)
Use web page input data in the dialog box (Jiangsheng original)
Click the following connection to view the article in the MSDN document library
Handling HTML Element Events (English Site)
To develop a Web-based Internet Explorer solution, visit the following site:
http://msdn.microsoft.com/workshop/entry.asp
(English site)
http://msdn.microsoft.com/ie/
(English site)
http://support.microsoft.com/highlights/iep.asp?fr=0&sd=msdn (English site)
Additional query keywords: MFC Internet Explorer mshtml htmlelement htmlelementCollection htmldocument2 htmlobjectElement
Keywords: 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
Last updated: January 21, 2005