【问题标题】:How to change font in TWebBrowser?如何更改 TWebBrowser 中的字体?
【发布时间】:2017-01-31 12:00:39
【问题描述】:

本题涉及:Which is the best way to load a string (HTML code) 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;

【问题讨论】:

  • 这取决于页面的内容,它的样式。不会有单一的解决方案。你可以控制页面的内容,还是可以任意控制?#
  • 内容是在 TWebBrowser 中创建的,而不是从 HTML 页面导入的。用户将输入粗体、项目符号等基本格式。因此,内容没有、、css等

标签: delphi font-face twebbrowser


【解决方案1】:

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

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 你知道你有一个有效的文档/正文,并设置样式或其他需要的东西。

【讨论】:

  • 就像一个魅力。非常感谢。你真的很擅长 TWebBrowser。
  • 我知道我不应该使用 ProcessMessages。我现在太懒/急于实现 OnDocumentComplete。但我保证我会做到的:)
  • 不客气。 TWebBrowser 正在异步处理和事件驱动。切换到OnDocumentComplete 最终会让你的生活更轻松,并且(希望)没有错误。
猜你喜欢
  • 1970-01-01
  • 2011-06-27
  • 2015-07-03
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 1970-01-01
  • 2020-10-02
相关资源
最近更新 更多