TWebBrowser in Delphi

TWebBrowser component (in the Internet palette of Delphi IDE) is a Microsoft ActiveX® control that you can use on your application's forms to browse Web sites, view Web pages and other documents, and download data located on the Internet. The TWebBrowser component is useful in situations where you don't want to disrupt the work flow in your application by switching from your application to a Web browser or other document-viewing application. The TWebBrowser component can display any Web page that Microsoft Internet Explorer version 3.0 or later (i.e. 4.0, 4.01, 5, 5.5, 6 ...) can display. 

For example, the TTWebBrowser component can display pages that includeany of the following features:

1. Standard HTML and HTML enhancements, such as floating frames and 
    cascading style sheets
2. Other ActiveX controls
3. Most Netscape plug-ins
4. Scripting, such as Microsoft Visual Basic Scripting Edition (VBScript) or 
    JavaScript
5. JavaTM applets
6. Multimedia content, such as video and audio playback
7. Three-dimensional virtual worlds created with Virtual Reality Modeling 
    Language (VRML)

In addition to opening Web pages, the TWebBrowser component can open any ActiveX document, which includes most Microsoft Office documents. For example, if Microsoft Office is installed on a user's computer, an application that uses the TWebBrowser component can open and edit Microsoft Excel spreadsheets, Microsoft Word documents, and Microsoft PowerPoint presentations from within the control. Similarly, if Microsoft Excel Viewer, Microsoft Word Viewer, or Microsoft PowerPoint Viewer is installed, users can view those documents within the TWebBrowser component. 
With the TWebBrowser component, users of your application can browse sites on the World Wide Web, as well as folders on a local hard disk and on a local area network. Users can follow hyperlinks by clicking them or by typing a URL into a text box. Also, the TWebBrowser component maintains a history list that users can browse through to view previously browsed sites, folders, and documents.

Display a Web page or document in the TWebBrowser

To display a Web page or document in the TWebBrowser component, we need use the Navigate method programmatically. The syntax for the Navigate method is:

procedure Navigate(const URL: WideString); 
overload;
 
procedure Navigate(const URL: WideString;
  var Flags: OleVariant); overload;
 
procedure Navigate(const URL: WideString;
  var Flags: OleVariant;
  var TargetFrameName: OleVariant); overload;
 
procedure Navigate(const URL: WideString;
  var Flags: OleVariant;
  var TargetFrameName: OleVariant;
  var PostData: OleVariant); overload;
 
procedure Navigate(const URL: WideString;
  var Flags: OleVariant;
  var TargetFrameName: OleVariant;
  var PostData: OleVariant;
  var Headers: OleVariant); overload;

Where:

URL specifies the path name of a file or the Uniform Resource Locator (URL) of an Internet resource that the Web browser should display. If URL refers to an Internet protocol and a location on an intranet server, the computer running your application must be connected to the internet or intranet and have permission to access that server. If URL refers to a standard file system path on a local hard drive or intranet, the TWebBrowser component opens the document and displays it immediately. The TWebBrowser component can open Microsoft Office documents, text files, and HTML documents.

Flags is a set of values that specify whether to add the resource to the history list, whether to read from or write to the cache, and whether to display the resource in a new window.

TargetFrameName is the name of the frame in which the resource will be displayed, or nil if the resource should not be displayed in a named frame.

PostData contains the data sent to the server when using Navigate to generate an HTTP POST message. If PostData is nil, Navigate generates an HTTP GET message. PostData is ignored if URL does not specify an HTTP URL.

Headers contains any headers sent to the servers when the URL represents an HTTP URL. HTTP headers specify such things as the intended action required of the server, the type of data, and so on

Now let’s check with an example.

Create a new VCL Application project and Save to a folder. Then add a TWebBroswer component from tool palate to form. Then add buttons, Edits or other required components to form as follow.

unit Unit1;
interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.OleCtrls, SHDocVw, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons;
type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Panel1: TPanel;
    Label1: TLabel;
    editURL: TEdit;
    btnLoad: TButton;
    btnnext: TSpeedButton;
    btnback: TSpeedButton;
    btnrefresh: TSpeedButton;
    btnHome: TSpeedButton;
    btnStop: TSpeedButton;
    btnQuit: TSpeedButton;
    procedure btnLoadClick(Sender: TObject);
    procedure editURLKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure btnbackClick(Sender: TObject);
    procedure btnnextClick(Sender: TObject);
    procedure btnHomeClick(Sender: TObject);
    procedure btnrefreshClick(Sender: TObject);
    procedure btnStopClick(Sender: TObject);
    procedure btnQuitClick(Sender: TObject);
    procedure WebBrowser1TitleChange(ASender: TObject; const Text: WideString);
  private
    { Private declarations }
    procedure OpenURL;
  public
    { Public declarations }
  end;
var
  Form1: TForm1;

implementation
{$R *.dfm}

procedure TForm1.OpenURL;
begin
  WebBrowser1.Navigate(editURL.Text);
// URL can be www.google.com
// URL can be C:\testfolder
// URL can be C:\testfolder\tset1.jpg
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
  OpenURL;
end;

procedure TForm1.editURLKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_RETURN then
    OpenURL;
end;

procedure TForm1.btnbackClick(Sender: TObject);
begin
  WebBrowser1.GoBack
end;

procedure TForm1.btnHomeClick(Sender: TObject);
begin
  WebBrowser1.GoHome;
end;

procedure TForm1.btnnextClick(Sender: TObject);
begin
  WebBrowser1.GoForward;
end;

procedure TForm1.btnQuitClick(Sender: TObject);
begin
  WebBrowser1.Quit;
end;

procedure TForm1.btnrefreshClick(Sender: TObject);
begin
  WebBrowser1.Refresh;
end;

procedure TForm1.btnStopClick(Sender: TObject);
begin
  WebBrowser1.Stop;
end;

procedure TForm1.WebBrowser1TitleChange(ASender: TObject;
  const Text: WideString);
begin
  editURL.Text := Text;
end;

end.

Design Time.


After Run.

As web browser and html browser


As file explorer.


As picture preview



Comments

  1. why google page/other web pages is not showing as we see in latest browsers??

    ReplyDelete

Post a Comment

Popular posts from this blog

MS Excel Automation in Delphi

ShellExecute in Delphi

Drawing Shapes in Delphi