Solved

hide TWebBrowser

Posted on 2006-07-17
11
1,252 Views
Last Modified: 2012-06-27
I need to make TWebBrowser not visible but operate same like visible?
Let say put on hiden form but stil load pages normaly. is it posible?
0
Comment
Question by:delfis
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 17123631

The simplest way is to set the width and or height to zero, which allows it operate normally and still fire the events correctly.

Regards,
Russell

0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17123830
or put a TPanel over it.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17125669
just create it dynamincally, so that it is not visible

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw_TLB;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    bgo: TButton;
    procedure bgoClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    HiddenWebBrowser: TWebBrowser;
    procedure HiddenWebBrowser_BeforeNavigate2(Sender: TObject;
      const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
      Headers: OleVariant; var Cancel: WordBool);
    procedure HiddenWebBrowser_NavigateComplete2(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure HiddenWebBrowser_NavigateError(Sender: TObject;
      const pDisp: IDispatch; var URL, Frame, StatusCode: OleVariant;
      var Cancel: WordBool);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
    // create our hidden webbrowser
    HiddenWebBrowser := TWebBrowser.Create(nil);
    HiddenWebBrowser.OnBeforeNavigate2 := HiddenWebBrowser_BeforeNavigate2;
    HiddenWebBrowser.OnNavigateComplete2 := HiddenWebBrowser_NavigateComplete2;
    HiddenWebBrowser.OnNavigateError := HiddenWebBrowser_NavigateError;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
    if assigned(HiddenWebBrowser) then
      FreeAndNil(HiddenWebBrowser);
end;

procedure TForm1.HiddenWebBrowser_BeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
    memo1.lines.add('before navigate: ' + url);
end;

procedure TForm1.HiddenWebBrowser_NavigateComplete2(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
    memo1.lines.add('navigate complete: ' + url);
end;

procedure TForm1.HiddenWebBrowser_NavigateError(Sender: TObject;
  const pDisp: IDispatch; var URL, Frame, StatusCode: OleVariant;
  var Cancel: WordBool);
begin
    memo1.lines.add('navigate error: ' + url + #13#10 + statuscode);
end;

procedure TForm1.bgoClick(Sender: TObject);
begin
    HiddenWebBrowser.Navigate('http://yahoo.com');
end;

end.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17125835

Thats fine as long as you aware of the limitations. If you keep the browser control hidden, the DocumentComplete event will NOT fire. This is documented by MS and can be verified with the code below:

Russell


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    bgo: TButton;
    procedure bgoClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    HiddenWebBrowser: TWebBrowser;
    procedure HiddenWebBrowser_BeforeNavigate2(Sender: TObject;
      const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
      Headers: OleVariant; var Cancel: WordBool);
    procedure HiddenWebBrowser_NavigateComplete2(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure HiddenWebBrowser_DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
    // create our hidden webbrowser
    HiddenWebBrowser := TWebBrowser.Create(nil);
    HiddenWebBrowser.OnBeforeNavigate2 := HiddenWebBrowser_BeforeNavigate2;
    HiddenWebBrowser.OnNavigateComplete2 := HiddenWebBrowser_NavigateComplete2;
    HiddenWebBrowser.OnDocumentComplete:=HiddenWebBrowser_DocumentComplete;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
    if assigned(HiddenWebBrowser) then
      FreeAndNil(HiddenWebBrowser);
end;

procedure TForm1.HiddenWebBrowser_DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
   memo1.lines.add('document complete: ' + url);
end;

procedure TForm1.HiddenWebBrowser_BeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
begin
    memo1.lines.add('before navigate: ' + url);
end;

procedure TForm1.HiddenWebBrowser_NavigateComplete2(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
    memo1.lines.add('navigate complete: ' + url);
end;

procedure TForm1.bgoClick(Sender: TObject);
begin
    HiddenWebBrowser.Navigate('http://yahoo.com');
end;

end.

0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17127678
a very simple way ( I use it ) is to put the browser somewhere outside your screen. To say Browser.Top := - (Browser.Height +10 );
0
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.

 
LVL 26

Expert Comment

by:Russell Libby
ID: 17127693

 >> The simplest way is to set the width and or height to zero, which allows it operate normally and still fire the events correctly.

Set width to zero, set height to zero, move it off the display coords of the form, cover it with another contol, place it in panel and hide the panel, it all comes down to one point. The control itself should not be hidden in order for all events to fire correctly, but it does not have to appear visually on the screen


 
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17127764
ah thanks for that Russel, you learn something new every day :-)
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17127774
Its something I had to learn the HARD way :-%

<g>
Russell
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17131302
If you set the width and height to zero you can't get a snap of browser image for example. OK I can now :) but I talk using the codes found usualy on net.

0
 
LVL 9

Expert Comment

by:ginsonic
ID: 17131345
What I don't understand whay you can't navigate with hidden browsers? I use in my brower app TEmbeddedWB, better than TWebBrowser. I create a new panel with an EmbeddedWB inside it for each session. I set all panels to Visible:=False and just keep visible the selected browser. When wish to change set the curent to not visible and true to new browser.

I can navigate to any webpage with browser set to visible:=false.
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17133915
with an invisible twebbrowser, you can navigate, and it works fine, but you do not get the "DocumentComplete" event, which is the most reliable way of knowing the page has finished loading... probably not a big deal if you have another way of knowing, or just wait a large amount of time
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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

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