如何更改 TWebBrowser 中的字体?

How to change font in TWebBrowser?

此问题与:

我正在尝试使用 doc.body.style.fontFamily 更改 TWebBrowser 中的字体,但没有任何反应。字体还是TimesNewRoman.

procedure THTMLEdit.SetHtmlCode(CONST HTMLCode: string);
VAR
   Doc: Variant;
begin
 if NOT Assigned(wbBrowser.Document)
 then wbBrowser.Navigate('about:blank');

 WHILE wbBrowser.ReadyState < READYSTATE_INTERACTIVE
   DO Application.ProcessMessages;

 Doc := wbBrowser.Document;
 Doc.Clear;
 Doc.Write(HTMLCode);
 doc.body.style.fontFamily:='Arial'; <------ won't work
 Doc.DesignMode := 'On';
 Doc.Close;
end;

您需要在关闭文档后让文档再次交互。 例如:

procedure TForm1.SetHtmlCode(CONST HTMLCode: string);
VAR
   Doc: Variant;
begin
  if NOT Assigned(wbBrowser.Document)
  then wbBrowser.Navigate('about:blank');

  //WHILE wbBrowser.ReadyState < READYSTATE_INTERACTIVE // not really needed
  //DO Application.ProcessMessages;

  Doc := wbBrowser.Document;
  //Doc.Clear; // not needed
  Doc.Write(HTMLCode);
  Doc.Close; 
  Doc.DesignMode := 'On';

  WHILE wbBrowser.ReadyState < READYSTATE_INTERACTIVE
  DO Application.ProcessMessages;

  doc.body.style.fontFamily:='Arial';

  ShowMessage(doc.body.outerHTML); // test it
end;

但我认为最好的方法是处理 OnDocumentComplete 你知道你有一个有效 document/body 的地方,然后设置样式或其他需要的东西。