我已经找到解决的办法:
var
Form1: TForm1;
EcgFs:TFileStream;
OldPoint:TPoint;
pbx,pbY:Integer;
First:Boolean=True;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
EcgFs:=TFileStream.Create('c:/save.dat',fmOpenReadWrite);
pbx:=3;pby:=10;
end;
procedure TForm1.DrawRect(Point: TPoint; DeltaX, DeltaY: Integer);
begin
with pb,Canvas do
begin
Pen.Style:=psSolid;
Pen.Color:=clYellow;
Pen.Mode:=pmXor;
MoveTo(Point.X-DeltaX,Point.Y-DeltaY);LineTo(Point.X+DeltaX,Point.Y-DeltaY);
LineTo(Point.X+DeltaX,Point.Y+DeltaY);LineTo(Point.X-DeltaX,Point.Y+DeltaY);
LineTo(Point.X-DeltaX,Point.Y-DeltaY);
end;
end;
procedure TForm1.pbClick(Sender: TObject);
var
Point:TPoint;
Index:Integer;
begin
if Not First then
DrawRect(OldPoint,pb.Width div (2*pbx),pb.Height div (2*pby))
else
First:=False;
GetCursorPos(Point);
Point:=pb.ScreenToClient(Point);
Point:=GetPoint(Index,Point,pbx,pby);
Label1.Caption:=IntToStr(Index);
OldPoint:=Point;
DrawRect(Point,pb.Width div (2*pbx),pb.Height div (2*pby));
end;
function TForm1.GetPoint(var Index:Integer;Point: TPoint; xStep, yStep: Integer): TPoint;
var
i:Integer;
DeltX,DeltY,tmpX,tmpY:Integer;
IndexX,IndexY:Integer;
begin
for i:=0 to xStep-2 do
begin
IndexX:=i+1;
if (abs(Point.X-((pb.Width div (2*xStep))+i*(pb.Width div xStep))))
<=(pb.Width div (2*xStep)) then
begin
Result.X:=(pb.Width div (2*xStep))+i*(pb.Width div xStep);
Break;
end
else begin
Result.X:=(pb.Width div (2*xStep))+(i+1)*(pb.Width div xStep);
end
end;
for i:=0 to yStep-2 do
begin
IndexY:=i*xStep;
Index:=IndexX+IndexY;
if (abs(Point.Y-((pb.Height div (2*yStep))+i*(pb.Height div yStep))))
<=(pb.Height div (2*yStep)) then
begin
Result.Y:=(pb.Height div (2*yStep))+i*(pb.Height div yStep);
Break;
end
else begin
Result.Y:=(pb.Height div (2*yStep))+(i+1)*(pb.Height div yStep);
end
end;
end;
end.