Posted by Donald on March 31, 2007
In Reply to Re: re-jig two classes posted by Willy on March 31, 2007: : I have a zigzag line called TWire (TObject). To avoid the clickable area from being a surrounding rectangle for the whole zizgzag, which would overlap others, I created a second class TWseg (TGraphicControl). I have an array of 6 TWseg within TWire.
: Add a property "Wire" to the TWSeg class, that gets/sets a pointer to the TWire that the TWSeg object belongs to. Example:
: TWSeg = class(TGraphicControl)
: FWire: TWire; // added
: property Wire: TWire read FWire write FWire; // added
: In your application, set the TWSeg.Wire property when you initialize the TWSeg objects, for example:
: procedure TForm1....;
: var Wire1: TWire;
: Wire1 := TWire.Create;
: ... initialize Wire1
: ... finally, create its 6 segments:
: for i := 1 to 6 do begin
: Wire1.WSeg[i] := TWSeg.Create;
: ...initialize the segment:
: // in this example: the same handler for mouse-down event
: // of every segment (of course each segment could have it's own handler(s))
: Wire1.WSeg[i].OnMouseDown := Form1.SegmentMouseDown;
: // store a pointer to Wire1 in the segment:
: Wire1.WSeg[i].Wire := Wire1;
: OnMouseDown is a property of TWSeg, and it's of the type TMouseEvent. So, write an event handler like this:
: procedure TForm1.SegmentMouseDown(Sender: TObject; Button: TMouseButton;
: Shift: TShiftState; X, Y: Integer);
: Wire: TWire;
: ... do whatever you want to do when the mouse button goes down...
: Wire := (Sender as TWSeg).Wire;
: ... use the wire-info, for example:
: ShowMessage("Left of the wire: " + IntToStr(Wire.Left));
Second response. I forgot to ask about the mouse handler. As you see, mine was in TWseg, is it normal to have all mouse handlers in TForm as per your example?
The reason I ask is that I thought I had succeeded in encapsulating by putting it in the class definition. I can't quite work out whether your example would work with my handler, but suspect there might be some 'going in circles'.