首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Delphi: TPanel和文本缩进

Delphi: TPanel和文本缩进
EN

Stack Overflow用户
提问于 2011-07-03 02:57:01
回答 2查看 2.9K关注 0票数 2

怎样才能使视图像黄色矩形一样。使用TPanel +颜色?如果是,那么从左边开始缩进文本呢?

感谢您的帮助和建议!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-07-03 03:03:57

最简单的方法是使用TPanel。将ParentBackground设置为false,将BevelOuter设置为bvNone,将Font.Color设置为clWhite,将Font.Style设置为[fsBold],将Color设置为所需的背景色。然后,只需在Caption属性中的文本前面放置一个或两个空格,如' This is an ordinary TPanel.'

一种更优雅的方法是编写一个自定义控件。这真的很简单。示例:

代码语言:javascript
复制
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.
票数 5
EN

Stack Overflow用户

发布于 2011-07-03 02:59:06

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6558956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档