请看如下代码
画线
var
Form1: TForm1;
OrignXY, MoveXY: TPoint;
Drawing:Boolean=false;
implementation
{$R *.DFM}
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
OrignXY:=Point(x,y);
MoveXY:=OrignXY;
With OrignXY, Canvas do
MoveTo(x,y);
with Canvas,MoveXY do
Lineto(x,y);
Drawing:=true;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if Drawing then
begin
With OrignXY,Canvas do
MoveTo(x,y);
with Canvas,MoveXY do
Canvas.LineTo(x,y);
With OrignXY,Canvas do
MoveTo(x,y);
MoveXY:=Point(x,y);
with Canvas,MoveXY do
LineTo(x,y);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Canvas.Pen.Mode:=pmNotXor;
Canvas.Pen.Color:=clRed;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Drawing:=false;
end;
画矩形
var
Form1: TForm1;
StartPt, EndPt : TPoint;
Drawing: Boolean;
implementation
{$R *.DFM}
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
With Canvas do
begin
Pen.Mode := pmNot;
Pen.Color := clBlack;
Brush.Style := bsClear;
end;
StartPt:= Point(x,y);
EndPt:= StartPt;
Drawing:= True;
SetCapture(Handle);
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if Drawing then
begin
Canvas.Ellipse(StartPt.x,StartPt.y,EndPt.x,EndPt.y);
EndPt := Point(x,y);
Canvas.Ellipse(StartPt.x,StartPt.y,EndPt.x,EndPt.y);
end;
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
ReleaseCapture;
Drawing := False;
Canvas.Ellipse(StartPt.x,StartPt.y,EndPt.x,EndPt.y);
With Canvas do
begin
Pen.Mode := pmCopy;
Brush.Color := clBlue;
Brush.Style := bsSolid;
end;
Canvas.Ellipse(StartPt.x,StartPt.y,EndPt.x,EndPt.y);
end;
end.