Board index » delphi » how to create a selection rectangle

how to create a selection rectangle

Hi,

i have a form with some object that i want to select with the mouse.

How is possible to create a selection rectangle ?

I'm new to graphic in Delphi, an example is epreciated :)

thanks

--
Nicchi Roberto
MASTER INFORMATICA
Hardware & Software House
AREZZO, ITALY

 

Re:how to create a selection rectangle


Humm.. let's give a try.
First I created a method to ajust the TRect record. What it does: if right
variable is less than left variable or bottom variable is less than top, the
method inverts the values (This is necessary for calling later
DrawFocusRect):

function AdjustRect(Rect: TRect): TRect;
var
  Temp: Integer;
begin
  //Test if right is less than left
  if Rect.Right < Rect.left then
  begin
    //Stores Rect.Right in a temporary variable to exchange the values
    Temp := Rect.Right;
    Rect.Right := Rect.Left;
    Rect.Left := Temp;
  end;

  //If bottom is less than top
  if Rect.Bottom < Rect.top then
  begin

    //Stores Rect.Bottom in a temporary variable to exchange the values
    Temp := Rect.Bottom;
    Rect.Bottom := Rect.Top;
    Rect.Top := Temp;
  end;

  //Returns the adjust rect
  Result := Rect;
end;

Add three variables inside TForm type
  TForm1 = class(TForm)
  //...Code created by IDE
  public
    Last, Start: TPoint;
    HasLast: Boolean;
  end;

Edit OnMouseDown event for the form. In this event we will store the
coordinates
where the user first clicked on the form as it's where the selection
rectangle will
start from. Note also HasLast variable. It is set to false as in OnMouseMove
later,
the last selection rectangle will not be erased since it has not being draw
yet.

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Start := Point(X, Y);
  HasLast := False;
end;

Edit OnMouseMove event. This event will erase the last selection rectangle
(if
necessary) and draw the new one acoording the where the mouse is.

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  //ONLY works if left button is pressed
  if ssLeft in Shift then
  begin
    //ERASE the last rectangle in case it has draw. The call to AdjustRect
makes
    //sure that Left and Top are not larger than Right and Bottom inverting
then.
    if HasLast then
      Canvas.DrawFocusRect(AdjustRect(Rect(Start.X, Start.Y, Last.X,
Last.Y)));
    //DRAWS the actual selection rectangle using X and Y cursor position
from
    //the parameters and Start.X and Start.Y stored when the user clicked on
the form
    Canvas.DrawFocusRect(AdjustRect(Rect(Start.X, Start.Y, X, Y)));
    //Stores the position for this rectangle as it will have to be erased
later and set
    //variable HasLast to true to tell we have draw a rectangle
    Last := Point(X, Y); HasLast := true;
  end;
end;

Finally, edit OnMouseUp. This method will erase the selection rectangle and
do the
actions you need

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  SelectionRect: TRect;
begin
  if Button = mbLeft then
  begin
    //Now we have the coordinates for the rectangle
    SelectionRect := AdjustRect(Rect(Start.X, Start.Y, Last.X, Last.Y));
    //Erase the rectangle
    if HasLast then
      Canvas.DrawFocusRect(SelectionRect);

    //Now you have the selection the user made inside SelectionRect
structure
    //check all the objects to see if they are inside this selection by
using PTInRect
    //(read in win32 api documentation) and selecting then the method you
need
    //add the code bellow to do this work.
  end;
end;

One final note, you must be wondering how a call to DrawFocusRect may erase
the rectangle since it's also used to draw. It's simple. DrawFocusRect takes
the
pixels in the form and invert their color. A second call to the same
coordinates
will invert again the inverted returning to the default. Read in WIN32API
for more

If you give some more info we could help you more..

"Roberto Nicchi" <softw...@masterinformatica.net> escreveu na mensagem
news:Xns91DDC650CC5AFsoftwaremasterinform@207.105.83.65...

Quote
> Hi,

> i have a form with some object that i want to select with the mouse.

> How is possible to create a selection rectangle ?

> I'm new to graphic in Delphi, an example is epreciated :)

> thanks

> --
> Nicchi Roberto
> MASTER INFORMATICA
> Hardware & Software House
> AREZZO, ITALY

Re:how to create a selection rectangle


"Gustavo Daud" <gubad...@terra.com.br> wrote in news:3ca0ee35_2@dnews:

THANKS!!!

i'll examine your code soon

thanks again

--
Nicchi Roberto
MASTER INFORMATICA
Hardware & Software House
AREZZO, ITALY

Re:how to create a selection rectangle


"Gustavo Daud" <gubad...@terra.com.br> wrote in news:3ca0ee35_2@dnews:

Quote

ok. it does what i want. now i have to understand how it works

thanks for the detailed message.

--
Nicchi Roberto
MASTER INFORMATICA
Hardware & Software House
AREZZO, ITALY

Other Threads