WebBrowser1DocumentComplete和WebBrowser1NavigateComplete2

时间:2013-07-30 06:32:21

标签: delphi twebbrowser

以下代码给出了几个错误,其中两个是:

  • [错误] Unit1.pas(28):未声明的标识符: 'WebBrowser1NavigateComplete2'
  • [错误] Unit1.pas(34):未声明的标识符: 'WebBrowser1DocumentComplete'

我想知道它们是否未在“使用”部分中声明。 有没有人知道为什么它不会编译。 我从Delphi帮助中获得了代码。

也不会出现完成的“消息框”或“嘟嘟声”。

unit Unit1;

interface

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

type
    TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
    Form1: TForm1;
    CurDispatch: IDispatch; {save the interface globally }

implementation

{$R *.dfm}
procedure TForm1.WebBrowser1NavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
    if CurDispatch = nil then
    CurDispatch := pDisp; { save for comparison }
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
begin
if (pDisp = CurDispatch) then
    begin
        Beep; {the document is loaded, not just a frame }
        showmessage('download complete');
        CurDispatch := nil; {clear the global variable }
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
    WebBrowser1.Navigate('www.google.com');
end;

end.

2 个答案:

答案 0 :(得分:5)

问题是,你没有声明方法。

type
    TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    // missing declarations
    procedure WebBrowser1NavigateComplete2(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
    procedure WebBrowser1DocumentComplete(Sender: TObject; const pDisp: IDispatch; var URL: OleVariant);
private
    { Private declarations }
public
    { Public declarations }
end;

似乎你只是从其他地方复制'粘贴方法:o)


手动分配事件处理程序

出于测试目的,我有一个带有单个TWebBrowser组件的简单表单,我编写了2个方法WebBrowser1DocumentCompleteHandmadeDocumentComplete

表单代码如下所示

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;

type
  TForm1 = class( TForm )
    WebBrowser1 : TWebBrowser;
    procedure WebBrowser1DocumentComplete( ASender : TObject; const pDisp : IDispatch; const URL : OleVariant );
    procedure HandmadeDocumentComplete( ASender : TObject; const pDisp : IDispatch; const URL : OleVariant );
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1 : TForm1;

implementation

{$R *.dfm}

procedure TForm1.HandmadeDocumentComplete( ASender : TObject; const pDisp : IDispatch; const URL : OleVariant );
begin
  // Do something
end;

procedure TForm1.WebBrowser1DocumentComplete( ASender : TObject; const pDisp : IDispatch; const URL : OleVariant );
begin
  // Do something
end;

end.

现在看一下WebBrowser1

的ObjectInspector

ObjectInspector

与事件处理程序属性匹配的每个已发布的方法都将显示在组合框中,并且可以选择。

或者您可以按代码分配方法

procedure TForm1.FormCreate( Sender : TObject );
begin
  // WebBrowser1.OnDocumentComplete := WebBrowser1DocumentComplete;
  WebBrowser1.OnDocumentComplete := HandmadeDocumentComplete;
end;

答案 1 :(得分:1)

此代码正常运行。在Xe³下测试。 http://i.stack.imgur.com/y3WdX.png

enter image description here

以下是您的代码和代码之间的差异。我的编译代码。

http://i.stack.imgur.com/sRITO.png

enter image description here

您必须将TWebBrowser可视组件放入表单。

http://i.stack.imgur.com/t1CBY.png

enter image description here

enter image description here