1. Home
  2. Computing & Technology
  3. Delphi Programming
RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
 
Creating Custom Delphi Components - Inside and Out
Page 2: New...Component
 More of this Feature
• Page 1: About components
• Page 3: On Fields
• Page 4: On Properties
• Page 5: On Constructors
• Page 6: On Destructors and Methods
• Page 7: On Events
• Page 8: Hiding data
• Page 9: Virtual functions and Abstract Classes
 Join the Discussion
"Post your views, comments, questions and doubts to this article."
Discuss!
 Related Resources
• VCL Writing/Enhancing
• Custom Component Dev.
• OOP in Delphi
• Free Source Components

   Creating a Component

Get the source code here: Line.zip (1Kb)

We'll start off at the deep end.  We're going to create a simple component, called TLine.  It will be a graphical control that will draw a line in one of four directions.  It won't do anything fancy, but it will deal with properties, drawing and other useful stuff.

   How Do We Start?
Every component needs a base class.  This class is simply the "Big Daddy" object for it, from which the component takes all properties, events and methods.  For example, if you were writing a new type of button you might make the base class "TButton".  Your new control would have all the TButton's properties (like caption, etc), all the Button's base class's stuff, and so on.

There are lots of possible base classes.  Here's a small selection:

Type of component Recommended Base Class
Generic, non-visual component TComponent
Lightweight visual component TGraphicControl
Visual component that needs focus TCustomControl
Form-type object TCustomForm

This is, of course, a small snippet of the possible cases you might need.  We're interested in a small visual control, so TGraphicControl is the best base class for us.  There's no need for TCustomControl because our component won't need window focus.

We can get started now.  Go to File->New and select component.  You'll be presented with a dialogue box.  Fill it out like this:

The new component dialogue box

The only two things you need to fill out are the "Ancestor type" and "Class name" boxes.  The rest get filled out automatically.  Once you've done that,  click the OK button and you get transported to the code editor with a skeleton class written out.

First we need to let the compiler know what we'll be using.  Each unit (a file, usually containing a class) has a "uses" clause.  This lists what other files will be needed to compile the unit.  Without this the compiler would have a really hard job so it's up to us to make sure it's correct.

Note:
The following section about adding Graphics to the uses clause is relevant for Delphi 6 (and possibly Delphi 5).  Delphi 4 adds Graphics automatically so if you start a new component and the uses clause has "graphics" in it then you don't need to do the next part.

You usually don't notice the "uses" clause as it's filled out with a large number of units by default (such as classes, sysutils, etc.).  These units include most of the functionality you require.  However, sometimes you have to add your own units (which usually listed in the help files for the appropriate class/procedure/function/type).  We'll be needing the type TColor, which represents a colour.  If you look this up in the help file you'll see it's within the 'Graphics' unit.  Let's add that now before we start.  Go to the very top of the file and find the uses part.  It will look like this:

// <-- name of file without .pas
unit Line;  

interface

uses
  Windows, Messages, SysUtils, Classes, Controls

You can see that each unit needed is simply separated by a comma. We'll add Graphics to the uses unit. Make this change:

uses
  Windows, Messages, SysUtils, 
  Classes, Controls, Graphics; //(note added unit)

   Question, Suggestions...
If you have any questions or comments to this (huge) article, please post them on the Delphi Programming Forum. Discuss!

Next page > On Fields > Page 1, 2, 3, 4, 5, 6, 7, 8, 9

All graphics (if any) in this feature created by Zarko Gajic.

 More Delphi
· Learn another routine every day - RTL Quick Reference.
· Download free source code applications and components.
· Talk about Delphi Programming, real time.
· Link to the Delphi Programming site from your Web pages.
· Tutorials, articles, tech. tips by date: 2001|2000|1999|1998 or by TOPIC.
· NEXT ARTICLE: GDI Graphics In Delphi.
From simple lines to direct API calls: the ultimate tutorial to GDI graphics in Delphi. This tutorial will introduce you to some of the techniques in the GDI Delphi drawing. Look for: drawing lines and shapes, drawing pictures, flicker-free drawings, off-screen bitmaps, GDI drawings the API way...
 Stay informed with all new and interesting things about Delphi (for free).
Subscribe to the Newsletter
Name
Email

 Got some code to share? Got a question? Need some help?
Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.