| 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 | |||||||||||||||||||
Adding Some Variables
to read:
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:
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):
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... |
|||||||||||||||||||
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). |
|
|
| Got some code to share? Got a question? Need some help? |
