Click here to Skip to main content
12,998,892 members (54,194 online)
Click here to Skip to main content
Add your own
alternative version


49 bookmarked
Posted 26 Dec 2002

How to operate controls in an HTML file, using C++

, 26 Dec 2002
Rate this:
Please Sign up or sign in to vote.
A class can operate on HTML in reusing Web browser.


Microsoft has provide the mshtml.dll with the COM interface. We can use it to manipulate all information in an HTML file as far as we wish. That’s a series of excellent interfaces. In Visual Studio .NET, there is a class named CDhtmlDialog, which facilitates us to use HTML as dialog interface. In this class, there are a lot of basic operations for HTML files. In one of my applications, all dialogs are using HTML dialog. I’ve found we shall need more tools to operate the elements in an HTML file. So I wrote this class, and submit it here.

Information of HTML

In MSDN, there is detailed information on how to operate each kind of HTML element in the file. One of its classes is IHTMLDocument2. Using this interface, we can collect all information of the HTML, adding, deleting of modifying information of HTML file dynamically according to our demand. Especially, using HTML as an interface, it is also very important to retrieve all information of controls located in the file. This wrapper class is mainly on getting or collecting information of these controls.

The elements of HTML are composed of these parts: tag names, inner text, attributes etc. For example:

<select name="select1" value="200">
  <Option value ="4" name="text1">text</Option>
  <Option value ="5" name="text12"></Option>

In this code segment, ‘select’, ‘option’ are tag names. ‘name’, ’value’ are name of attributes. “select1”, “200” are values of attributes. To operate on HTML is to operate these values.

In HTML, lots of controls are using INPUT tag with different types. The following is from MSDN:













GRAMMAR=url | strings


event = script


From this definition, we find that input tag includes the BUTTON, CHECKBOX, FILE, HIDDEN, IMAGE, PASSWORD, RADIO, RESET, SUBMIT and TEXT types. From the command window UI’s view, BUTTON, SUBMIT, IMAGE, and RESET are buttons. FILE, HIDDEN, PASSWORD, and TEXT are edit controls. CHECKBOX and RADIO are compared to their same named controls in Windows. There are no combo boxes and list boxes in the list! These controls are represented as SELECT in HTML.

In standard Windows UI, there is a control to deal with calendar or time. But in HTML, there is no Time or Calendar control. We need to deal with these data types by combining several controls together to complete the task. In HTML, we can assign a custom attribute name for specified HTML tags to indicate a group of controls are representing date/time or calendar.

What does this class do?

In this class, we implement the following features:

  1. Extract all controls from an existing HTML file.
  2. Determine the control type of the current HTML element.
  3. Set a new dictionary to an existing selection (ComboBox).
  4. Clear all options from selection.
  5. Find out a set of tags of HTML with custom attributes “mydate” or “mytime” for specified time/date control.
  6. Find out a set of radio button in a group by defining a ruler with same name in name attribute.

How to insert a set of options to a select (Combobox)

In HTML, Combobox and Listbox have the same tag name ----- Select. The difference between them is that Listbox has a none-zero “size” attribute. The items list in the list box is a set of OPTION tags in HTML. For example:

<select id="i_now_rule_hastwo" name="i_now_rule_hastwo" class="bot_border">
   <option selected>weak</option>

The OPTION tag is the selection in the list control. In the sample code, there are 3 items: strong, mid, and weak are listed in the ComboBox. To add new items to the select element, you must create IHTMLOptionElement and add to the given select. According to MSDN, “Before you can add an element to a collection, you must create it first by using the IHTMLDocument2::createElement method”. Actually, it does not work.

In order to insert a series of OPTIONs into a select, you shall operate HTML as in the following steps:

  1. Empty all options in a select object.
  2. Get the dictionary with given name from dictionary list.
  3. Get number of dictionary items from the dictionary got from dictionary list.
  4. Construct a new OPTION for each item in the dictionary.
  5. Add the items into combobox.

As per previous comment, we cannot use add method of IHTMLSelectElement, then how can I create the elements?

You can not use CreateElement of IHTMLDocument to create IHTMLOptionElement to replace the SELECT options. This object can be created using special interface, IHTMLOptionElementFactory. The sad thing is that there is no direct way to go to the interface from IHTMLDocument or IHTMLElement or IHTMLElementCollection. It must be got from IHTMLWindow2! Where is the IHTMLWindow2? He he, you shall use get_Script of IHTMLDocument2. Damn document! (See the document: Knowledge Base Articles Q249232 HOWTO: Get IHTMLDocument2 from a HWND.)

For the complete code, see source files. The main part of it looks like this:

CComQIPtr<IHTMLWindow2> pWindow;
CComPtr<IDispatch> pMyDisp;
if ( FAILED(m_pDoc->get_Script (&pMyDisp)))
    m_strErr = EHT_CANT_GET_SCRIPT;
    return FALSE;
pWindow = pMyDisp;

IHTMLOptionElementFactory *pOptionFactory= NULL;
if ( FAILED(pWindow->get_Option (&pOptionFactory)))
    dictNode->Release ();
    return FALSE;

// Add each items to selection
for (long l = 0 ; l < lNumItem ; l++ )
    CString strItem ;
    long code = 0;

    // Get current item in the dictionary
    if ( !pDict->GetDictItem (dictNode,l,code,strItem))
        dictNode->Release ();
        m_strErr.Format(EHT_DICT_ITEM, dictName,l);
        return FALSE;

    IHTMLOptionElement *pOption=NULL;
    if (ldefCode == code || strDef == strItem )
        vt_b = VARIANT_TRUE;
    pOptionFactory->create (CComVariant(strItem), CComVariant(code),

    // Add to selection tag
    if ( FAILED ( pSel->add ((IHTMLElement*)pOption,CComVariant(l))))
        dictNode->Release ();
        m_strErr = EHT_ADD_OPTION_ITEM;
        return FALSE;

How to use this class

To use this class is very simple.

Step 1: Create a variable of ChtmlDocment.

ChtmlDocument doc;

Step2 : Set the current IHTMLDocument2 interface pointer to document.


Step3: Call the method provided by the class CDhtmlDocument.

doc.AddDictElement( pElement ,_T("MyDict"),m_pDict);

This class will be using CXMLFile I wrote before. And next, I will publish a CDhtmlControlGroup as the last article for a standard Windows UI manager CControlGroup. This is a part of the detail. The next article will come soon.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Johnson Zhou
Software Developer (Senior)
China China
I'm write program from 1990. My research field is CAG,CAD and Image processing. I select C/C++, ASP, Java, XML as my usaully developing tools. Occasional , write code in Delphi and VB. I'm using Visual C++ from 1996. If you have anything unclear, e-mail to Software Engineering and CAD is my mainly research program.

You also can reach me on msn:

You may also be interested in...


Comments and Discussions

QuestionHow can I operate the elements that don't have ID or name attributes? Pin
rm_pkt26-Feb-07 19:31
memberrm_pkt26-Feb-07 19:31 
AnswerRe: How can I operate the elements that don't have ID or name attributes? Pin
Johnson Zhou28-Feb-07 19:22
memberJohnson Zhou28-Feb-07 19:22 
AnswerRe: How can I operate the elements that don't have ID or name attributes? Pin
rm_pkt6-Mar-07 17:16
memberrm_pkt6-Mar-07 17:16 
GeneralText Selection in HTML Pin
coolsap24-Apr-06 0:40
membercoolsap24-Apr-06 0:40 
GeneralFlash element Pin
KmAshif18-Oct-04 20:36
memberKmAshif18-Oct-04 20:36 
GeneralSimulate Click on Specific Option Pin
neeleshd24-Nov-03 18:24
memberneeleshd24-Nov-03 18:24 
Questionhow to use Resource's Gif file in html file? Pin
samfromcn21-Jan-03 22:53
membersamfromcn21-Jan-03 22:53 
AnswerRe: how to use Resource's Gif file in html file? Pin
Johnson Zhou22-Jan-03 14:30
sussJohnson Zhou22-Jan-03 14:30 
GeneralDoes it hurt to make a whole project Pin
Zhefu Zhang27-Dec-02 7:47
memberZhefu Zhang27-Dec-02 7:47 
GeneralRe: Does it hurt to make a whole project Pin
Neville Franks27-Dec-02 9:56
memberNeville Franks27-Dec-02 9:56 
GeneralRe: Does it hurt to make a whole project Pin
Zhefu Zhang27-Dec-02 11:16
memberZhefu Zhang27-Dec-02 11:16 
GeneralRe: Does it hurt to make a whole project Pin
Neville Franks27-Dec-02 11:29
memberNeville Franks27-Dec-02 11:29 
GeneralRe: Does it hurt to make a whole project Pin
Zhefu Zhang27-Dec-02 11:39
memberZhefu Zhang27-Dec-02 11:39 
GeneralRe: Does it hurt to make a whole project Pin
Neville Franks27-Dec-02 12:07
memberNeville Franks27-Dec-02 12:07 
GeneralRe: Does it hurt to make a whole project Pin
Johnson Zhou27-Dec-02 14:48
memberJohnson Zhou27-Dec-02 14:48 
GeneralRe: Does it hurt to make a whole project Pin
Neville Franks28-Dec-02 10:35
memberNeville Franks28-Dec-02 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170622.1 | Last Updated 27 Dec 2002
Article Copyright 2002 by Johnson Zhou
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid