Solved

hide TWebBrowser

Posted on 2006-07-17
11
1,234 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
Comment Utility

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
Comment Utility
or put a TPanel over it.
0
 
LVL 17

Expert Comment

by:TheRealLoki
Comment Utility
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
Comment Utility

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
Comment Utility
a very simple way ( I use it ) is to put the browser somewhere outside your screen. To say Browser.Top := - (Browser.Height +10 );
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility

 >> 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
Comment Utility
ah thanks for that Russel, you learn something new every day :-)
0
 
LVL 26

Expert Comment

by:Russell Libby
Comment Utility
Its something I had to learn the HARD way :-%

<g>
Russell
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
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
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

743 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

8 Experts available now in Live!

Get 1:1 Help Now