Title: How to open local files in a TWebbrowser and start links directly
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), nil, nil, SW_NORMAL);
end;
end;