Salvador
Salvador

Reputation: 16492

Custom paint method in TSplitter doesn't use Vcl Styles colors

I'm using the code posted in this link TSplitter enhanced with grab bar , to draw a grab bar in a splitter control,

procedure TSplitter.Paint;
var
  R: TRect;
  X, Y: integer;
  DX, DY: integer;
  i: integer;
  Brush: TBitmap;
begin
  R := ClientRect;
  Canvas.Brush.Color := Color;
  Canvas.FillRect(ClientRect);

  X := (R.Left+R.Right) div 2;
  Y := (R.Top+R.Bottom) div 2;
  if (Align in [alLeft, alRight]) then
  begin
    DX := 0;
    DY := 3;
  end else
  begin
    DX := 3;
    DY := 0;
  end;
  dec(X, DX*2);
  dec(Y, DY*2);

  Brush := TBitmap.Create;
  try
    Brush.SetSize(2, 2);
    Brush.Canvas.Brush.Color := clBtnHighlight;
    Brush.Canvas.FillRect(Rect(0,0,1,1));
    Brush.Canvas.Pixels[0, 0] := clBtnShadow;
    for i := 0 to 4 do
    begin
      Canvas.Draw(X, Y, Brush);
      inc(X, DX);
      inc(Y, DY);
    end;
  finally
    Brush.Free;
  end;

end;

the code works nicely but when I enabled the vcl styles, the colors used to draw the the splitter and the grab bar doesn´t fit the used by the vcl styles.

enter image description here

How I can draw the TSplitter using the Vcl style colors of the current theme?

Upvotes: 3

Views: 1153

Answers (1)

RRUZ
RRUZ

Reputation: 136451

The system color constants which uses the code (clBtnFace, clBtnHighlight, clBtnShadow) not store the vcl styles colors, you must use the StyleServices.GetSystemColor function to translate these to the vcl styles colors.

procedure TSplitter.Paint;
var
  R: TRect;
  X, Y: integer;
  DX, DY: integer;
  i: integer;
  Brush: TBitmap;
begin
  R := ClientRect;
  if TStyleManager.IsCustomStyleActive then
    Canvas.Brush.Color := StyleServices.GetSystemColor(clBtnFace)
  else
  Canvas.Brush.Color := Color;

  Canvas.FillRect(ClientRect);

  X := (R.Left+R.Right) div 2;
  Y := (R.Top+R.Bottom) div 2;
  if (Align in [alLeft, alRight]) then
  begin
    DX := 0;
    DY := 3;
  end else
  begin
    DX := 3;
    DY := 0;
  end;
  dec(X, DX*2);
  dec(Y, DY*2);

  Brush := TBitmap.Create;
  try
    Brush.SetSize(2, 2);

    if TStyleManager.IsCustomStyleActive then
      Brush.Canvas.Brush.Color := StyleServices.GetSystemColor(clBtnHighlight)
    else
      Brush.Canvas.Brush.Color := clBtnHighlight;

    Brush.Canvas.FillRect(Rect(0, 0, Brush.Height, Brush.Width));

    if TStyleManager.IsCustomStyleActive then
      Brush.Canvas.Pixels[0, 0] :=  StyleServices.GetSystemColor(clBtnShadow)
    else
      Brush.Canvas.Pixels[0, 0] :=  clBtnShadow;

    for i := 0 to 4 do
    begin
      Canvas.Draw(X, Y, Brush);
      inc(X, DX);
      inc(Y, DY);
    end;
  finally
    Brush.Free;
  end;

end;

Upvotes: 4

Related Questions