当前位置: 首页 > 图文教程 > 开发语言 > Delphi > Delphi图形图像编程(二)

Delphi
在线播放器DIY
关于VisiBroker For Delphi的使用(3)
关于VisiBroker For Delphi的使用(2)
关于VisiBroker For Delphi的使用(1)
Delphi的两个实用技巧(2)巧用Windows的API函数
Delphi的两个实用技巧(1)播放Flash
delphi学习:两种方法使用xml文档
Delphi与Word之间的融合技术
Delphi中动态链接库(DLL)的建立和使用
Delphi基础:Window 消息大全使用详解下
Delphi基础:Window 消息大全使用详解上
教你在DELPHI中如何调用系统对话框
Delphi开发单机瘦小数据库程序要点
用Delphi + DirectX开发简单RPG游戏
Delphi7从入门到精通之认识Delphi编辑器
Delphi7从入门到精通之历数Delphi七个版本
Delphi学习:图像放大漫游攻略
用编程来实现24小时制到12小时制的转换
一个实际的OLE服务器的开发
Delphi一点通:如何将源代码学好

Delphi图形图像编程(二)


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 55 ::
收藏到网摘: n/a

 
画直线时,用户只有在松开鼠标才能看见直线,对直线的变化不能进行实时观测。这是因为鼠标移动时程序没有进行某种应。Delphi定义了OnMouseMove事件来响应鼠标移动。以下代码可使用户随时观测直线的变化: 

  procedure TForm1.FormMouseMove(Sender:Tobject)

begin

Drowto(X,Y);

Moveto(origin);

end. 

origin是起始点。

5.2.3 绘图功能的实现

  绘图软件常根据用户的要求改变绘图工具。Graphex.dpr例程中,当用户按下某个按钮时,可选择绘图工具中的画笔或画刷,在程序类型说明部分定义了五种绘图工具。

   type

TDrawingTool = (dtLine,dtRectangle,dtEllips,dtRoundRect,dtPolygon); 

当选中某种按钮,则选中了相应的绘图工具,如: 

procedure TForm1.LineButtonClick(Sender: TObject);

begin

DrawingTool := dtLine;

end; 

procedure TForm1.RectangleButtonClick(Sender: TObject);

begin

DrawingTool := dtRectangle;

end; 

procedure TForm1.EllipseButtonClick(Sender: TObject);

begin

DrawingTool := dtEllipse;

end; 

procedure TForm1.RoundRectButtonClick(Sender: TObject);

begin

DrawingTool := dtRoundRect;

end; 

procedure TForm1.PolygonButtonClick(Sender: TObject);

begin

DrawingTool :=dtPolygon;

end;  

DrawShape过程定义了每种绘图工具的动作: 

procedure TForm1.DrawShape(TopLeft, BottomRight: TPoint; AMode: TPenMode);

begin

with Image.Canvas do

begin

Pen.Mode := AMode;

case DrawingTool of

dtLine: begin

MoveTo(TopLeft.X, TopLeft.Y);

LineTo(BottomRight.X, BottomRight.Y);

end;

dtRectangle: Rectangle(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y);

dtEllipse: Ellipse(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y);

dtRoundRect: RoundRect(TopLeft.X, TopLeft.Y, BottomRight.X, BottomRight.Y,

(TopLeft.X - BottomRight.X) div 2, (TopLeft.Y - BottomRight.Y) div 2);

dtPolygon:Polygon([Point(0,0),TopLeft,BottomRight]); end;

end;

end; 

 程序刚运行时,只有一个工具栏。当用户单击画笔和画刷时,则出现相应的工具栏,如图5.4。其代码如下: 

procedure TForm1.PenButtonClick(Sender: TObject);

begin

PenBar.Visible := PenButton.Down;

end; 

procedure TForm1.BrushButtonClick(Sender: TObject);

begin

BrushBar.Visible := BrushButton.Down;

end;

         在设计绘图程序时,还要解决一些问题。如为了在鼠标移动时能观测图形的变化,我们定义了OnMouseMove事件。但会出现这样的现象,当鼠标进入绘图区时,用户未按下鼠标键,画布上却出现绘制的图形,这是我们不希望看到的。其原因是没有对鼠标按钮是否按下进行判断。因此在窗体对象中定义了drawing的域,当鼠标按钮按下时,drawing 设置成真值。只有drawing为真,鼠标移动才执行绘图功能;当鼠标键松开时,drawing设置成假,鼠标移动将不执行绘图动作。

       另外一个问题是, 我们希望得到的是鼠标按钮按下和松开这两点所形成的图形,但OnMouseMove却把鼠标轨迹上各点与起始点所形成的所有图形画在屏幕上,这同样是我们不希望看到的,为了解决这些问题,程序定义了鼠标的三个事件: 

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;

Shift: TShiftState; X, Y: Integer);

begin

Drawing := True;

Image.Canvas.MoveTo(X, Y);

Origin := P