Solved

Making a component inherited from TControl and TCustompanel

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now