1. Computing
RTL referenceGlossary|Tips/Tricks|FREE App/VCL|Best'O'Net|Books|Link To
Creating Custom Delphi Components - Inside and Out
Page 4: Adding propeties to components
 More of this Feature
• Page 1: About components
• Page 2: New...Component
• Page 3: On Fields
• 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."
 Related Resources
• VCL Writing/Enhancing
• Custom Component Dev.
• OOP in Delphi
• Free Source Components

   Adding Properties
The next step is to add properties to the component. We've laid the groundwork so it's simply a matter of bunging them in, like so:

  { Published Declarations } 
  property Direction: TLineDirection read FLineDir write SetLineDir;
  property LineColour: TColor read GetLineColour write SetLineColour;
  property LineWidth: Integer read GetLineWidth write SetLineWidth;

There should be no great surprises in there as you've seen similar property declarations in the previous tutorial part. Now, believe it or not, we've very nearly finished the component!

A quick note about "published": any properties in this section will be available in the Object Inspector at design-time. This is, of course, what we want. If you don't want the properties published (if, for example, you only create the component at run-time) then you can place properties in the "public" section. Properties there will be available to outside code, just like published properties, but will not appear in the Object Inspector. If you see "run-time" properties in the help files, it means they're public.

The next part is pretty fundamental to TLine - its Paint procedure. We need to draw the component, so it's time to add in some code for that. Since our component has TGraphicControl as a base class it gains a Paint procedure. We just need to modify this to draw our code and we're almost there. Add in this code to the component's "Protected" section:

  { Protected declarations }
  procedure Paint; override;

The override part is a fancy bit, and means our procedure will get called instead of the base class's Paint procedure. We can write the good stuff now, namely the drawing procedure. Add this code in after the implementation part:

procedure TLine.Paint;
  start: Integer;

  case FLineDir of
        start := (Height - Canvas.Pen.Width) div 2;
        Canvas.MoveTo(0, start);
        Canvas.LineTo(Width, Start);
        start := (Width - Canvas.Pen.Width) div 2;
        Canvas.MoveTo(start, 0);
        Canvas.LineTo(start, Height);
        Canvas.MoveTo(0, 0);
        Canvas.LineTo(Width, Height);
        Canvas.MoveTo(Width, 0);
        Canvas.LineTo(0, Height);

The only thing you shouldn't understand is the "inherited" bit. Because the Paint procedure belongs to the base class and we're overriding it, we don't want to miss out on important bits the base class does. The base class here might need to do things like set up the canvas to draw on, get device contexts, allocate resources, or whatever. If we don't let the base class do its thing by calling it via "inherited" we might just find our code blows up. Best not to let that happen, eh?

And that, my friend, is enough for a working component. Save the file and go to the Component->Install Component menu, select your file and say OK when it asks if you want to rebuild your library. If all goes well it should tell you that TLine has been registered. You can find it on the Samples palette page and try messing around with it.

The final product

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

 Got some code to share? Got a question? Need some help?

©2014 About.com. All rights reserved.