這二個功能很容易啊!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, Spin, ExtCtrls;
type
TMydrawTool=(dtLine,dtRectangle,dtEllipse,dtRoundRect,dtPolygon);
TForm1 = class(TForm)
ScrollBox1: TScrollBox;
Image1: TImage;
StatusBar1: TStatusBar;
SpinEdit1: TSpinEdit;
Label1: TLabel;
procedure lineClick(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCreate(Sender: TObject);
procedure SpinEdit1Change(Sender: TObject);
private
{ Private declarations }
public
procedure drawshape( topleft, bottomright:Tpoint;AMode:TpenMode);
end;
var
Form1: TForm1;
MydrawTool: TMydrawTool;
drawing:boolean;
origin,movept:Tpoint;
implementation
{$R *.dfm}
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
drawing:=true;
image1.Canvas.MoveTo(x,y);
origin:=point(x,y);
movept:=origin;
statusbar1.Panels[1].Text:=format('origin
%d,%d)',[x,y]);
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if drawing then
begin
drawshape(origin,point(x,y),pmCopy);
drawing:=false;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
bitmap:Tbitmap;
begin
bitmap:=nil;
try
bitmap:=tbitmap.Create ;
bitmap.Width :=800;
bitmap.Height:=800;
image1.Picture.Graphic:=bitmap;
finally
bitmap.Free ;
end;
end;
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if drawing then
begin
drawshape(origin,movept,pmNotXor);
movept:=point(x,y);
drawshape(origin,movept,pmNotXor);
end;
statusbar1.Panels[2].Text:=format('current
%d.%d)',[x,y]);
statusbar1.Panels[0].Text:='你正在畫你所喜愛的圖形';
end;
procedure TForm1.drawshape(topleft, bottomright: Tpoint; AMode: TpenMode);
begin
with image1.Canvas do
begin
pen.Mode:=AMode ;
case MydrawTool of
dtLine:
begin
image1.Canvas.MoveTo( topleft.X ,topleft.Y);
image1.Canvas.LineTo(bottomright.X,bottomright.Y);
end;
dtRectangle:
begin
image1.Canvas.Rectangle(topleft.X,topleft.Y,bottomright.X,bottomright.Y);
end;
dtEllipse:
begin
image1.Canvas.Ellipse (topleft.X , topleft.Y,bottomright.X,bottomright.Y);
end;
dtRoundRect:
begin
image1.Canvas.RoundRect(topleft.X,topleft.Y,bottomright.X,bottomright.Y,(topleft.X -bottomright.X )div 2,(topleft.Y -bottomright.Y)div 2);
end;
dtpolygon:
begin
image1.Canvas.Polygon ([Point(topleft.X,bottomright.X),TopLeft,BottomRight]);
end;
end;
end;
end;
procedure TForm1.lineClick(Sender: TObject);
begin
MydrawTool:=dtLine;
statusbar1.Panels[0].Text:='你選擇了直線';
end;
procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
image1.Canvas.Pen.Width:=spinedit1.Value ;
end;
end.
你自己慢慢看吧。。。可以實現你要的功能