Newsgroup: it.comp.lang.delphi
Titolo: Creazione componente

Ciao a tutti, sto cercando creare un componente composto da 3 led ma ho un
problema. Prima ho creato il componente Led il quale accetta dall'esterno un
ImageList per il blinking : (riporto il codice)
---------------------------------------------------------------------
unit HNLed;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;
type
  THNLed = class(TcustomControl)
  private
    { Private declarations }
    fImage:TPaintBox;
    fTimer:TTimer;
    FImageList:TImageList;
    fCurrImage:Integer;
    FIsON:boolean;
    Procedure TimerOnTimer(Sender:Tobject);
  protected
    { Protected declarations }
    procedure SetImageList(Value:TImageList);
    procedure SetON(Value:Boolean);
    Function  GetON:boolean;
    procedure SetBlinking(Value:Boolean);
    Function  GetBlinking():boolean;
    procedure SetInterval(Value:integer);
    Function  GetInterval:integer;
    Procedure SetBounds(ALeft,ATop, AWidth, AHeight:Integer); override;
    Procedure PaintBoxPaint(Sender:TObject);
  public
    { Public declarations }
    constructor Create(AOwner:TComponent);override;
  published
    { Published declarations }
    property ImageList:TImageList read FImageList write SetImageList;
    property LedON:Boolean read GetON write SetON default False;
    property Interval:Integer read GetInterval write SetInterval default
500;
    property Blink:Boolean read GetBlinking write SetBlinking default False;
  end;
procedure Register;
implementation
procedure Register;
begin
  RegisterComponents('Fiumicino', [THNLed]);
end;
constructor THNLed.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
  { Creo la paintbox}
  FImage:=TPaintBox.Create(self);
  FImage.Parent:= self;
  //Fimage.Width:=65;
  //FImage.Height:=65;
  FImage.Enabled:=False;
  FImage.OnPaint:=PaintBoxPaint;
  {Istanzio il timer }
  FTImer:=TTimer.Create(Self);
  FTimer.OnTimer:=TimerOnTimer;
  FTimer.Enabled:=False;
  FTimer.Interval:=500;
end;
{--------------------------------------------------------}
procedure THNLed.SetBounds (
  ALeft, ATop, AWidth, AHeight: Integer);
begin
  inherited SetBounds (ALeft, ATop, AWidth, AHeight);
  // center the paintbox, if exists
  if Assigned (fImage) then
  begin
    if Assigned (fImageList) then
      fImage.SetBounds (
        (Width - fImageList.Width) div 2,
        (Height - fImageList.Height) div 2,
        fImageList.Width, fImageList.Height)
    else
      fImage.SetBounds (
        Width div 2, Height div 2, 0, 0);
    fImage.Invalidate;
  end;
end;
{--------------------------------------------------------}
procedure THNLed.SetON (Value: Boolean);
begin
 { if value then fCurrImage:=1;
  else fCurrImage:=0;
  FImage.Repaint;}
  FIsOn:=Value;
  if FIsOn then FCurrImage:=1
  else FCurrImage:=0;
  FImage.Repaint
end;
{--------------------------------------------------------}
function THNLed.GetON: Boolean;
begin
  Result:=FIsOn;
end;
{--------------------------------------------------------}
procedure THNLed.SetBlinking (Value: Boolean);
begin
  fTimer.Enabled := Value;
  FIsON:=Value;
  setOn(value);
end;
{--------------------------------------------------------}
function THNLed.GetBlinking: Boolean;
begin
  Result := fTimer.Enabled;
end;
{--------------------------------------------------------}
procedure THNLed.SetInterval (Value: Integer);
begin
  fTimer.Interval := Value;
end;
{--------------------------------------------------------}
function THNLed.GetInterval: Integer;
begin
  Result := fTimer.Interval;
end;
{--------------------------------------------------------}
procedure THNLed.SetImageList (Value: TImageList);
begin
  if fImageList <> Value then
  begin
    fImageList := Value;
    Caption := '';
    // change the position
    SetBounds (Left, Top, Width, Height);
  end;
end;
{--------------------------------------------------------}
procedure THNLed.TimerOnTimer(Sender: TObject);
begin
  if Assigned (fImageList) then
  begin
    // update counter and repaint
    Inc (fCurrImage);
    if fCurrImage >= fImageList.Count then
      fCurrImage := 0;
    fImage.Repaint;
  end;
end;
{--------------------------------------------------------}
procedure THNLed.PaintBoxPaint (Sender: TObject);
begin
  // paintbox OnPaint event handler
  if Assigned (fImageList) then
    fImageList.Draw(fImage.Canvas, 0, 0, fCurrImage);
end;
end.
----------------------------------------------------------------------------
--------
A questo punto creo un altro componente insieme di tre componenti LED, ma
non riesco a capire come farglieli rappresentare... qualcuno puņ aiutarmi?:
unit StationClient;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs,HNLed,ExtCtrls;
type
  THNStation = class(TCustomControl)
  private
  { Private declarations }
  FLedR     : THNLed;
  FLedG     : THNLed;
  FLedY     : THNLed;
  FimListR  : TImageList;
  FimListG  : TImageList;
  FimListY  : TImageList;
  FTimer    : TTimer;
  FCurrImage: Integer;
  Procedure TimerOnTimer(Sender:Tobject);
  protected
  { Protected declarations }
  procedure SetImageListR(Value:TImageList);
  procedure SetImageListG(Value:TImageList);
  procedure SetImageListY(Value:TImageList);
  procedure SetInterval(Value:integer);
  Function  GetInterval:integer;
  procedure SetBlinkingR(Value:Boolean);
  Function  GetBlinkingR():boolean;
  procedure SetBlinkingG(Value:Boolean);
  Function  GetBlinkingG():boolean;
  procedure SetBlinkingY(Value:Boolean);
  Function  GetBlinkingY():boolean;
  Procedure Paint(); override ;
  public
  { Public declarations }
  constructor Create(AOwner:TComponent);override;
  published
  { Published declarations }
  property ImageListG:TImageList read FImListR write SetImageListR;
  property ImageListY:TImageList read FImListG write SetImageListG;
  property ImageListR:TImageList read FImListY write SetImageListY;
  property BlinkR:Boolean read GetBlinkingR write SetBlinkingR default
False;
  property BlinkG:Boolean read GetBlinkingG write SetBlinkingG default
False;
  property BlinkY:Boolean read GetBlinkingY write SetBlinkingY default
False;
  property Interval:Integer read GetInterval write SetInterval default 500;
  end;
procedure Register;
implementation
constructor THNStation.Create(AOwner:TComponent);
begin
  inherited Create(AOwner);
  { Istanzio i tre led}
  FLedR:=THNLed.Create(self);
  FLedG:=THNLed.Create(self);
  FLedY:=THNLed.Create(self);
  FLedR.Parent :=self;
  FLedG.Parent :=self;
  FLedY.Parent :=self;
  {Istanzio il timer }
  FTimer:=TTimer.Create(Self);
  FTimer.OnTimer:=TimerOnTimer;
  FTimer.Enabled:=False;
  FTimer.Interval:=500;
end;
{.....................................................}
procedure THNStation.SetImageListY (Value: TImageList);
begin
  if fImListY <> Value then
  begin
     FImListY:=Value;
     FLedY.ImageList:= FImListY;
    Caption := '';
    // change the position
    //setBounds (Left, Top, Width, Height);
  end;
end;
{.....................................................}
procedure THNStation.SetImageListR (Value: TImageList);
begin
  if fImListR <> Value then
  begin
    FImListR:=Value;
    FLedR.ImageList:= FImListR;
    Caption := '';
    // change the position
    //SetBounds (Left, Top, Width, Height);
  end;
end;
{.....................................................}
procedure THNStation.SetImageListG (Value: TImageList);
begin
  if FImListG <> Value then
  begin
    FImListG:=Value;
    FLedG.ImageList:= FImListG;
    Caption := '';
    // change the position
    //SetBounds (Left, Top, Width, Height);
  end;
end;
{.....................................................}
procedure THNStation.SetInterval (Value: Integer);
begin
  fTimer.Interval := Value;
end;
{.....................................................}
function THNStation.GetInterval: Integer;
begin
  Result := fTimer.Interval;
end;
{--------------------------------------------------------}
procedure THNStation.SetBlinkingR(Value: Boolean);
begin
   FLedR.Blink:=Value;
end;
{.....................................................}
function THNStation.GetBlinkingR: Boolean;
begin
    Result:=FLedR.Blink;
end;
{--------------------------------------------------------}
procedure THNStation.SetBlinkingG(Value: Boolean);
begin
   FLedG.Blink:=Value;
end;
{.....................................................}
function THNStation.GetBlinkingG: Boolean;
begin
    Result:=FLedG.Blink;
end;
{--------------------------------------------------------}
procedure THNStation.SetBlinkingY(Value: Boolean);
begin
   FLedY.Blink:=Value;
end;
{.....................................................}
function THNStation.GetBlinkingY: Boolean;
begin
    Result:=FLedY.Blink;
end;
{--------------------------------------------------------}
procedure THNStation.TimerOnTimer(Sender: TObject);
begin
 end;
{--------------------------------------------------------}
procedure THNStation.Paint;
begin
   if Assigned (fImListR) then
    end;
{--------------------------------------------------------}
procedure Register;
begin
  RegisterComponents('Fiumicino', [THNStation]);
end;
end.
----------------------------------------------------------------------------
-------
Grazie mille.
Giuseppe

 end;
io farei :
procedure THNStation.Paint;
var
  X, Y, W, H: Integer;  //
begin
   if Assigned (fImListR) then
     if (csDesigning in ComponentState) or (Assigned (fImListR)) then
       with inherited Canvas do
       begin
         X := (Width div 2) - (Size div 2);  // al centro il LerR
         Y := (Height div 2) - (Size div 2);
         W := 30;
         H := 30;
         FLedR.SetBounds(x,y,w,h);  // il setbounds lo promuovo da protected
a public
       end;
end;
... e cosi' via per gli altri led
^_Goblin_^

 non riesco a capire come farglieli rappresentare... qualcuno puņ aiutarmi?:
che intendi x "rappresentarli"?
A

 Cioč quando trascino il componente su una nuova applicazione, i led non si
vedono..penso che nel metodo Paint sia necessario dire all'ogeetto di
disegnare i led, ma non so come..
Grazie.

 ... e cosi' via per gli altri led
..ho un problema di compilazione nella riga X:=... mi si ferma dopo il
secondo 'div' di cendomi che si aspetta una ')' ma che trova DIV. Ho provato
a commentare X e Y e comunque non vedo disegnato il led (magari è
giusto..non so). Sai darmi qualche indicazione?
Grazie ancora.
Giuseppe


Sorry :)
Nel copia/incolla ho dimenticato un pezzo :(
Ho inserito una proprieta' Size
  property Size: Integer read fSize write SetSize default 30;
procedure THNStation.SetSize(const Value: Integer);
begin
  if FSize <> Value then
  begin
    FSize := Value;
    Invalidate;
  end;
end;
e la paint con questa proprietà diventa:
procedure THNStation.Paint;
var
  X, Y, W, H, S: Integer;
begin
   if Assigned (fImListR) then
     if (csDesigning in ComponentState) or (Assigned (fImListR)) then
       with inherited Canvas do
       begin
         X := (Width div 2) - (Size div 2);
         Y := (Height div 2) - (Size div 2);
         W := Size;
         H := Size;
         FLedR.SetBounds(x,y,w,h); // il setbounds del LED lo promuovo da
protected a public
       end;
end;
Il tutto fatto in modo molto veloce e senza test approfonditi :)
^_Goblin_^

 ^_Goblin_^
Grazie ancora.
Giuseppe.

 end;
Ho provato ad inserire il codice ma sembra non funzionare ancora (lo compila
e tutto ok am non vedo i led), eppure entra nella paint ed esegue la
setBound. Mi chiedo se il tipo di oggetto antenato č giusto
(TCustomControl)...
Grazie ancora per il tuo prezioso aiuto.
Giuseppe

 Giuseppe
Niente, ho risolto.. grazie mille ancora.
giuseppe