Solved

hide TWebBrowser

Posted on 2006-07-17
11
1,293 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

710 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