Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Making a component inherited from TControl and TCustompanel

Posted on 2003-11-26
4
Medium Priority
?
562 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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