Delphi让网页只允许在WebBrowser里面打开

[添加组件]

添加 Internet->WebBrowser //显示网页

[添加事件]

鼠标点击WebBrowser组件,在Events事件选项框中找到.

OnNewWindows2,OnStatusTextChange.双击添加事件.

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1NewWindow2(Sender: TObject; var ppDisp: IDispatch;
      var Cancel: WordBool);
    procedure WebBrowser1StatusTextChange(Sender: TObject;
      const Text: WideString);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  NewURL:String;
  //定义一个全局变量,让这个全局变量来获取最新的连接地址.

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
    WebBrowser1.Navigate(http://www.cnblogs.com/)
    //默认打开博客园
end;

procedure TForm1.WebBrowser1NewWindow2(Sender: TObject;
  var ppDisp: IDispatch; var Cancel: WordBool);
begin
    Cancel:=true;
    //对话框,这个可以这么理解.
    WebBrowser1.Navigate(NewURL);
    //读取新的地址,并加载.
end;

procedure TForm1.WebBrowser1StatusTextChange(Sender: TObject;
  const Text: WideString);
begin
    NewURL:=text;
    //Url地址发生改变,存到全局变量当中.
end;

end.

 

Delphi让网页只允许在WebBrowser里面打开

上一篇:蓝猫微谈全纪录 | 解读奇书《WIN BIGLY》,揭晓特朗普说服的秘密


下一篇:WPF学习笔记(一)