首先在TImage的OnMouseDown里设置矩形框的起始坐标,
然后在OnMouseMove事件里,在Canvas上画框就行了,
不过要注意在画目前矩阵时,要先擦去上一次的。
所以要设置画笔的光栅模式为异或,还要保存每一次
矩形框的顶点坐标。
最后在OnMouseUp里可以得到目标矩形,用CopyRect就能
得到所要的图片部分。
下面是我从前一个程序里的代码。
procedure TForm1.TestCanvaMouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
with TempImagedo
begin
Canvas.Pen.Style:=psSolid;
Canvas.Pen.Color:=CurrentColor;
Canvas.MoveTo(X,Y);
end;
if RectStyle.Checked then
begin
Canvas.Pen.Color:=CurrentColor;
Canvas.Pen.Style:=psDash;
TargetRect.Left:=X;
TargetRect.Top:=Y;
TempRect.Left:=X;
TempRect.Top:=Y;
TempRect.Right:=X;
TempRect.Bottom:=y;
end;
begin
Paint:=true;
end;
procedure TForm1.TestCanvaMouseMove(Sender: TObject;
Shift: TShiftState;
X,
Y: Integer);
begin
if not begin
Paint then
exit;
if LineStyle.Checked then
With TempImagedo
Canvas.LineTo(X,Y);
if RectStyle.Checked then
begin
With TempImagedo
begin
// 擦去前一次的窗口
Canvas.Pen.Mode:=pmNotXor;
//光栅模式为异或
Canvas.MoveTo(TempRect.Left,TempRect.Top);
Canvas.Lineto(TempRect.Right,TempRect.Top);
Canvas.LineTo(TempRect.Right,TempRect.Bottom);
Canvas.LineTo(TempRect.Left,TempRect.Bottom);
Canvas.LineTo(TempRect.Left,TempRect.Top);
//
TempRect.Right:=X;
TempRect.Bottom:=y;
Canvas.MoveTo(TempRect.Left,TempRect.Top);
Canvas.Lineto(TempRect.Right,TempRect.Top);
Canvas.LineTo(TempRect.Right,TempRect.Bottom);
Canvas.LineTo(TempRect.Left,TempRect.Bottom);
Canvas.LineTo(TempRect.Left,TempRect.Top);
end;
end;
TestCanva.OnPaint(self);
end;
procedure TForm1.TestCanvaMouseUp(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
begin
if RectStyle.Checked then
begin
TargetRect.Right:=X;
TargetRect.Bottom:=Y;
with TempImagedo
begin
// Canvas.Pen.Mode:=pmNotXor;
//光栅模式为异或
Canvas.MoveTo(TempRect.Left,TempRect.Top);
Canvas.Lineto(TempRect.Right,TempRect.Top);
Canvas.LineTo(TempRect.Right,TempRect.Bottom);
Canvas.LineTo(TempRect.Left,TempRect.Bottom);
Canvas.LineTo(TempRect.Left,TempRect.Top);
Canvas.Brush.Color:=CurrentColor;
Canvas.FillRect(TargetRect);
Canvas.Pen.Mode:=pmCopy;
//恢复缺省光栅模式
end;
TestCanva.OnPaint(self);
end;
begin
Paint:=false;
end;