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 3: Adding variables to components
 More of this Feature
• Page 1: About components
• Page 2: New...Component
• 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

   Adding Some Variables
We need to customise this skeleton code to suit our purposes so let's get busy. The first thing to do is to declare a new type. Change the bit that says:

type
  TLine = class(TGraphicControl) 

to read:

type
  TLineDirection = (drLeftRight, 
                    drUpDown, 
                    drTopLeftBottomRight, 
                    drTopRightBottomLeft);

  TLine = class(TGraphicControl) 

That should be fairly self-explanatory.  We declared a new type called TLineDirection, which we can use when drawing to figure out where to draw the line from/to.  Now we add in a few variables (called member variables) into the class itself.  We need to store:

  • Line width

  • Line colour

  • Direction

This is where the whole idea of customisation comes in.  If you want to, you could add in any variables you think are necessary... you could add in code to draw dotted lines, for example.  However, we'll stick with the basics for now.

One thing you should have spotted is that we don't actually need to store the line width and colour, as they are properties of the canvas's pen.  This is very handy.  However, we will need to add in a variable for the direction, as well as get and set functions.  Change the private section of the component to look like this:

private
  { Private declarations }
  FLineDir: TLineDirection;

  function GetLineWidth: Integer;
  function GetLineColour: TColor;
  procedure SetLineWidth(const NewWidth: Integer);
  procedure SetLineColour(const NewColour: TColor);
  procedure SetLineDir(const NewDir: TLineDirection);

Even though we haven't got variables for the line width and colour, we still need functions to set and retrieve the values from the component's canvas. This is because these three values will be properties of TLine, and properties want you to say how they will read/write, remember? Note we don't need a get function for FLineDir because we can just let the property read its value directly from the variable.

The "F" before LineDir is a bit of notation. It stands for "field", and just means the variable belongs to the class. This makes it a bit easier when writing your component to avoid mixing up local variables with class members.

Right, we've declared the functions so I suppose we'd better implement them. Bung this code in after the implementation section (look for the "implementation" keyword in your file and add this immediately after it):

function TLine.GetLineWidth: Integer;
begin
  Result := Canvas.Pen.Width;
end;

function TLine.GetLineColour: TColor;
begin
  Result := Canvas.Pen.Color;
end;

procedure TLine.SetLineWidth(const NewWidth: Integer);
begin
  if NewWidth <> Canvas.Pen.Width then
  begin
    Canvas.Pen.Width := NewWidth;
    Invalidate; // redraws the component
  end;
end;

procedure TLine.SetLineColour(const NewColour: TColor);
begin
  if NewColour <> Canvas.Pen.Color then
  begin
    Canvas.Pen.Color := NewColour;
    Invalidate;
  end;
end;

procedure TLine.SetLineDir(const NewDir: TLineDirection);
begin
  if NewDir <> FLineDir then
  begin
    FLineDir := NewDir;
    Invalidate;
  end;
end;

The get functions are very self-explanatory. They just nab the values from the Canvas and pass them along. However, the set functions are slightly more complex than just setting the variables.

When the set function gets called, we only want to change the appropriate value if necessary. After all, since it's a visual control any unnecessary changes will be apparent to the user (e.g. setting the line direction to the same value again shouldn't cause a redraw). Each of the set functions checks if the new value is different to the current one. If, and only if, it is then we need to change the value and cause a redraw.

   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 Properties > 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

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

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

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

All rights reserved.