procedure TForm1.FormCreate(Sender: TObject);
begin
ed := TLabelOffset.Create(Self);
with ed do
begin
autosize := false;
Parent := Form1;
Left := 00;
Top := 0;
Height := 400;
Width := 500;
caption := 'edit1';
Color := clYellow;
end;
end;
procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
ed.Offset := spinedit1.Value;
self.Paint;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
//不同字符显示字距不同???
//如英文+数字、中文+英文
ed.Caption := edit1.Text;
end;
end.
//-------------------------------------------------------------
unit LabelOffset;
interface
uses
Windows, SysUtils, Classes, Controls, StdCtrls, Graphics;
type
TLabelOffset = class(TLabel)
private
{ Private declarations }
FOffset: Integer;
procedure SetOffset(Value: Integer);
protected
{ Protected declarations }
procedure Paint; override;
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
published
{ Published declarations }
property Offset: Integer read FOffset write SetOffset default 0;
end;
procedure Register;
implementation
{----------------------------------------------------------------------}
var
H: Cardinal = 0;
V: Cardinal = 0;
procedure TLabelOffset.Paint;
const
Alignments: array[TAlignment] of Word = (DT_LEFT, DT_RIGHT, DT_CENTER);
Layout: array[TTextLayout] of Word = (DT_TOP, DT_VCENTER, DT_Bottom);
WordWraps : Array[Boolean] of Word = (0, DT_WORDBREAK);
var
Rect,aRect: TRect;
Flags: Word;
x, y: Integer;
begin
{N:=(Width-canvas.TextWidth(str)) div (length(str)-1);
setTextcharacterExtra(canvas.Handle,N);
Textout(0,0,str);
这样str就占满Width宽度。 }
with Canvas do
begin
ARect := ClientRect;
if not Transparent then
begin
Brush.Color := Self.Color;
Brush.Style := bsSolid;
FillRect(ARect);
end;
Brush.Style := bsClear;
Flags := DT_EXPANDTABS or WordWraps[WordWrap] or Alignments[Alignment];
SetTextCharacterExtra(Handle, FOffset);
{$IfDef Delphi3andHigher}
if Layout <> tlTop then
begin
CalcRect := ARect;
DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags or DT_CALCRECT)
if Layout = tlBottom
then OffsetRect(ARect, 0, (Height - CalcRect.Bottom))
else OffsetRect(ARect, 0, (Height - CalcRect.Bottom) div 2);
end;
{$EndIf Delphi3andHigher}
DrawText(Canvas.Handle, PChar(Text), Length(Text), Rect, Flags);
end;
inherited Paint;
end;
procedure TLabelOffset.SetOffset(Value: Integer);
begin
if Value <> FOffset then
begin
FOffset := Value; {Update the angle in the ObjectInspector}
Invalidate; {Update label}
end;
end;
{----------------------------------------------------------------------}
procedure Register;
begin
RegisterComponents('Samples', [TLabelOffset]);
end;
constructor TLabelOffset.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FOffset := 0; {To start with}
end;
end.