将画笔取反, 先在旧的网格画一便, 到新的位置再话一便网格。希望对你有帮助。
下边的例子在Form上画一条绿色的对角线,点一下鼠标后就能看到一个大十字光标随着鼠标
移动。与你问题的性质是一样的,只是你要画一个网格而不是简单的十字。
unit Crossfrm;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
procedure FormClick(Sender: TObject);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormPaint(Sender: TObject);
public
ShowCrossing: boolean;
PCursor: TPoint;
Procedure DrawCrossing;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.DrawCrossing;
begin
With Canvas do
begin
Pen.Mode:=pmNotXor;
Pen.Color := clBlack;
MoveTo(0,PCursor.y);
LineTo(width,PCursor.y);
MoveTo(PCursor.x, 0);
LineTo(PCursor.x, Height)
end;
end;
procedure TForm1.FormClick(Sender: TObject);
begin
if ShowCrossing then
begin
ShowCrossing:=false;
DrawCrossing;
Cursor := crDefault;
end else
begin
ShowCrossing:=true;
GetCursorPos(PCursor);
PCursor:=ScreenToClient(PCursor);
DrawCrossing;
Cursor := crNone;
end;
end;
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if ShowCrossing then
begin
DrawCrossing;
PCursor := Point(x,y);
DrawCrossing;
end;
Panel1.Caption:=Format('X,Y: %d,%d', [X,Y]);
end;
procedure TForm1.FormPaint(Sender: TObject);
begin
With Canvas do
begin
Pen.Mode := pmCopy;
Pen.Color := clGreen;
MoveTo(5, 5);
LineTo(width - 5, ClientHeight - Panel1.Height - 5);
end;
if ShowCrossing then
DrawCrossing;
end;
end.