【问题标题】:Custom paint method in TSplitter doesn't use Vcl Styles colorsTSplitter 中的自定义绘制方法不使用 Vcl Styles 颜色
【发布时间】:2012-03-29 20:05:05
【问题描述】:

我正在使用此链接TSplitter enhanced with grab bar 中发布的代码,在拆分器控件中绘制抓取条,

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;

代码运行良好,但是当我启用 vcl 样式时,用于绘制拆分器和抓取栏的颜色不适合 vcl 样式使用的颜色。

如何使用当前主题的 Vcl 风格颜色绘制 TSplitter?

【问题讨论】:

    标签: delphi delphi-xe2 vcl-styles


    【解决方案1】:

    使用代码(clBtnFace、clBtnHighlight、clBtnShadow)的system color constants不存储vcl样式颜色,您必须使用StyleServices.GetSystemColor函数将这些转换为vcl样式颜色。

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-20
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多