TEmbeddedWB and background color

How can I change the background color (clWhite) of a TEmbeddedWB component while no page is displayed?

enter image description here

+3


source to share


2 answers


This can be done by loading the default page when creating the form (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 customize TWebBrowser UI .
This explains how to customize the WB using IOleClientSite

and IDocHostUIHandler

, which also provide default CSS for the object itself .

We can dynamically create a stylesheet that knows about the color and fonts of the form and tell the browser to use it (see the result in part 5 of 6).

Since it TEmbeddedWB

implements IDocHostUIHandler

, you can use its property HostCSS

(you still need to load an empty document):

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;

      

Note that using the property HostCSS

with a CSS style template will also use that template for non CSS styled pages.

+6


source


Easily by setting the property IHTMLDocument2.bgColor

to a specific value from color table

. The following code will jump to a blank page and change the background color to red. Please note that the background color you need to set after each navigation to this blank page with this solution.



uses
  GraphUtil, MSHTML;

procedure TForm1.Button1Click(Sender: TObject);
begin
  EmbeddedWB1.AssignEmptyDocument(True);
  (EmbeddedWB1.Document as IHTMLDocument2).bgColor := ColorToWebColorStr(clRed);
end;

      

+6


source







All Articles