好的,使用 Delphi 7 我能够重现该问题。
在深入研究问题后,我发现有两种不同的方式来获取加载页面的来源:
1) procedure GetSourceProc(const proc: TCefStringVisitorProc);
当你使用。该解决方案的问题在于 Dlephi 2009 之前的版本,它只接受一个独立的过程,当您有多个对象实例并希望在其中保留 HTML 时,这会给您带来问题。
2) procedure GetSource(const visitor: ICefStringVisitor);
让我们从查看预期的界面开始
ICefStringVisitor = interface(ICefBase)
['{63ED4D6C-2FC8-4537-964B-B84C008F6158}']
procedure Visit(const str: ustring);
end;
所以我开始研究那个。深入挖掘后,unit ceflib;
我发现TCefStringVisitorOwn
这是您解决方案的关键。
的界面 TCefStringVisitorOwn
非常简单:
TCefStringVisitorOwn = class(TCefBaseOwn, ICefStringVisitor)
protected
procedure Visit(const str: ustring); virtual;
public
constructor Create; virtual;
end;
所以有了这个,我创建了一个后代 TCefStringVisitorOwn
:
unit SourceContainerU;
interface
uses
ceflib;
type
ISourceContainer = interface(ICefStringVisitor)
function Source: ustring;
end;
TSourceContainer = class(TCefStringVisitorOwn, ISourceContainer)
private
FSource: ustring;
protected
procedure Visit(const str: ustring); override;
public
function Source: ustring;
end;
implementation
{ TSourceContainer }
function TSourceContainer.Source: ustring;
begin
Result := FSource;
end;
procedure TSourceContainer.Visit(const str: ustring);
begin
FSource := str;
end;
end.
这里没有什么神奇的。
剩下的就是使用这个类创建一个演示项目。在我的示例中,我TChromium
在运行时创建。该表单包含一个按钮和一个备忘录,然后是以下代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
cefvcl, ceflib, SourceContainerU;
type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Chromium1: TChromium;
SourceContainer : ISourceContainer;
procedure Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer);
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.Chromium1LoadEnd(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; httpStatusCode: Integer);
begin
if Frame = nil then
exit;
SourceContainer := TSourceContainer.Create;
Frame.GetSource(SourceContainer);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Chromium1 := TChromium.Create(self);
Chromium1.Parent := self;
Chromium1.OnLoadEnd := Chromium1LoadEnd;
Chromium1.load('http://www.google.com');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Assigned(SourceContainer) then
Memo1.Lines.Text := SourceContainer.Source;
end;
end.
该解决方案已使用最新版本的 Chromium 进行了测试,我已经在Delphi 7、Delphi XE和Delphi XE6中对其进行了测试。我没有任何Delphi 2007安装,但我相信它也应该在 wotk 中工作。