gpt4 book ai didi

delphi - WebBrowser1DocumentComplete 和 WebBrowser1NavigateComplete2

转载 作者:行者123 更新时间:2023-12-02 01:25:47 35 4
gpt4 key购买 nike

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

  • [错误] 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.

最佳答案

问题是,您没有声明这些方法。

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;

关于delphi - WebBrowser1DocumentComplete 和 WebBrowser1NavigateComplete2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17939813/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com