图形区域划分(50分)

  • 主题发起人 主题发起人 pipi158
  • 开始时间 开始时间
P

pipi158

Unregistered / Unconfirmed
GUEST, unregistred user!
一个问题比较棘手,请高手指教:一个Image,被划分成不同数量
(4,8,16,24,32...)的等同矩形(长宽都相等),当鼠标点
击Image不同区域的时候,怎么通过程序得知点击的点属于哪个区
域?区域从左向右,从上向下编号?
 
建一个区域的索引,然后用PtInRect函数
 
我已经找到解决的办法:
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.
 
接受答案了.
 
后退
顶部