0

如何在 MSHTML 中设置整个 HTML?

我正在尝试使用此作业:

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

但我得到了错误:

“此操作的目标元素无效”

我也尝试过使用

(Document as IHTMLDocument2).write 

但是这个表单只在正文部分添加了 HTML,我需要替换所有的 HTML 源。
有人知道我是怎么做到的吗?

提前致谢。

4

2 回答 2

1

这是我的一些旧代码,看看它是否对您有帮助:

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;
于 2010-03-20T14:31:45.550 回答
0

作为替代方案,您还可以使用TEmbededWB,它是 Web 浏览器的扩展包装器,并具有一些易于使用的方法来提供此功能。

于 2010-03-22T16:12:02.207 回答