Il sito dedicato all'informatica ideato da Iasparra Francesco
Il seguente esempio e' stato sviluppato in ambiente Borland C++ versione 6 su sistema operativo Windows, quindi gli oggetti utilizzati sono disponibili nelle librerie della piattaforma Borland.
File, relativo al progetto:
#include <vcl.h>
#pragma hdrstop
USEFORM("Unit1.cpp", Form1);
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
Application->Initialize();
Application->CreateForm(__classid(TForm1), &Form1);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
catch (...)
{
try
{
throw Exception("");
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
}
return 0;
}
Il codice appena visto non fa altro che avviare la nostra applicazione creando la finestra.
L'header file del nostro esempio e' il seguente:
#ifndef Unit1H
#define Unit1H
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include "SHDocVw_OCX.h"
#include <Buttons.hpp>
#include <OleCtrls.hpp>
#include <AppEvnts.hpp>
class TForm1 : public TForm
{
__published: // IDE-managed Components
TCppWebBrowser *CppWebBrowser1;
TBitBtn *BitBtn1;
TApplicationEvents *AppEvents;
TEdit *Edit1;
TEdit *Edit2;
void __fastcall FormShow(TObject *Sender);
void __fastcall FormPaint(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
extern PACKAGE TForm1 *Form1;
#endif
La nostra finestra principale:
#include <vcl.h>
#include <Mshtml.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma link "SHDocVw_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm1::FormShow(TObject *Sender)
{
CppWebBrowser1->Navigate(WideString("prova.html").c_bstr());
}
void __fastcall TForm1::FormPaint(TObject *Sender)
{
while(CppWebBrowser1->Busy) Application->ProcessMessages();
IHTMLDocument2 *HTMLDoc = NULL;
if(SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2,(LPVOID*)&HTMLDoc))){
IHTMLElementCollection *pAll = NULL;
if(SUCCEEDED(HTMLDoc->get_all(&pAll))){
IDispatch *pDisp = NULL;
TVariant index=0;
TVariant name = "Documento";
if(SUCCEEDED(pAll->item(name, index, &pDisp))){
if(pDisp){
IHTMLInputElement *pInput = NULL;
pDisp->QueryInterface(IID_IHTMLInputElement, (LPVOID*)&pInput);
pDisp->Release();
if(pInput){
BSTR mybuffer;
pInput->get_value(&mybuffer);
Edit1->Text=AnsiString(mybuffer);
pInput->put_value(WideString("Vocabolario"));
ShowMessage("Il contetnuto del campo cambia\nda Libro 1 a Vocabolario");
}
pInput->Release();
}
}
name = "Chiave";
if(SUCCEEDED(pAll->item(name, index, &pDisp))){
if(pDisp){
IHTMLInputElement *pInput = NULL;
pDisp->QueryInterface(IID_IHTMLInputElement, (LPVOID*)&pInput);
pDisp->Release();
if(pInput){
BSTR mybuffer;
pInput->get_value(&mybuffer);
Edit2->Text=AnsiString(mybuffer);
ShowMessage("Lettura del campo nascosto hidden");
}
pInput->Release();
}
}
}
pAll->Release();
}
IHTMLElementCollection *HTMLForms = NULL;
if(SUCCEEDED(HTMLDoc->get_forms(&HTMLForms)) && HTMLForms){
TVariant vName = "Richiesta";
IDispatch *pDisp = NULL;
TVariant vIndex = 0;
if(SUCCEEDED(HTMLForms->item(vName, vIndex, &pDisp)) && pDisp){
IHTMLFormElement *HTMLForm = NULL;
if(SUCCEEDED(pDisp->QueryInterface(IID_IHTMLFormElement,(LPVOID*)&HTMLForm)) && HTMLForm){
HTMLForm->submit();
HTMLForm->Release();
}
pDisp->Release();
}
HTMLForms->Release();
}
HTMLDoc->Release();
}
L'oggetto CppWebBrowser1 utilizza l'oggetto COM di Internet Explorer per la navigazione delle pagine Web, attraverso il metodo Navigate(WideString("prova.html").c_bstr()); viene caricata la pagina di esempio "prova.html".
Supponiamo che all'interno della pagina html vi sia il seguente codice:
<form name="richiesta" action="prova2.html" method="post"> <input name="Documento" type="text" value="libro 1"> <input type="hidden" name="Chiave" value="12345"> <input name="Conferma" type="submit" value="ok1" onClick="javascript:return risposta();" > </form>
La nostra applicazione effettua le seguenti operazioni:
- sostituisce il contenuto della variabile Documento da "libro 1" a "Vocabolario"
- legge il contenuto della variabile nascosta "Chiave"
- effettua il submit della pagina.










