如何在TWebBrowser中更改字体? [英] How to change font in TWebBrowser?

查看:105
本文介绍了如何在TWebBrowser中更改字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与以下内容有关:哪种方法是在TWebBrowser中加载字符串(HTML代码)的最佳方法?

This question is related to: Which is the best way to load a string (HTML code) in TWebBrowser?

我试图用doc.body更改TWebBrowser中的字体。 style.fontFamily,但没有任何反应。字体仍然是TimesNewRoman。

Iam trying to change font in TWebBrowser with doc.body.style.fontFamily but nothing happens. The font is still 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;


推荐答案

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

You need to let the document be interactive again after you close the document. e.g.:

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 知道您有有效的文档/正文,并设置样式或其他所需的内容。

But I think the best way is to handle the OnDocumentComplete where you know you have a valid document/body, and set the style or what ever else needed.

这篇关于如何在TWebBrowser中更改字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆