How to implement a 'Lasso' #146

Here's a possible approach:

1. In the OnMouseDown event for the form that you are "lasso-ing" controls on:

bMarquee :=  True;
{set a boolean so that you can differentiate between decisions that
might have to be made during other mouse events}
ptOrigin := Point(X, Y);  // get the starting point of the marquee
ptMove  := Point(X, Y);   // initialize the stopping point</div>

Set the pen and brush attributes here or by calling a common procedure that can be reused elsewhere in the unit.

Pen.Color := clBlack;
Pen.Width := 1;
Pen.Style := psDash;
Brush.Style := bsClear;

Then draw the marquee rectangle:

DrawMarquee(ptOrigin, ptMove, pmNotXor);

2. In the OnMouseMove event for the form:

if bMarquee = True then
begin
  DrawMarquee(ptOrigin, ptMove, pmNotXor);
  DrawMarquee(ptOrigin, Point(X, Y), pmNotXor);
  ptMove := Point(X, Y);
  Canvas.Pen.Mode := pmCopy;
end;

3. In the OnMouseUp event for the form:

if bMarquee = True then
begin
  bMarquee := False;
  DrawMarquee(ptOrigin, Point(X, Y), pmNotXor);
  ptMove := Point(X, Y);
  {check for any intersections between the marquee frame and controls}
  {call the procedure that will highlight (focus) the desired controls}
end;

The DrawMarquee procedure:

procedure myForm.DrawMarquee(mStart, mStop: TPoint; AMode: TPenMode);
begin
  Canvas.Pen.Mode := AMode;
  Canvas.Rectangle(mStart.X, mStart.Y, mStop.X, mStop.Y);
end;
Original resource: The Delphi Pool
Author: Unknown
Added: 2009/11/06
Last updated: 2009/11/06