delphi - TSplitter 中的自定义绘制方法不使用 Vcl 样式颜色

标签 delphi delphi-xe2 vcl-styles

我正在使用此链接 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 样式使用的颜色。

enter image description here

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

最佳答案

system color constants其中使用的代码(clBtnFace、clBtnHighlight、clBtnShadow)不存储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;

关于delphi - TSplitter 中的自定义绘制方法不使用 Vcl 样式颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9932668/

相关文章:

Delphi:TOpenDialog + VCL 样式会损坏大量文件的文件名

Delphi TChromeTabs 在 Delphi XE 中不工作

Delphi Copyfile 在长文件名上失败(超过 MAX_PATH)

delphi - 如何检查特定用户是否对 Delphi 中的文件夹/文件具有特定访问权限

delphi - 启用VCL样式时如何制作透明表单?

delphi - 获取 EDBEngineError - 操作不适用于 Delphi XE2 中的 ExecSQL 语句

delphi - 古老的 Delphi VCL 应用程序看起来没有 Windows 主题或 VCL 风格

forms - 所有 MDI 表单关闭时发生的事件

delphi - 鼠标拖出控件边界后,控件如何接收鼠标事件?

delphi - 动态创建组件时如何强制VCL样式覆盖?