Link to home
Start Free TrialLog in
Avatar of Sam80
Sam80

asked on

Making a component inherited from TControl and TCustompanel

Hi ,  I wanna make my own component that should have the functions as follow:
contains a image component to display image
contains a label for display text
when mouse move on & move out ,change the background color

What I have done:
1.try to write a component inherited from TControl.
2. use Tcontrolcanvas to change the background color in the mouse move and mouse out event(in the paint api message)
  but I can not use insertcontrol to insert the image ,panel ,label in my Tcontrol type component ,there has no insertcontrol method but insertcomponent .

and I change the component parent class to TCustompanel
1. inserting the panel ,image, label  successful by insertcontrol method
2. but the color does not change when I active the mouse on and mouse out event

Anyone has the idea to create this component ??
With Details is better

Regards,
Sam
Avatar of andrewjb
andrewjb
Flag of United Kingdom of Great Britain and Northern Ireland image

Roughly:

Derive from TCustomControl - it's more helpful

class TMyControl = class(TCustomControl)
private:
  fImage : TImage;
  fLabel : TLabel;
public
  constructor Create( TComponent pOwner );
end;


constructor TMyControl.Create( pOwner : TComponent )
{
  inherited;
  fLabel := TLabel.Create( Self );
fLabel.Parent := Self;
fLabel.Width := 50; // etc

fImage := TImage.Create( Self );
fImage.Parent := Self;
// Set size etc.
}

Then just set 'Color' to change you background color, I think, and you've got fLabel and fImage components to play with.

Avatar of DeerBear
DeerBear

Hi,

Use TCustomControl as it's been told you and use composition inheritance.
Plus, I would use a factory pattern, in order to allow further customization of the
whole component.

Something like this:

unit uMyComponent;

uses Classes, Controls, StdCtrls;

Type

  TCustomMyComponent = class( TCustomControl )
  private
     FImage : TImage;
     FPanel : TCustomPanel;
  protected
     function CreateImage : TImage;virtual;abstract;
     function CreatePanel : TCustomPanel;virtual;abstract;
  public
     constructor Create( AOwner : TComponent );override;
     destructor Destroy;override;
  end;

implementation

constructor TCustomMyComponent.Create;
begin
  Inherited;
  FImage := CreateImage;
  FPanel := CreatePanel;
end;

destructor TCustomMyComponent.Destroy;
begin
  FPanel.Free;
  FImage.Free;
  inherited;
end;

end.

Then you add a "real" CreateImage and CreatePanel overrided in a descendant class and you're done :-)

HTH,

Andrew
Avatar of Sam80

ASKER

Thanks ,guys.
Andrew , you mean I should write the code in function createimage to setsize or set position the Flabel or FImage?

function createimage:Timage
var
img:Timage;
begin
img:=Timage.create(self);
img.parent:=self;
img.width=50 //etc
result := img ;
end;

Is that like above ?
I cant figure out what is the " virtual;abstract; " , can you tell me a little bit more ?

ASKER CERTIFIED SOLUTION
Avatar of DeerBear
DeerBear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial