{******************************************************************************} { } { NeoNumLED V0.3 模拟数字 LED 显示 } { } { 作者:mosane 2005-04-02 } { 本着相互学习而来,请大家指教:mosane@163.com } { } { Copyright 2005(C) gdone. } { } {******************************************************************************} unit NeoNumLED; interface uses Windows, Controls, Graphics, Classes, ExtCtrls, SysUtils; type TNeoNumLED = class(TGraphicControl) private { Private declarations } FOffsetX: Integer; FOffsetY: Integer; FWordWidth: Integer; FWordHeight: Integer; FThick: Integer; FSpace: Integer; FText: String; FBGColor: TColor; FLightColor: TColor; FDarkColor: TColor; FAutoSize: Boolean; FTransparent: Boolean; OriginX: Integer; OriginY: Integer; d: array [0..6, 0..5] of TPoint; LED: array [0..10] of String; procedure SetOffsetX(const Value: Integer); procedure SetOffsetY(const Value: Integer); procedure SetWordWidth(const Value: Integer); procedure SetWordHeight(const Value: Integer); procedure SetThick(const Value: Integer); procedure SetSpace(const Value: Integer); procedure SetText(const Value: String); procedure SetBGColor(const Value: TColor); procedure SetLightColor(const Value: TColor); procedure SetDarkColor(const Value: TColor); procedure SetAutoSize2(const Value: Boolean); procedure MakeMatrix; procedure Draw; protected { Protected declarations } procedure Paint; override; public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Published declarations } property OffsetX: Integer read FOffsetX write SetOffsetX default 4; property OffsetY: Integer read FOffsetY write SetOffsetY default 4; property WordWidth: Integer read FWordWidth write SetWordWidth default 17; property WordHeight: Integer read FWordHeight write SetWordHeight default 29; property Thick: Integer read FThick write SetThick default 3; property Space: Integer read FSpace write SetSpace default 4; property Text: String read FText write SetText; property BGColor: TColor read FBGColor write SetBGColor default $004A424A; property LightColor: TColor read FLightColor write SetLightColor default $0000FFF7; property DarkColor: TColor read FDarkColor write SetDarkColor default $00636363; property AutoSize: Boolean read FAutoSize write SetAutoSize2 default True; property ShowHint; property Visible; property PopupMenu; property OnClick; property OnDblClick; property OnMouseDown; property OnMouseUp; end; procedure Register; implementation procedure Register; begin RegisterComponents('NeoSoft', [TNeoNumLED]); end; constructor TNeoNumLED.Create(AOwner : TComponent); begin inherited; Width := 47; Height := 38; FOffsetX := 4; FOffsetY := 4; FThick := 3; FWordWidth := 17; FWordHeight := 29; FSpace := 4; FText := '00'; FBGColor := $004A424A; FLightColor := $0000FFF7; FDarkColor := $00636363; FAutoSize := True; FTransparent := False; LED[0] := '012345'; LED[1] := '12'; LED[2] := '01643'; LED[3] := '01623'; LED[4] := '5612'; LED[5] := '05623'; LED[6] := '054326'; LED[7] := '012'; LED[8] := '0123456'; LED[9] := '650123'; LED[10] := '6'; end; destructor TNeoNumLED.Destroy; begin inherited; end; procedure TNeoNumLED.Paint; begin inherited; Draw; end; procedure TNeoNumLED.Draw; var MemImage: TImage; i: Integer; j: Integer; begin if not Visible then Exit; OriginX := 0; OriginY := 0; MemImage := TImage.Create(Self); MemImage.Width := Width; MemImage.Height := Height; with MemImage.Canvas do begin Brush.Color := FBGColor; FillRect(ClipRect); for i := 1 to Length(Text) do begin if i = 1 then begin Inc(OriginX, FOffsetX); Inc(OriginY, FOffsetY); end else Inc(OriginX, FWordWidth + FSpace); MakeMatrix; if FDarkColor <> FBGColor then begin Brush.Color := FDarkColor; Pen.Color := FDarkColor; for j := 1 to Length( LED[8] ) do Polygon( d[ StrToInt( LED[8][j] ) ] ); end; if (Text[i] <> ' ') and (FLightColor <> FBGColor) then begin Brush.Color := FLightColor; Pen.Color := FLightColor; if Text[i] = '-' then begin for j := 1 to Length( LED[10] ) do Polygon( d[ StrToInt( LED[10][j] ) ] ); end else if Text[i] in ['0'..'9'] then begin for j := 1 to Length( LED[ StrToInt( Text[i] ) ] ) do Polygon( d[ StrToInt( LED[ StrToInt( Text[i] ) ][j] ) ] ); end; end; end; end; Canvas.Draw(0, 0, MemImage.Picture.Graphic); MemImage.Free; if FAutoSize and (Width <> FWordWidth * Length(Text) + FSpace * (Length(Text) - 1) + OffsetX * 2 + 1) then begin Width := FWordWidth * Length(Text) + FSpace * (Length(Text) - 1) + OffsetX * 2; Height := FWordHeight + OffsetY * 2; end; end; procedure TNeoNumLED.MakeMatrix; begin d[0, 0] := Point(OriginX + 2, OriginY); d[0, 1] := Point(OriginX + FThick + 1, OriginY + FThick - 1); d[0, 2] := Point(OriginX + FWordWidth - FThick - 2, OriginY + FThick - 1); d[0, 3] := Point(OriginX + FWordWidth - 3, OriginY); d[0, 4] := d[0, 3]; d[0, 5] := d[0, 3]; d[1, 0] := Point(OriginX + FWordWidth - 1, OriginY + 1); d[1, 1] := Point(OriginX + FWordWidth - FThick, OriginY + FThick); d[1, 2] := Point(OriginX + FWordWidth - FThick, OriginY + (FWordHeight - 1) div 2 - FThick); d[1, 3] := Point(OriginX + FWordWidth - 1, OriginY + (FWordHeight - 1) div 2 - 1); d[1, 4] := d[1, 3]; d[1, 5] := d[1, 3]; d[2, 0] := Point(OriginX + FWordWidth - 1, OriginY + (FWordHeight - 1) div 2 + 1); d[2, 1] := Point(OriginX + FWordWidth - FThick, OriginY + (FWordHeight - 1) div 2 + FThick); d[2, 2] := Point(OriginX + FWordWidth - FThick, OriginY + FWordHeight - FThick - 1); d[2, 3] := Point(OriginX + FWordWidth - 1, OriginY + FWordHeight - 2); d[2, 4] := d[2, 3]; d[2, 5] := d[2, 3]; d[3, 0] := Point(OriginX + FWordWidth - 3, OriginY + FWordHeight - 1); d[3, 1] := Point(OriginX + FWordWidth - FThick - 2, OriginY + FWordHeight - FThick); d[3, 2] := Point(OriginX + FThick + 1, OriginY + FWordHeight - FThick); d[3, 3] := Point(OriginX + 2, OriginY + FWordHeight - 1); d[3, 4] := d[3, 3]; d[3, 5] := d[3, 3]; d[4, 0] := Point(OriginX, OriginY + FWordHeight - 2); d[4, 1] := Point(OriginX + FThick - 1, OriginY + FWordHeight - FThick - 1); d[4, 2] := Point(OriginX + FThick - 1, OriginY + (FWordHeight - 1) div 2 + FThick); d[4, 3] := Point(OriginX, OriginY + (FWordHeight - 1) div 2 + 1); d[4, 4] := d[4, 3]; d[4, 5] := d[4, 3]; d[5, 0] := Point(OriginX, OriginY + (FWordHeight - 1) div 2 - 1); d[5, 1] := Point(OriginX + FThick - 1, OriginY + (FWordHeight - 1) div 2 - FThick); d[5, 2] := Point(OriginX + FThick - 1, OriginY + FThick); d[5, 3] := Point(OriginX, OriginY + 1); d[5, 4] := d[5, 3]; d[5, 5] := d[5, 3]; d[6, 0] := Point(OriginX + FThick, OriginY + (FWordHeight + 1) div 2 - FThick + 1); d[6, 1] := Point(OriginX + 2, OriginY + (FWordHeight + 1) div 2 - 1); d[6, 2] := Point(OriginX + FThick, OriginY + (FWordHeight + 1) div 2 + FThick - 3); d[6, 3] := Point(OriginX + FWordWidth - FThick - 1, OriginY + (FWordHeight + 1) div 2 + FThick - 3); d[6, 4] := Point(OriginX + FWordWidth - 3, OriginY + (FWordHeight + 1) div 2 - 1); d[6, 5] := Point(OriginX + FWordWidth - FThick - 1, OriginY + (FWordHeight + 1) div 2 - FThick + 1); if FThick = 1 then begin d[6, 0] := Point(d[6, 0].X + 1, d[6, 0].Y - 1); d[6, 2] := Point(d[6, 2].X + 1, d[6, 2].Y + 1); d[6, 3] := Point(d[6, 3].X - 1, d[6, 3].Y + 1); d[6, 5] := Point(d[6, 5].X - 1, d[6, 5].Y - 1); end; end; procedure TNeoNumLED.SetOffsetX(const Value: Integer); begin if FOffsetX <> Value then begin FOffsetX := Value; Invalidate; end; end; procedure TNeoNumLED.SetOffsetY(const Value: Integer); begin if FOffsetY <> Value then begin FOffsetY := Value; Invalidate; end; end; procedure TNeoNumLED.SetWordWidth(const Value: Integer); begin if (FWordWidth <> Value) and (FThick * 2 < Value) then begin FWordWidth := Value; Invalidate; end; end; procedure TNeoNumLED.SetWordHeight(const Value: Integer); begin if (FWordHeight <> Value) and (FThick * 4 - 1 < Value) then begin if (Value - FThick * 4 + 1) mod 2 = 0 then FWordHeight := Value else FWordHeight := Value + 1; Invalidate; end; end; procedure TNeoNumLED.SetThick(const Value: Integer); begin if (FThick <> Value) and (FWordWidth > Value * 2) and (FWordHeight > Value * 4 - 1) and ((FWordHeight - Value * 4 + 1) mod 2 = 0) then begin FThick := Value; Invalidate; end; end; procedure TNeoNumLED.SetSpace(const Value: Integer); begin if FSpace <> Value then begin FSpace := Value; Invalidate; end; end; procedure TNeoNumLED.SetText(const Value: String); begin if FText <> Value then begin FText := Value; Draw; end; end; procedure TNeoNumLED.SetBGColor(const Value: TColor); begin if FBGColor <> Value then begin FBGColor := Value; Invalidate; end; end; procedure TNeoNumLED.SetLightColor(const Value: TColor); begin if FLightColor <> Value then begin FLightColor := Value; Invalidate; end; end; procedure TNeoNumLED.SetDarkColor(const Value: TColor); begin if FDarkColor <> Value then begin FDarkColor := Value; Invalidate; end; end; procedure TNeoNumLED.SetAutoSize2(const Value: Boolean); begin if FAutoSize <> Value then begin FAutoSize := Value; if FAutoSize and (Width <> FWordWidth * Length(Text) + FSpace * (Length(Text) - 1) + OffsetX * 2) then begin Width := FWordWidth * Length(Text) + FSpace * (Length(Text) - 1) + OffsetX * 2; Height := FWordHeight + OffsetY * 2; end; end; end; end.