Solved

delphi override Twebbrowser

Posted on 2010-11-19
4
884 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:ewangoya
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:
ewangoya 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:ewangoya
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 10 Seattle Dataset Actions 5 79
Find and Replace Stream with 0s 8 57
FMX and jaudiotracker playing memory stream 29 67
Dynamically Created Query 3 50
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now