简单的划直线问题:在鼠标按下 Move的过程中,边划线,边擦除原划线,最后只要起点到鼠标Up的那一条 (50分)

  • 主题发起人 主题发起人 gzleefx
  • 开始时间 开始时间
G

gzleefx

Unregistered / Unconfirmed
GUEST, unregistred user!
如题: 
procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);

begin
if(Flag) then
Image1.Canvas.LineTo (X,Y);
end;
  不能满足我的要求,我的意思是在Image1.MouseDown ---Image1.MouseUp之间划直线,
但是在Image1.MouseMove的过程中可以看到要划的线的效果。
 
其实是一个把背景更新后再重新画的过程
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
with Canvas do
begin
canvas.Brush.Color:=clBlack;
canvas.fillrect(rect(0,0,width,height));
Pen.Color := clRed;
MoveTo(x,0);
LineTo(x,Height);
MoveTo(0,y);
LineTo(width,y);
end;
end;
 
以前我回答过的,再发一遍吧。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
procedure Draw(X1,Y1,X2,Y2 : integer);
end;

var
Form1: TForm1;
Down : Boolean;
FtPt,CurPt : TPoint;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
Down := FALSE;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Down := TRUE;
Canvas.Pen.Mode := pmNotXor;
FtPt := Point(X,Y);
CurPt := Point(X,Y);
Draw(FtPt.X,FtPt.Y,CurPt.x,CurPt.Y);

end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Down := FALSE;
// Draw(FtPt.X,FtPt.Y,CurPt.x,CurPt.Y);
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if not Down then Exit;
Draw(FtPt.X,FtPt.Y,CurPt.x,CurPt.Y);
CurPt := Point(X,Y);
Draw(FtPt.X,FtPt.Y,CurPt.x,CurPt.Y);
end;

procedure TForm1.Draw(X1,Y1,X2,Y2 : integer);
begin
Canvas.MoveTo(X1,Y1);
Canvas.LineTo(X2,Y2);
end;

end.
 
吕雪松是对的!
 
var
IsDown:boolean;
DownX,DownY,OldX,OldY:integer;
procedure TForm1.FormCreate(Sender: TObject);
begin
Canvas.Pen.Mode :=pmNotXor;
end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
Isdown:=true;
DownX:=x;
Downy:=y;
OldX:=x;
OldY:=y;
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if IsDown then
begin
Canvas.MoveTo(Downx,Downy);
Canvas.LineTo(Oldx,Oldy);

OldX:=x;
OldY:=Y;

Canvas.MoveTo(Downx,Downy);
Canvas.LineTo(x,y);
end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
IsDown:=false;
end;
 
多谢各位,特别是吕雪松先生,您在这个网站那么热心地回答了大家的很多问题,而且
答案详细,准确。
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
488
import
I
后退
顶部