...open local files in a TWebbrowser and start links directly?

Author: Thomas Stutz

Category: Objects/ActiveX

{
  This example shows how to open local files in a TWebbrowser
  and start links directly without showing a Dialog.
}

{
  Dieses Beispiel zeigt, wie man lokale Dateien in einem TWebbrowser
  öffnen kann und wie man lokale Links direct ausführen kann ohne
  dass ein Dialog erscheint.
}


unit Unit1;

interface

uses
  
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw;

type
  
TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure WebBrowser1BeforeNavigate2(Sender: TObject;
      const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
      Headers: OleVariant; var Cancel: WordBool);
  private
    
FIsStartPage: Boolean;
    { Private declarations }
  
public
    
{ Public declarations }
  
end;

var
  
Form1: TForm1;

implementation

{$R *.DFM}

uses
  
ShellApi;


// Open a local page:
procedure TForm1.Button1Click(Sender: TObject);
const
 
LOCAL_PAGE ='C:/StartPage.htm'
begin
  
FIsStartPage := True;
  Webbrowser1.Navigate('file:///' + LOCAL_PAGE);
  FIsStartPage := False;
end;


procedure TForm1.WebBrowser1BeforeNavigate2(Sender: TObject;
  const pDisp: IDispatch; var URL, Flags, TargetFrameName, PostData,
  Headers: OleVariant; var Cancel: WordBool);
var
  
newURL: string;
begin
  
newURL := URL;
  // For local links, don't show a dialog but open the file directly
  
if (not FIsStartPage) and FileExists(newURL) then
  begin
    
Cancel := True;
    ShellExecute(Application.Handle, 'open', PChar(newURL), nilnil, SW_NORMAL);
  end;
end;


 

printed from
www.swissdelphicenter.ch
developers knowledge base