|
OnDocumentComplete: This event fire when the document has reached ReadyState _Complete
Write an OnDocumentComplete event handler to take a specific action when a frame or document is fully loaded into your browser
.For a
document without frames, this event occurs once when the document finishes
loading.
On a document containing multiple frames, this event occurs once for
each frame.
When the multiple-frame document finishes loading, the Web browser
fires the event one final time.
In simple words, here is the place to take action when you need to be sure the document is fully loaded. like when you want to hide your Progressbar when the document is loaded or when you want to hande Auto login form like in the folowing examples.
procedure TbrowserFrm.EmbeddedWB1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
Progressbar1.Visible:=false;
end;
//Create AutoLogin for several Website Like: mail.yahoo, kompas.co.id, etc :
//by yhoezt@yahoo.com
//We need two form, : form login and form web browser(Twebbrowser)
//Here the code program :
//Form AutoLogin:
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, StdCtrls, ExtCtrls, Wininet, Registry,
ShellAPI,DDEMan,MSHTML, FileCtrl,ShlObj,SHDocVw,Unit_con8_b;
type
TAutoLogin = class(TForm)
Panel1: TPanel;
GroupBox1: TGroupBox;
Panel2: TPanel;
Label1: TLabel;
UserTxt: TEdit;
Label2: TLabel;
Panel4: TPanel;
LogBtn: TSpeedButton;
Panel5: TPanel;
Panel6: TPanel;
HistoryBtn: TSpeedButton;
Panel8: TPanel;
CloseBtn: TSpeedButton;
Panel3: TPanel;
GoURLBtn: TSpeedButton;
OpenUrlText: TComboBox;
PassTxt: TEdit;
procedure GoURLBtnClick(Sender: TObject);
procedure HistoryBtnClick(Sender: TObject);
procedure DefaultBtnClick(Sender: TObject);
procedure CloseBtnClick(Sender: TObject);
procedure LogBtnClick(Sender: TObject);
private
procedure Url_Open;
procedure Login_Web;
{ Private declarations }
public
{ Public declarations }
end;
var
AutoLogin: TAutoLogin;
ValueStr: string;
LinkOpened: integer;
DDE: TDdeClientConv;
KeyName: string;
Reg: TRegistry;
implementation
{$R *.dfm}
function IsWin95: boolean;
var OS: TOSVersionInfo;
begin
ZeroMemory(@OS,SizeOf(OS));
OS.dwOSVersionInfoSize:= SizeOf(OS);
GetVersionEx(OS);
Result:= (OS.dwMajorVersion>=4) and (OS.dwMinorVersion=0) and
(OS.dwPlatformId= VER_PLATFORM_WIN32_WINDOWS);
end;
function FuncAvail(_dllname, _funcname: string;var _p: pointer): boolean;
var _lib: tHandle;
begin
Result := false;
if LoadLibrary(PChar(_dllname)) = 0 then exit;
_lib := GetModuleHandle(PChar(_dllname)) ;
if _lib <> 0 then
begin
_p := GetProcAddress(_lib, PChar(_funcname)) ;
if _p <> NIL then Result := true;
end;
end;
procedure ConnectedToInternet;
var InetIsOffline : function(dwFlags: DWORD):BOOL; stdcall;
Lib: string;
begin
if(IsWin95) then Lib:= 'SHELL32.DLL'
else Lib:= 'URL.DLL';
if FuncAvail(Lib, 'InetIsOffline',@InetIsOffline) then
begin
if (InetIsOffLine(0)) then
ShowMessage('Not Connected')
else
ShowMessage('Connected!');
end;
end;
function IsUrlValid(Url: string): boolean;
var hINet: HINTERNET;
hConnect: HINTERNET;
infoBuffer: array[0..512] of Char;
dummy: DWORD;
bufLen: DWORD;
okay: LongBool;
reply: string;
begin
hINet:= InternetOpen(PChar(Application.Title),
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY,nil,nil,0);
hConnect:= InternetOpenUrl(hINet,PChar(Url),nil,0,INTERNET_FLAG_NO_UI,0);
if not Assigned(hConnect) then
Result:= True //seharusnya False , nanti diganti
else
begin
dummy:= 0;
bufLen:= Length(infoBuffer);
okay:= HttpQueryInfo(hConnect,HTTP_QUERY_STATUS_CODE,@infoBuffer[0],
bufLen,dummy);
if not(okay) then
Result:= False
else
begin
reply:= infoBuffer;
if (reply='200') then Result:= True
else if (reply='401') then Result:= True
else if (reply='404') then Result:= False
else if (reply='500') then Result:= False
else Result:= False;
end;
InternetCloseHandle(hConnect);
end;
InternetCloseHandle(hINet);
end;
procedure DeleteIECache;
var
lpEntryInfo: PInternetCacheEntryInfo;
hCacheDir: LongWord;
dwEntrySize: LongWord;
begin
dwEntrySize := 0;
FindFirstUrlCacheEntry(nil, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
hCacheDir := FindFirstUrlCacheEntry(nil, lpEntryInfo^, dwEntrySize);
if hCacheDir <> 0 then
begin
repeat
DeleteUrlCacheEntry(lpEntryInfo^.lpszSourceUrlName);
FreeMem(lpEntryInfo, dwEntrySize);
dwEntrySize := 0;
FindNextUrlCacheEntry(hCacheDir, TInternetCacheEntryInfo(nil^), dwEntrySize);
GetMem(lpEntryInfo, dwEntrySize);
if dwEntrySize > 0 then lpEntryInfo^.dwStructSize := dwEntrySize;
until not FindNextUrlCacheEntry(hCacheDir, lpEntryInfo^, dwEntrySize);
end;
FreeMem(lpEntryInfo, dwEntrySize);
FindCloseUrlCache(hCacheDir);
end;
procedure TAutoLogin.Login_Web;
begin
UserTxt.Enabled:= True;
PassTxt.Enabled:= True;
LogBtn.Enabled:= True;
UserTxt.SetFocus;
end;
procedure TAutoLogin.Url_Open;
begin
if(IsUrlValid(OpenUrlText.Text)) then
begin
if(Application.MessageBox('Login?','Confirmation',MB_YESNO))=IDYES then
Login_Web
else
begin
ShellExecute(self.WindowHandle,'open',PChar(OpenUrlText.Text),
nil,nil, SW_MINIMIZE);
OpenUrlText.Clear;
end;
end
else ShowMessage('URL is Not Valid');
end;
procedure TAutoLogin.GoURLBtnClick(Sender: TObject);
begin
ConnectedToInternet;
if(OpenUrlText.Text<>'') then Url_Open
else
begin
ShowMessage('U Must Fill Url Text First');
OpenUrlText.SetFocus;
end;
end;
procedure TAutoLogin.HistoryBtnClick(Sender: TObject);
begin
if(Application.MessageBox('Delete History?','Confirmation',MB_YESNO))=IDYES then
begin
DeleteIECache;
end;
end;
procedure TAutoLogin.DefaultBtnClick(Sender: TObject);
begin
AutoLogin.Hide;
WebSiteDef.Show;
end;
procedure TAutoLogin.CloseBtnClick(Sender: TObject);
begin
Close;
Application.Terminate;
end;
procedure TAutoLogin.LogBtnClick(Sender: TObject);
begin
if(UserTxt.Text='')then
begin
ShowMessage('U Must Fill UserName');
UserTxt.SetFocus;
end
else if(PassTxt.Text='')then
begin
ShowMessage('U Must Fill Password');
PassTxt.SetFocus;
end
else
begin
FormLogin.Show;
FormLogin.WebBrowser1.Navigate(OpenUrlText.Text);
end;
end;
end.
//FormLogin (WebBrowser) :
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, OleCtrls, SHDocVw, ExtCtrls, MSHTML, ShellApi;
type
TFormLogin = class(TForm)
Panel2: TPanel;
CloseLogin: TSpeedButton;
WebBrowser1: TWebBrowser;
procedure CloseLoginClick(Sender: TObject);
procedure WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormLogin: TFormLogin;
implementation
{$R *.dfm}
uses Unit_con8;
procedure TFormLogin.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var iDoc: IHtmlDocument2;
i: integer;
ov: OleVariant;
iDisp: IDispatch;
iColl: IHTMLElementCollection;
iInputElement: IHTMLInputElement;
begin
WebBrowser1.ControlInterface.Document.QueryInterface(IID_IHTMLDocument, iDoc);
if not assigned(iDoc) then
begin
ShowMessage(' Nothing dowloaded ');
Exit;
end;
ov := 'INPUT';
IDisp := iDoc.all.tags(ov);
if assigned(IDisp) then
begin
IDisp.QueryInterface(IID_IHTMLElementCollection, iColl);
if assigned(iColl) then
begin
for i := 1 to iColl.Get_length do
begin
iDisp := iColl.item(pred(i), 0);
iDisp.QueryInterface(IID_IHTMLInputElement, iInputElement);
if assigned(iInputElement) then
begin
if (iInputElement.type_='text') or (iInputElement.name='user')
then iInputElement.value:= AutoLogin.UserTxt.Text;
if (iInputElement.type_= 'password') or (iInputElement.name='password')
or (iInputElement.name='passwd')
then iInputElement.value:= AutoLogin.PassTxt.Text;
Caption := AutoLogin.OpenUrlText.Text;
end;
end;
end;
end;
with AutoLogin do
begin
UserTxt.Clear;
PassTxt.Clear;
UserTxt.Enabled:= False;
PassTxt.Enabled:= False;
LogBtn.Enabled:= False;
OpenUrlText.Clear;
end;
end;
procedure TFormLogin.CloseLoginClick(Sender: TObject);
begin
FormLogin.Hide;
AutoLogin.Show;
end;
end.
|