Solved

hide TWebBrowser

Posted on 2006-07-17
11
1,277 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 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