Solved

Making a component inherited from TControl and TCustompanel

Posted on 2003-11-26
4
552 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:Sam80
  • 2
4 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 9824372
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.

0
 
LVL 5

Expert Comment

by:DeerBear
ID: 9824624
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
0
 

Author Comment

by:Sam80
ID: 9830399
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 ?

0
 
LVL 5

Accepted Solution

by:
DeerBear earned 50 total points
ID: 9830499
Hi,

Virtual;Abstract is a way to tell the Delphi compiler: "Hey, you shouldn't allow this class to be instantiated, emit a
warning if someone wants to use it!" and at the same time with the "abstract" directive, you tell the
compiler:" Look! This method is not implemented HERE, but will be implemented in descendant classes.
I put it here because in this way you know you can call it when needed."

Thus, you create an abstraction layer on your component, so that if some time in the future you want to
use totally different components, you just derive and create them different... isn't this great?

HTH,

Andrew
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question