Solved

hide TWebBrowser

Posted on 2006-07-17
11
1,261 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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 …

770 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