qddelphi你好,我是刚学Delphi,看了你的问题,好像可以解决。
下面试我做的,可能不好,请多多指教~!!!!
unit main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
PMyList = ^AList;
AList = record
px:Integer;
py:Integer;
end;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
XX,YY:Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
DrawFlag: String;
pxyList:TList;//点链表
pxyRecord
MyList;//结构实例
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
Var
I: Integer;
begin
XX:=StrToInt(Edit2.Text);//Edit2中为多边形宽度
YY:=StrToInt(Edit1.Text);//Edit1中为多边形高度
Edit1.Visible:=False;
Edit2.Visible:=False;
Canvas.FillRect(ClientRect);
for I := 0 to 128 do // Iterate
begin
Canvas.MoveTo(20+I*XX, 60);
Canvas.LineTo(20+I*XX, 60+96*YY);
end; // for
for I := 0 to 96 do // Iterate
begin
Canvas.MoveTo(20, 60+I*YY);
Canvas.LineTo(20+128*XX, 60+I*YY);
end; // for
DrawFlag:='CanDraw';
end;
procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
Var
I: Integer;
rRect:TRect;
xFlag,yFlag:Bool;
xPoint,yPoint:Integer;
begin
if DrawFlag='CanDraw' then
begin
xFlag:=False;
yFlag:=False;
xPoint:=0;
yPoint:=0;
If (X>=20) and (X<=(20+128*XX))and (Y>=60)and (Y<= (60+96*YY)) then
begin
for I := 1 to 128 do // Iterate
begin
if (X>=20+(I-1)*XX) and (X<=20+I*XX) then
begin
xFlag:=True;
xPoint:=I;
Break;
end;
end; // for
for I := 1 to 96 do // Iterate
begin
if (Y>=60+(I-1)*YY) and (Y<=60+I*YY) then
begin
yFlag:=True;
yPoint:=I;
Break;
end;
end; // for
if (xFlag=True) and (yFlag=True) then
begin
New(pxyRecord);
rRect.Left:=20+(xPoint-1)*XX;
rRect.Right:=20+xPoint*XX;
rRect.Top:=60+(yPoint-1)*YY;
rRect.Bottom:=60+yPoint*YY;
Canvas.Brush.Color:=clRed;
Canvas.FillRect(rRect);
pxyRecord.px:=xPoint;
pxyRecord.py:=yPoint;
pxyList.Add(pxyRecord);
end;
end;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
pxyList:=TList.Create;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
I: Integer;
begin
try
for I := 0 to pxyList.Count-1 do // Iterate
begin
New(pxyRecord);
pxyRecord:=pxyList.Items
;
Dispose(pxyRecord);
end // for
finally // wrap up
pxyList.Free;
end; // try/finally
end;
end.
程序现在可以画图,可以点点,我把每个被点中的点保存到了List中,
然后可以做个循环读出来。