Re: re-jig two classes

Posted by Willy on March 31, 2007

In Reply to re-jig two classes posted by Donald on March 27, 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.
: The end result is that the clickable area is only as thick as the line and follows it's contour. Also, which is essential, I can detect a mouse click on any segment and know which.
: My problem is, when I click on a segment I cannot tell which wire it belongs to and am not too sure about the sender use.
-------------------

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)
private
...
FWire: TWire; // added
...
public
...
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;
...
begin
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;
end;
...
end;

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);
var
Wire: TWire;
begin
... 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));
end;

Related articles

       

Follow Ups