Delphi32.com - Home!
| Home/News | Downloads | Forums | D32 Magazine | Resources | Info and Facts |  
 
 Selection rectangle


How can I create a selection rectangle for use with selecting portions of an image for editing?   

    The easiest way to do this is to use the Windows API function DrawFocusRect. The DrawFocusRect function uses the XOR function when drawing, so drawing the rectangle twice in the same location erases the rectangle, and the rectangle will always be visible when drawing over different colors.

    Example:
    type
      TForm  = class(TForm)
        procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
          Y: Integer);
        procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
      private
        { Private declarations }
        Capturing : bool;
        Captured : bool;
        StartPlace : TPoint;
        EndPlace : TPoint;
      public
        { Public declarations }
      end;
    var
      Form : TForm ;
    implementation
    {$R *.DFM}
    function MakeRect(Pt  : TPoint;
                      Pt2 : TPoint) : TRect;
    begin
      if pt .x < pt2.x then begin
        Result.Left := pt .x;
        Result.Right := pt2.x;
      end else begin
        Result.Left := pt2.x;
        Result.Right := pt .x;
      end;
      if pt .y < pt2.y then begin
        Result.Top := pt .y;
        Result.Bottom := pt2.y;
      end else begin
        Result.Top := pt2.y;
        Result.Bottom := pt .y;
      end;
    end;
    procedure TForm .FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      if Captured then
        DrawFocusRect(Form .Canvas.Handle,
                      MakeRect(StartPlace,
                               EndPlace));
      StartPlace.x := X;
      StartPlace.y := Y;
      EndPlace.x := X;
      EndPlace.y := Y;
      DrawFocusRect(Form .Canvas.Handle,
                    MakeRect(StartPlace,
                             EndPlace));
      Capturing := true;
      Captured := true;
    end;
    procedure TForm .FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    begin
      if Capturing then begin
        DrawFocusRect(Form .Canvas.Handle,
                      MakeRect(StartPlace,
                               EndPlace));
        EndPlace.x := X;
        EndPlace.y := Y;
        DrawFocusRect(Form .Canvas.Handle,
                      MakeRect(StartPlace,
                               EndPlace));
      end;
    end;
    procedure TForm .FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
      Capturing := false;
    end;
    

     



  << Previous Faq     Complete List     Next Faq >>  



 
 Hits/month  2,500,000+ 
 Downloads
 (Since May 2000)
 7,393,709 
 Total Files  6,023 
 Forum msgs  7,670 
 Articles/FAQs  70+/900+ 
Kylix
Tips n Tricks
FAQs
Knowledge Base
Bug Listings
Articles
Books
Newsgroups
Links
Submissions
Testimonials
Advertising
Contact Us
About Us
Search Amazon:
Top Selling Software at Amazon

| Home/News | Downloads | Forums | Resources | Info and Facts | Testimonials |
  Site Search:
 


Comments/Problems: Webmaster@delphi32.com
Copyright © 1998-2006, Delphi32.com. All rights reserved.
Terms of Use