Solved

Making a component inherited from TControl and TCustompanel

Posted on 2003-11-26
4
551 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

821 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