Paint on Shape

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]



Search WWW


As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.


Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.


Feedback:  Send an e-mail with your comments about this program (or anything else).

Search only



Problem Description

Here is a program that  demonstrates how to add an "OnPaint" event to a TGraphic control which does not have that event defined.  Specifically the problem is to to add an identifying name or number to TShape controls.  


Background & Techniques

I recently became involved in an interesting project to design software to drive nozzles for a children's water fountain.  We do not have the Digital IO card or the solenoids yet, but I decided to write a simulator to  help my buddy define locations and trial "scripts" to drive the fountain.  I am representing the nozzles with TShape controls but needed to identify each with a number.  An OnPaint event exit was the logical way to do this, but TShape does not support that event.

Here is a summary of the steps necessary to add the feature:

  • Define a new class derived from the control.  in this case:
    •  TNozzle = class(TShape)
  • Expose FOnPaint and Paint methods for the new class
    • private
           procedure Paint; override;
  • Publish the OnPaint event property as a new event to make it accessible to the using program:
    •  published
          property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
  • Add a line to the Paint method to check for assignment and call FOnPaint inherited paint;
    • procedure TNozzle.Paint;
          inherited paint; {Let TShape paint do its thing}
          if assigned(FOnPaint) then FonPaint(self); {call our paint event}
  • The new class could be installed as a new component based on the above four steps. I prefer to reintroduce the Create constructor to pass  additional initial property values and to use the existing graphic control as a "prototype" to source the new pertinent properties such as owner, parent, left, top, height, width, shape, color, etc. for the new control. This allows us to use Delphi's IDE to visually define the the characteristics we want.   The prototype control may be freed after transferring its properties to the new control;
    • constructor create(NewNozzleId:integer; s:TShape); reintroduce;
  •  6. Define your own OnPaint TNotifyEvent and assign it to OnPaint as new control is initialized.
    • procedure TForm1.NozzlePaint(Sender: TObject);
        With TNozzle(sender), canvas do
           {Draw NozzleId in center of the nozzle}
           textout(width div 2 -4, height div 2 -4,nozzleId);

A similar technique could be used to customize the appearance of  TLabel or TBevel controls, both TGraphic descendents without an OnPaint event..

March 6, 2017:  A Delphi programmer recently sent me his attempt to move the TNozzle  class to a separate unit and found that the his drawing attempts were displayed in the wrong place.   The solution was to draw on the  canvas of the TNozzle Parent property.  This happened by default when the class existed in the same unit as the parent.  ShapePaint Version 2.0 of demonstrates how to do this.   I also added a "ShowId" Boolean property to TNozzle which can eliminate the need for the previous user OnPaint exit.  It is initialized to True by default.   I left the user exit (Step 6 above) in place and modified it to only draw the nozzle Id when ShowId is false.

Running/Exploring the Program 


Original Date: August 21,2006

Modified: March 15, 2017



  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.