Solved

delphi override Twebbrowser

Posted on 2010-11-19
4
916 Views
Last Modified: 2012-06-27
Hello experts,

I try to make a cutomized webbrowser which infact is a Twebbrowser that has an extra feature: it dispatch an event TimerOut when the page does not load within 30 seconds.

SO I added an timer in TwebbrowsersTimerOut that inherits from Twebbrowser.

The idea would be to lunch this timer on the BeforeNavigate or DownloadBegin event:  and stop the counter on the onDownloadComplete event.

**Trouble is that I have no idea how to override these events:**  and more globally how to override events from existing components: can someone help me with overriding TwebBrowser components and especailly its events ?


Thanks
0
Comment
Question by:yarekGmail
  • 3
4 Comments
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34177860

There are really no methods to override in the TWebBrowser component. You could just add a timer to your form and when the downloadbegin is triggered start your timer and stop it when DownLoadComplete event is triggered

However, if you want to extend, TWebBrowser, you can add your own events to mirror the existing events then hide the existing events. Here is an example with BeginDownload and Downloacomplete events
uses
  Windows, SysUtils, Classes, OleCtrls, SHDocVw, ExtCtrls;

type
  TCustomWebBrowser = class(TWebBrowser)
  private
    FOnCustomDownloadBegin: TNotifyEvent;
    FOnCustomDownloadComplete: TNotifyEvent;
    FTimer: TTimer;
    procedure DoDownLoadComplete(Sender: TObject);
    procedure DoDownLoadBegin(Sender: TObject);
    procedure DoTimerEvent(Sender: TObject);
    function GetTimeout: Cardinal;
    procedure SetTimeout(const Value: Cardinal);
  protected
    property OnDownloadBegin;
    property OnDownloadComplete;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Timeout: Cardinal read GetTimeout write SetTimeout;
    property OnCustomDownloadBegin: TNotifyEvent read FOnCustomDownloadBegin write FOnCustomDownloadBegin;
    property OnCustomDownloadComplete: TNotifyEvent read FOnCustomDownloadComplete write FOnCustomDownloadComplete;
  end;


implementation


{ TCustomWebBrowser }

constructor TCustomWebBrowser.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FTimer := TTimer.Create(nil);
  //set the events
  OnDownloadBegin := DoDownloadBegin;
  OnDownloadComplete := DoDownLoadComplete;
end;

destructor TCustomWebBrowser.Destroy;
begin
  FTimer.Enabled := False;
  FreeAndNil(FTimer);
  inherited Destroy;
end;

procedure TCustomWebBrowser.DoDownLoadBegin(Sender: TObject);
begin
  if Assigned(FOnCustomDownLoadBegin) then
    FOnCustomDownLoadBegin(Self);
  FTimer.Enabled := True;
end;

procedure TCustomWebBrowser.DoDownLoadComplete(Sender: TObject);
begin
  FTimer.Enabled := False;
  if Assigned(FOnCustomDownloadComplete) then
    FOnCustomDownloadComplete(Self)
end;

procedure TCustomWebBrowser.DoTimerEvent(Sender: TObject);
begin
  //add what you want to do here
end;

function TCustomWebBrowser.GetTimeout: Cardinal;
begin
  Result := FTimer.Interval;
end;

procedure TCustomWebBrowser.SetTimeout(const Value: Cardinal);
begin
  FTimer.Interval := Value
end;

end;

Open in new window

0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 34177869
You have to set the ontimer event on create

uses
  Windows, SysUtils, Classes, OleCtrls, SHDocVw, ExtCtrls;

type
  TCustomWebBrowser = class(TWebBrowser)
  private
    FOnCustomDownloadBegin: TNotifyEvent;
    FOnCustomDownloadComplete: TNotifyEvent;
    FTimer: TTimer;
    procedure DoDownLoadComplete(Sender: TObject);
    procedure DoDownLoadBegin(Sender: TObject);
    procedure DoTimerEvent(Sender: TObject);
    function GetTimeout: Cardinal;
    procedure SetTimeout(const Value: Cardinal);
  protected
    property OnDownloadBegin;
    property OnDownloadComplete;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property Timeout: Cardinal read GetTimeout write SetTimeout;
    property OnCustomDownloadBegin: TNotifyEvent read FOnCustomDownloadBegin write FOnCustomDownloadBegin;
    property OnCustomDownloadComplete: TNotifyEvent read FOnCustomDownloadComplete write FOnCustomDownloadComplete;
  end;


implementation


{ TCustomWebBrowser }

constructor TCustomWebBrowser.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FTimer := TTimer.Create(nil);
  FTimer.OnTimer := DoTimerEvent;
  //set the events
  OnDownloadBegin := DoDownloadBegin;
  OnDownloadComplete := DoDownLoadComplete;
end;

destructor TCustomWebBrowser.Destroy;
begin
  FTimer.Enabled := False;
  FreeAndNil(FTimer);
  inherited Destroy;
end;

procedure TCustomWebBrowser.DoDownLoadBegin(Sender: TObject);
begin
  if Assigned(FOnCustomDownLoadBegin) then
    FOnCustomDownLoadBegin(Self);
  FTimer.Enabled := True;
end;

procedure TCustomWebBrowser.DoDownLoadComplete(Sender: TObject);
begin
  FTimer.Enabled := False;
  if Assigned(FOnCustomDownloadComplete) then
    FOnCustomDownloadComplete(Self)
end;

procedure TCustomWebBrowser.DoTimerEvent(Sender: TObject);
begin
  //add what you want to do here
end;

function TCustomWebBrowser.GetTimeout: Cardinal;
begin
  Result := FTimer.Interval;
end;

procedure TCustomWebBrowser.SetTimeout(const Value: Cardinal);
begin
  FTimer.Interval := Value
end;

end;
0
 

Author Comment

by:yarekGmail
ID: 34179333
This is Great but I have ONE QUETSION:
You added some EXTRA events: like OnCustomDownloadBegin, OnCustomDownloadComplete

Is it possible to override the existing one without having to create new ones ?

regards
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34180387

Not incase of the TWebBrowser, you can only override methods, not events. Since TWebBrowser is a direct descendant of TOleControl, there are no methods of TWebBrowser that can be overriden. You can only create extra methods and events
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
LAN or WAN ? 11 105
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 75
Dev express lookupcombo 3 44
Not able to call Delphi XE10 dll function from Delphi 6.0 4 48
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question