Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TEmbeddedWB and background color

How i can change the defult backgound color (clWhite) of a TEmbeddedWB component while is not showing any page?

enter image description here

like image 375
Salvador Avatar asked Dec 05 '25 22:12

Salvador


1 Answers

That could be done by loading a default page when the form is created (The quick way):

function ColorToHTML(const Color: TColor): string;
var
  ColorRGB: Integer;
begin
  ColorRGB := ColorToRGB(Color);
  Result := Format('#%0.2X%0.2X%0.2X',
    [GetRValue(ColorRGB), GetGValue(ColorRGB), GetBValue(ColorRGB)]);
end;

WebBrowser1.Navigate(Format('about:<body bgcolor="%s" style="overflow:hidden"/>', [ColorToHTML(clRed)]));

Or the more common way (TWebBrowser/TEmbeddedWB):

uses ActiveX, MSHTML;
procedure LoadDocFromString(ABrowser: TWebBrowser; const HTMLString: WideString);
var
  v: OleVariant;
  HTMLDocument: IHTMLDocument2;
begin
  if not Assigned(ABrowser.Document) then
  begin
    ABrowser.Navigate('about:blank');
    while ABrowser.ReadyState <> READYSTATE_COMPLETE do
      Application.ProcessMessages;
  end;
  HTMLDocument := ABrowser.Document as IHTMLDocument2;
  v := VarArrayCreate([0, 0], varVariant);
  v[0] := HTMLString;
  HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
  HTMLDocument.Close;
end;

LoadDocFromString(WebBrowser1, Format('<body style="background-color:%s; scrollbar-base-color:%s;"/>',
  [ColorToHTML(clGray), ColorToHTML(clBlack)]));

TEmbeddedWB specific:

EmbeddedWB1.LoadFromString(Format('<body style="background-color:%s; scrollbar-base-color:%s;"/>',
    [ColorToHTML(clGray), ColorToHTML(clBlack)]));

Edit: Take a look at this tutorial: How to customise the TWebBrowser user interface.
This explains how to customize WB using IOleClientSite and IDocHostUIHandler which also provide a default CSS to the browser object itself.

We can dynamically create a style sheet that knows about a form's colour and fonts and tell the browser to use it (Look at the result at part 5 of 6).

Since TEmbeddedWB implements IDocHostUIHandler you can use it's HostCSS property (You still need to load a blank document though):

procedure TForm1.Button1Click(Sender: TObject);
const
  // Template for default CSS style
  cCSSTplt = 'body {background-color: %0:s}';
var
  FmtCSS: string;  // Stores default CSS
begin
  FmtCSS := Format(cCSSTplt, [ColorToHTML(clYellow)]);
  EmbeddedWB1.HostCSS := FmtCSS;
  EmbeddedWB1.AssignEmptyDocument;
end;

Please note that using HostCSS property with CSS style template will use this template also for pages with no CSS styling.

like image 198
kobik Avatar answered Dec 07 '25 18:12

kobik