| |
 |
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;
|
|
| Hits/month |
2,500,000+ |
Downloads (Since May 2000) |
7,393,709 |
| Total Files |
6,023 |
| Forum msgs |
7,670 |
| Articles/FAQs |
70+/900+ |
Top Selling Software at Amazon
|