Delphi:TPanel 和文本缩进

发布于 2024-11-18 08:18:24 字数 134 浏览 6 评论 0原文

在此处输入图像描述 我怎样才能使视图像黄色矩形一样。使用 TPanel + 颜色?如果是的话,文本从左侧缩进怎么样?

感谢您的帮助和建议!

enter image description here
How can I make the view like in the Yellow rectangle. Using TPanel + Color? If yes what about an indent of the text from the left?

Thanks for help and advices!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2024-11-25 08:18:24

最简单的方法是使用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.

The simplest way is to use a TPanel. Set ParentBackground to false, BevelOuter to bvNone, Font.Color to clWhite, Font.Style to [fsBold] and the Color to the background colour you want. Then simply put one or two spaces in front of your text in the Caption property, like ' This is an ordinary TPanel.'.

Screenshot

A more elegant soution is to write a custom control. This is really easy. Example:

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.
并安 2024-11-25 08:18:24

将标签放入面板中,缩进(设置 Left 属性 > 0)并正确设置面板颜色。

Drop a Label into the panel, indented (set the Left property > 0) and set the panel color correctly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文