本文介绍了如何在MSHTML中设置整个HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在MSHTML中设置整个HTML?

How to set entire HTML in MSHTML?

我试图使用这个任务:

I am trying using this assignment:

   (Document as IHTMLDocument3).documentElement.innerHTML := 'abc';  

但我收到错误:

but I got the error:

我也试过使用 p>

I've also tried using

(Document as IHTMLDocument2).write 

但这种形式只会将HTML添加到正文部分,而我需要替换所有HTML源。

有人知道我是如何做到这一点的吗?

but this form only adds HTML into the body section, and I need to replace all the HTML source.
Does somebody have any idea of how I do this?

在此先感谢。

推荐答案

以下是我的一些旧代码,看看它是否可以帮助你:

Here's some of my old code, see if it helps you:

type
  THackMemoryStream = class(TMemoryStream);

procedure Clear(const Document: IHTMLDocument2);
begin
  Document.write(PSafeArray(VarArrayAsPSafeArray(VarArrayOf([WideString('')]))));
  Document.close;
end;

procedure LoadFromStream(const Document: IHTMLDocument2; Stream: TStream);
var
  Persist: IPersistStreamInit;
begin
  Clear(Document);
  Persist := (Document as IDispatch) as IPersistStreamInit;
  OleCheck(Persist.InitNew);
  OleCheck(Persist.Load(TStreamAdapter.Create(Stream)));
end;

procedure SetHtml(const Document: IHTMLDocument2; const Html: WideString);
var
  Stream: TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  try
    THackMemoryStream(Stream).SetPointer(PWideChar(Html), (Length(Html) + 1) * SizeOf(WideChar));
    Stream.Seek(0, soFromBeginning);
    LoadFromStream(Document, Stream);
  finally
    Stream.Free;
  end;
end;

这篇关于如何在MSHTML中设置整个HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:07