Einzelnen Beitrag anzeigen

Benutzerbild von SirThornberry
SirThornberry
(Moderator)

Registriert seit: 23. Sep 2003
Ort: Bockwen
12.235 Beiträge
 
Delphi 2006 Professional
 
#13

Re: neue Komponente : "TlabeledLabel"

  Alt 29. Mär 2005, 19:56
und hier ist das schmuckstück (sollte selbsterklärend sein):
Delphi-Quellcode:
  TLabelLabel = class(TGraphicControl)
  private
    fAlignment: TAlignment;
    fCaption: String;
    fLeftMargin: Integer;
    fText: String;
    procedure FSetAlignment(AValue: TAlignment);
    procedure FSetStr(AType: Integer; AValue: String);
  public
    constructor Create(AOwner: TComponent); override;
    procedure Paint; override;
  published
    property Alignment: TAlignment read fAlignment write FSetAlignment default taLeftJustify;
    property Caption: String Index 0 read fCaption write FSetStr;
    property Font;
    property Text: String Index 1 read fText write FSetStr;
  end;

[...]

constructor TLabelLabel.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  fAlignment := taLeftJustify;
  fCaption := 'Caption:';
  fLeftMargin := 5;
  fText := 'Text';
end;

procedure TLabelLabel.Paint;
var LCaptionWidth, LTextWidth, LLeft: Integer;
begin
  Canvas.Brush.Style := bsClear;
  Canvas.Font.Assign(Font);
  LCaptionWidth := Canvas.TextWidth(fCaption);
  LTextWidth := Canvas.TextWidth(fText);
  Canvas.TextOut(0, 0, fCaption);
  if (fAlignment = taLeftJustify) or
     (Width < LCaptionWidth + fLeftMargin + LTextWidth) then
    Canvas.TextOut(LCaptionWidth + fLeftMargin, 0, fText)
  else begin
    if fAlignment = taRightJustify then
      Canvas.TextOut(Width - LTextWidth, 0, fText)
    else begin
      LLeft := (Width - LCaptionWidth - fLeftMargin - LTextWidth) div 2;
      Canvas.TextOut(LCaptionWidth + fLeftMargin + LLeft, 0, fText);
    end;
  end;
end;

procedure TLabelLabel.FSetAlignment(AValue: TAlignment);
begin
  if AValue <> fAlignment then
  begin
    fAlignment := AValue;
    Repaint;
  end;
end;

procedure TLabelLabel.FSetStr(AType: Integer; AValue: String);
  procedure LSetVal(var LStr: String);
  begin
    if AValue <> LStr then
    begin
      LStr := AValue;
      Repaint;
    end;
  end;
begin
  if AType = 0 then
    LSetVal(fCaption)
  else if AType = 1 then
    LSetVal(fText);
end;
Jens
Mit Source ist es wie mit Kunst - Hauptsache der Künstler versteht's
  Mit Zitat antworten Zitat