Search code examples
c++visual-c++webbrowser-controlc++buildervcl

Convert Visual C++ code to Borland C++Builder


I have coded a program with Visual C++. But now I must put my code in a program which is coded with Borland C++Builder. I have a WebBrowser item on my form. In Visual C++, I write data to a textbox, get data from the textbox, and click a button on the WebBrowser with this code:

Write Data:

WebBrowser1->Document->GetElementById("okul_kod")->SetAttribute("value", TextBox2->Text);

Get Data:

textBox17->Text = WebBrowser1->Document->GetElementById("kay_cev")->GetAttribute("value");

Button Click:

WebBrowser1->Document->GetElementById("panelden_kayit")->InvokeMember("click");

I tried lots of things, and searched online, but I can't find how to convert this code to Borland C++Builder.

Can you please give me a clue or advice?


Solution

  • In C++Builder 6, its TCppWebBrowser VCL component is a thin wrapper for Internet Explorer's ActiveX control. Its Document property returns an IDispatch that you can use to gain access to IE's raw DOM interfaces directly (whereas Visual C++ appears to have wrapped those interfaces a little more nicely for you).

    Try something like this:

    #include <mshtml.h>
    #include <utilcls.h>
    
    // helpers for interface reference counting
    // could alternatively use TComInterface instead of DelphiInterface
    typedef DelphiInterface<IHTMLDocument3> _di_IHTMLDocument3;
    typedef DelphiInterface<IHTMLElement> _di_IHTMLElement;
    
    ...
    
    // Write Data:
    _di_IHTMLDocument3 doc = CppWebBrowser1->Document;
    _di_IHTMLElement elem;
    OleCheck(doc->getElementById(WideString("okul_kod"), &elem));
    if (elem) OleCheck(elem->setAttribute(WideString("value"), TVariant(Edit2->Text)));
    
    // Get Data:
    _di_IHTMLDocument3 doc = CppWebBrowser1->Document;
    _di_IHTMLElement elem;
    OleCheck(doc->getElementById(WideString("kay_cev"), &elem));
    TVariant value;
    if (elem) OleCheck(elem->getAttribute(WideString("value"), 2, &value));
    Edit17->Text = value;
    
    //Button Click:
    _di_IHTMLDocument3 doc = CppWebBrowser1->Document;
    _di_IHTMLElement elem;
    OleCheck(doc->getElementById(WideString("panelden_kayit"), &elem));
    if (elem) elem->click();