1

我使用此例程将 HTML 添加到 TWebbrowser 文档:

procedure WBAppendHTML(WB: SHDocVw.TWebbrowser; const HTML: string);
(*Appends the given HTML to the end of the given web browser control's current document body.
Does nothing if no document is loaded, if the document does not support the DOM, or if the document is a frameset.*)
var
  Doc: MSHTML.IHTMLDocument2;
  BodyElem: MSHTML.IHTMLBodyElement;
  Range: MSHTML.IHTMLTxtRange;
begin
  if not System.SysUtils.Supports(WB.Document, MSHTML.IHTMLDocument2, Doc) then
    EXIT;
  if not System.SysUtils.Supports(Doc.body, MSHTML.IHTMLBodyElement, BodyElem) then
    EXIT;
  Range := BodyElem.createTextRange;
  Range.collapse(False);
  Range.pasteHTML(HTML);
end;

这适用于 HTML 是这样的:'我的文本':

WBAppendHTML(wb, '<B>My text</B>');

但它不适用于 HTML 注释:

WBAppendHTML(wb, '<!-- {93706302-FC6C-43D6-8362-DE71550AD998} -->');

为什么这不起作用?那么如何在 TWebbrowser 文档中添加 HTML 注释呢?

编辑 201306142055:

为了更好地了解我如何测试是否添加了评论:尝试使用 WBAppendHTML 附加评论后,我将文档复制到剪贴板:

// select the entire document:
wb.ExecWB(OLECMDID_SELECTALL,      OLECMDEXECOPT_DODEFAULT);
// copy the text to Clipboard:
wb.ExecWB(OLECMDID_COPY,           OLECMDEXECOPT_DODEFAULT);
// clear the selection:
wb.ExecWB(OLECMDID_CLEARSELECTION, OLECMDEXECOPT_DONTPROMPTUSER);

之后,我从剪贴板内容的 CF_HTML 部分检索 HTML 源代码:

function GetClipboardAsHTMLSource: string;
var
  Data: THandle;
  Ptr: PAnsiChar; // PChar;
begin
  Result := '';  
  Clipboard.Open;
  try
    Data := Clipboard.GetAsHandle(CF_HTML);
    if Data <> 0 then
    begin
      Ptr := PAnsiChar(GlobalLock(Data));
      if Ptr <> nil then
      begin
        try
          Result := UTF8Decode(Ptr);
        finally
          GlobalUnlock(Data);
        end;
      end;
    end;
  finally
    Clipboard.Close;
  end;  
end;
4

0 回答 0