paintbox中画折线的问题(100分)

  • 主题发起人 主题发起人 happymanfreeman
  • 开始时间 开始时间
H

happymanfreeman

Unregistered / Unconfirmed
GUEST, unregistred user!
各位: 大家好 !

paintbox中画折线的问题. 画折线时先在paintbox中按一下鼠标, 然后拖动鼠标,
就画出来.

这里有一个问题: 一些软件画折线时, 拖动鼠标时, 如果鼠标移动过程中按键都是松开的, 或者先按着鼠标按键拖动, 接着松开鼠标按键继续拖动, 这些对画折线没有影响.
那么, 上述拖动鼠标画线的代码应该在paintboxmousumove()事件中执行呢, 还 是....?? paintboxmousumove()事件中鼠标按键到底有没有按着 ??

上述paintbox中画折线到底如何编程呢 ?? 到底在应该什么事件中进行呢 ??
 
我图形接触不多,感觉先建立全局数组,应该在paintboxmouseclick()中记录鼠标单击时的
坐标(x,y)到数组中,只要全局数组有两个不为空,就可以对坐标之间进行画线
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
PaintBox1: TPaintBox;
procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCreate(Sender: TObject);
procedure PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure PaintBox1Paint(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

var
down:boolean;
Points:array[0..1000] of TPoint;
n:integer;
{$R *.dfm}

procedure TForm1.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
down:=true;
PaintBox1.Canvas.MoveTo(x,y);
end;

procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if down then
begin
Points[n].X:=x;
Points[n].Y:=y;
n:=n+1;
PaintBox1.Repaint;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
down:=false;
n:=0;
end;

procedure TForm1.PaintBox1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
down:=false;
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
var
i:integer;
begin
if n=0 then exit;
PaintBox1.Canvas.MoveTo(Points[0].x,Points[0].y);
for i:=1 to n-1 do
paintBox1.Canvas.LineTo(Points.X,Points.Y);
end;

end.
 
这里有一个问题: 一些软件画折线时, 拖动鼠标时, 如果鼠标移动过程中按键都是松开的, 或者先按着鼠标按键拖动, 接着松开鼠标按键继续拖动, 这些对画折线没有影响.
那么, 上述拖动鼠标画线的代码应该在paintboxmousumove()事件中执行呢, 还 是....?? paintboxmousumove()事件中鼠标按键到底有没有按着 ??

请大家针对性地回答 !! 谢谢 !!
 
在onmousedown或者onmouseup事件中都可以,记住前面一个点坐标,在第二次mousedown或者mouseup的时候用lineto画线即可
 
拖动鼠标画线的代码应该在paintboxmousemove()事件中执行呢 ?? mousemove()是
什么意思 ?? 鼠标移动 ?? 移动过程中鼠标按键到底有没有按着 ??
 
mousemove()是鼠标移动事件,移动过程中不管有没有按着只要移动鼠标就会触发
mousemove()事件,画线的效果取决于保存坐标(x,y)的数组,上面的例子代码有一个
down标志,表示只记录鼠标[red]'按下'[/red]状态时的move的轨迹,所以只画出鼠标
按下状态时移动的轨迹,但由于画线放在了PaintBox1Paint()事件中,所以按下--放开
--再按下两个坐标之间不是断开的,而是一条连着的直线,不知你明白了没有
 
如果你的线要跟踪鼠标移动,按下的时候再确定具体位置呢,就要用mousemove 来跟踪鼠标位置 。
如果你关心要线的起点和结束点,那么仅仅用mouseup 和 就可以了。
 
后退
顶部