如何将一段HTML文字在webbrowser中显示出来(100分)

  • 主题发起人 主题发起人 yexiaoming
  • 开始时间 开始时间
Y

yexiaoming

Unregistered / Unconfirmed
GUEST, unregistred user!
比如有如下一段html文字存于memo1中,如何在webbrowser中显示出来。请在行的大虾指点.
<html>
<body>
<p>hello world!</p>
</body>
</html>
之前查过的回答大概如下:但初次接触不知如何使用。
uses comobj, activex, mshtml, Variants;
procedure TForm1.htmlButtonClick(Sender: TObject);
var
html:string;
Stream: IStream;
hHTMLText: HGLOBAL;
psi: IPersistStreamInit;
DoC: IHTMLDocument2;
begin
Doc := WebBrowser1.Document as IHTMLDocument2;
SetLength(html, strtoint(DOC.Get_fileSize)+1);
html:=Doc.body.outerHTML;
if not Assigned(WebBrowser2.Document) then Exit;
hHTMLText := GlobalAlloc(GPTR, Length(Html) + 1);
if 0 = hHTMLText then RaiseLastWin32Error;
CopyMemory(Pointer(hHTMLText), PChar(Html), Length(Html));
OleCheck(CreateStreamOnHGlobal(hHTMLText, True, Stream));
try
OleCheck(WebBrowser2.Document.QueryInterface(IPersistStreamInit, psi));
try
OleCheck(psi.InitNew);
OleCheck(psi.Load(Stream));
finally
psi := nil;
end;
finally
Stream := nil;
end;
end;
 
uses MSHTML, Activex;
{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
vv: Variant;
HTMLDocument: IHTMLDocument2;
begin
tag := 0;
WebBrowser1.Navigate('about:blank');
while(tag=0) do Application.ProcessMessages;
vv := VarArrayCreate([0, 0], varVariant);
vv[0] := '<html><body><p>hello world!</p></body></html>';
HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
HTMLDocument.Write(PSafeArray(TVarData(vv).VArray));
HTMLDocument.Close;
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
begin
tag := 1;
end;
 
谢谢zw84611,之前我也有调试过了,只是忘了加uses 语句。这是在hubdog上面有的.
 
后退
顶部