擦除网格里画的线 ( 积分: 100 )

  • 主题发起人 主题发起人 ohahaok
  • 开始时间 开始时间
O

ohahaok

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls,Math;

type
TForm1 = class(TForm)
Image1: TImage;
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);

private
FFirstDown: Boolean;{ Private declarations }
public
function FindNearestPoint(APoint: TPoint; AGridSize: TSize): TPoint;{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
var
pts:array [0..1] of TPoint;
IFW,IFH:integer;
IGridCntX,IGridCntY:Integer;
IDeltaX,IDeltaY:integer;
Ix1,Ix2,Iy1,Iy2,i,j:Integer;
NumOfNeedle:integer;
X0,Y0:integer;

{------查找最近点--------}
function TForm1.FindNearestPoint(APoint: TPoint; AGridSize: TSize): TPoint;
var
nW, nH: Integer;
begin
nW := Max(AGridSize.cx, 2);
nH := Max(AGridSize.cy, 2);

Result.X := (APoint.X div nW) * nW + Round((APoint.X mod nW)/nW) * nW;
Result.Y := (APoint.Y div nH) * nH + Round((APoint.Y mod nH)/nH) * nH;
end;


procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
pt: TPoint;
Size: TSize;

begin
Size.cx := IDeltaX;
SIze.cy := IDeltaY;
pt := Point(X, Y);
pt := FindNearestPoint(pt, Size);
pt.X := pt.X + 5;
pt.Y := pt.Y + 5;
Image1.Canvas.Pen.Width:= 2;//线型
Image1.Canvas.Pen.Color:= clRed;//颜色

pts[0]:=pt;

if FFirstDown then
begin
pts[1] := pt;
FFirstDown := False;
end;

Image1.Canvas.MoveTo(pts[0].X, pts[0].Y);
Image1.Canvas.LineTo(pts[1].X, pts[1].Y);
pts[1] := pt;

end;



procedure TForm1.Button1Click(Sender: TObject);
begin
NumOfNeedle:=strtoint(form1.edit1.Text); //一个花形的针数
IFW:=X0-10;//棋盘宽
IFH:=Y0-10;//棋盘高
IGridCntX:=NumOfNeedle-1;//水平格数
IGridCntY:=NumOfNeedle-1;//垂直格数
IDeltaX:=IFW div IGridCntX;//水平格宽
IDeltaY:=IFH div IGridCntY;//垂直格高

IFW:=IDeltaX * IGridCntX;//重整棋盘宽
IFH:=IDeltaY * IGridCntY;//重整棋盘高

Iy1:=5;
Iy2:=IFH+5;
for i:=1 to IGridCntX-1 do
begin
Ix1:=i*IDeltaX+5;
Ix2:=Ix1;
Image1.Canvas.MoveTo(Ix1,Iy1);
Image1.Canvas.LineTo(Ix2,Iy2);
end;
Ix1:=5;
Ix2:=IFW+5;
for j:=1 to IGridCntY-1 do
begin
Iy1:=j*IDeltaY+5;
Iy2:=Iy1;
Image1.Canvas.MoveTo(Ix1,Iy1);
Image1.Canvas.LineTo(Ix2,Iy2);

end;
FFirstDown := True;
end;



procedure TForm1.FormCreate(Sender: TObject);
begin
with Image1 do
begin
x0:=round(Image1.ClientWidth);
y0:=round(Image1.ClientHeight);
Image1.Canvas.Pen.Color:=clBlack;//线条颜色
canvas.MoveTo(0,0);
canvas.MoveTo(5,5);
canvas.LineTo(x0-5,5);
canvas.LineTo(x0-5,y0-5);
canvas.LineTo(5,y0-5);
canvas.LineTo(5,5);
end;
end;

end.
哪位大师能告诉我怎么使每次鼠标点下一点后在点旁边自动显示点的数字(0,1,2,3,4等),并且点击一个按钮能檫掉在网格上画的线
 
后退
顶部