Delphi:TPanel 和文本缩进

标签 delphi text colors panel indentation

enter image description here 我怎样才能使 View 像黄色矩形一样。使用 TPanel + 颜色?如果是的话,文本从左侧缩进怎么样?

感谢您的帮助和建议!

最佳答案

最简单的方法是使用TPanel。将 ParentBackground 设置为 false,将 BevelOuter 设置为 bvNone,将 Font.Color 设置为 >clWhiteFont.Style[fsBold]Color 为您想要的背景颜色。然后只需在 Caption 属性中的文本前面放置一两个空格,例如 ' 这是一个普通的 TPanel。'

Screenshot

更优雅的解决方案是编写自定义控件。这真的很容易。示例:

unit CaptionBar;

interface

uses
  Windows, SysUtils, Classes, Controls, Graphics;

type
  TCaptionBar = class(TCustomControl)
  private
    FColor: TColor;
    FCaption: TCaption;
    FEllipsis: boolean;
    FIndent: integer;
    procedure SetCaption(const Value: TCaption);
    procedure SetColor(const Value: TColor);
    procedure SetEllipsis(const Value: boolean);
    procedure SetIndent(const Value: integer);
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Font;
    property Anchors;
    property Align;
    property Caption: TCaption read FCaption write SetCaption;
    property Color: TColor read FColor write SetColor default clSkyBlue;
    property Ellipsis: boolean read FEllipsis write SetEllipsis default true;
    property Indent: integer read FIndent write SetIndent default 4;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Rejbrand 2009', [TCaptionBar]);
end;

{ TCaptionBar }

constructor TCaptionBar.Create(AOwner: TComponent);
begin
  inherited;
  FIndent := 4;
  FColor := clSkyBlue;
  FEllipsis := true;
end;

procedure TCaptionBar.Paint;
const
  Ellipsis: array[boolean] of cardinal = (0, DT_END_ELLIPSIS);
var
  r: TRect;
begin
  inherited;
  Canvas.Brush.Color := FColor;
  Canvas.FillRect(ClientRect);
  r := ClientRect;
  r.Left := r.Left + FIndent;
  Canvas.Font.Assign(Font);
  DrawText(Canvas.Handle,
    PChar(FCaption),
    length(FCaption),
    r,
    DT_SINGLELINE or DT_LEFT or DT_VCENTER or Ellipsis[FEllipsis]);
end;

procedure TCaptionBar.SetCaption(const Value: TCaption);
begin
  if not SameStr(FCaption, Value) then
  begin
    FCaption := Value;
    Invalidate;
  end;
end;

procedure TCaptionBar.SetColor(const Value: TColor);
begin
  if FColor <> Value then
  begin
    FColor := Value;
    Invalidate;
  end;
end;

procedure TCaptionBar.SetEllipsis(const Value: boolean);
begin
  if FEllipsis <> Value then
  begin
    FEllipsis := Value;
    Invalidate;
  end;
end;

procedure TCaptionBar.SetIndent(const Value: integer);
begin
  if FIndent <> Value then
  begin
    FIndent := Value;
    Invalidate;
  end;
end;

end.

关于Delphi:TPanel 和文本缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6558956/

相关文章:

delphi - 在Delphi中实现观察者模式的最佳方法

r - R中带有轮廓的文本标签

linux - 如何以跨平台方式将彩色打印到控制台?

python-3.x - 参数 'c' 和 'cmap' 在 matplotlib 散点图中的表现如何?

mysql - delphi:通过互联网连接到托管的 mysql 服务器

delphi - 如何修复 Delphi XE3 迁移错误?

c++ - 在 C++ 中将彩色文本打印到控制台

java - 如何将 pre-wrap 与 swing HTML 文本一起使用?

python - 如何区分两种颜色?

string - Delphi:64 位快速 Pos