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.

Mensa Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

Mensa 365 Puzzlers  Calendar 2017

Mensa 365 Puzzlers Calendar 2018

(Hint: If you can wait, current year calendars are usually on sale in January.)


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:

bulletDefine a new class derived from the control.  in this case:
bullet TNozzle = class(TShape)
bulletExpose FOnPaint and Paint methods for the new class
     procedure Paint; override;
bulletPublish the OnPaint event property as a new event to make it accessible to the using program:
bullet published
    property OnPaint: TNotifyEvent read FOnPaint write FOnPaint;
bulletAdd a line to the Paint method to check for assignment and call FOnPaint inherited paint;
bulletprocedure TNozzle.Paint;
    inherited paint; {Let TShape paint do its thing}
    if assigned(FOnPaint) then FonPaint(self); {call our paint event}
bulletThe 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;
bulletconstructor create(NewNozzleId:integer; s:TShape); reintroduce;
bullet 6. Define your own OnPaint TNotifyEvent and assign it to OnPaint as new control is initialized.
bulletprocedure 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 

bullet Download source
bullet Download  executable


Original Date: August 21,2006

Modified: May 15, 2018



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