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));
Thank you Willy for your time and effort. It is nice to have the full example and explanation.
Your answer looks to be superb and I aim to try it today.
If I have problems, which I don't foresee with your example, I hope I can come back to you.