Question

When I dock a TForm to a TTabSheet the form has a gray background color. The tab sheet on the other hand has a white background color.

It gets more complicated when theming is disabled (e.g. classic Windows theme).

With the current code the grey form has a white border which is pretty ugly.

So how do I set the form background color to the tab sheet background color? In case this doesn't work: How do I set the tab sheet background color to the forms background color?

It should work with runtime themes enabled and runtime themes disabled.

program Project1;

uses
  Graphics,
  Controls,
  Forms,
  ComCtrls;

{$R *.res}

var
  Main        : TForm;
  Sub         : TForm;
  PageControl : TPageControl;
  TabSheet    : TTabSheet;
begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm, Main);
  Application.CreateForm(TForm, Sub);
  PageControl        := TPageControl.Create(Main);
  PageControl.Parent := Main;
  PageControl.Align  := alClient;
  TabSheet             := TTabSheet.Create(Main);
  TabSheet.PageControl := PageControl;
  Sub.Dock(TabSheet, TabSheet.ClientRect);
  Sub.Align := alClient;
  Sub.Show;
  // Sub.Color := clWhite; // TabSheet.Color;
  Application.Run;
end.
Was it helpful?

Solution

I found this workaround

type
  TWinControlAccess = class(TWinControl)
  end;

procedure TMainForm.CreateEmbedded(FormClass: TFormClass; Parent: TWinControl);
var
  form: TForm;
begin
  form := FormClass.Create(Self);
  form.Align := alClient;
  form.BorderIcons := [];
  form.BorderStyle := bsNone;
  form.Parent := Parent;
  TWinControlAccess(form).ParentBackground := True; // <<<-
  form.Show;
end;

OTHER TIPS

Head to this Blog post entitled Theming Owner-Drawn Tabs by Chris Rolliston.


Quotes:

The problem: say you want to customise the font colour of a tab on a tab or page control. Back in the day, this was simple — set OwnerDraw to True and provide a handler for OnDrawTab that just sets the font and draws the text. With Windows themes, however, things are not so simple, since setting OwnerDraw to True disables theming for the control. Not a VCL limitation as such, this merely reflects the behaviour of the underlying API control, and if you think about it, it makes good sense from a backwards compatibility point of view. Nonetheless, this is still an issue if you know what you want, which is simply to customise a tab’s caption. Enter, then, TTabControlEx and TPageControlEx, which use the theming API to provide the proper ‘look’ even when OwnerDraw is set to True.


At least, he might have partly solved your issues with his TTabControlEx and/or TPageControlEx components.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top