浏览器直接读文本的问题! (50分)

  • 主题发起人 主题发起人 softchina
  • 开始时间 开始时间
S

softchina

Unregistered / Unconfirmed
GUEST, unregistred user!
我想把一段文字直接在浏览器中显示出来,我用了下面的程序,当点击
按钮的时候出现,Access violation at address XXXX in module
'project1.exe'.read of address FFFFFF.
请大家帮忙看一下,告诉我错在哪里,谢谢!!
(应该是IHtmlDocument出的错!)

unit unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,OleCtrls, SHDocVw, StdCtrls, Activex;

type
TForm1 = class(TForm)
Button1: TButton;
IE: TWebBrowser;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure ShowInWebBrowser(WebB:TWebBrowser;Str:String);
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

uses comobj,mshtml;

procedure TForm1.Button1Click(Sender: TObject);
var
S:String;
begin
S:='<html><body><H1>This is a demo!!!</H1></body></html>';
ShowInWebBrowser(IE,S);
end;

procedure TForm1.ShowInWebBrowser(WebB:TWebBrowser;Str:String);
var
v: Variant;
HTMLDocument: IHTMLDocument2;
begin
HTMLDocument := WebB.Document as IHTMLDocument2;
v := VarArrayCreate([0, 0], varVariant);
v[0] := str;
HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
HTMLDocument.Close;
end;

initialization
OleInitialize(nil);

finalization
OleUninitialize;

end.

 
S:='<html><body><A>This is a demo!!!</A></body></html>';
哪有这样的?
试试 S:='<html><body>This is a demo!!!</body></html>';

 
以下代码通过

var
Doc: OleVariant;
begin
Doc:= Wb.Document;
Doc.Write('<html><body><H1>This is a demo!!!</H1></body></html>');
end;
 
我按照您的代码加入程序中,还是出现Access violation at address XXXX in module
'project1.exe'.read of address FFFFFF的错误!以下是代码:

unit Unit1;

interface

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

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

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
Doc: OleVariant;
begin
Doc:= Wb.Document;
Doc.Write('<html><body><H1>This is a demo!!!</H1></body></html>');
end;

end.
 
那是因为你没有对 TWEBBROWSER 控件进行初始化的问题;你在 FORM 的 ONCREATE 事件中
加入:
var
URL : OleVariant;
begin
URL := 'about:blank';
Wb.Navigate2(URL);
end;


procedure TForm1.Button1Click(Sender: TObject);
var
Doc: OleVariant;
begin
Doc:= Wb.Document;
Doc.Write('<html><body><H1>This is a demo!!!</H1></body></html>');
end;
 
是了,你的WebBrowser.Document没有初始化了.

var
Doc: OleVariant;
begin
WebBrowser.Navigate('About:Blank');
Doc:= WebBrowser.Document;
//同样可以用:Doc.Body.InnerHTML:='<html><body><H1>This is a demo!!!</H1></body></html>';
WebBrowser.Write('<html><body><H1>This is a demo!!!</H1></body></html>');
end;
没有问题的.


 
  [:)][red]遇到这种问题,重启一下Delphi就好了[/red]
 
多人接受答案了。
 
后退
顶部