用双缓冲实现图形的移动
2007-03-30 11:51
357 查看
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Timer1: TTimer;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
procedure RefreshActivePoint;
procedure MoveImage;
{ Private declarations }
public
{ Public declarations }
FBitMap:TBitMap; //保存图形
FBitMapBuff:TBitMap; //缓冲区
FActivePoint:TPoint; //当前移动到的位置
FMoveXSpace:Integer; //X方向要移动的距离
FMoveYSpace:Integer; //Y方向要移动的距离
FStop:Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FBitMapBuff:=TBitmap.Create;
FBitMap:=TBitmap.Create;
//缓冲区图大小和Image1大小相同
FBitMapBuff.Width:=Image1.Width;
FBitMapBuff.Height:=Image1.Height;
FBitMap.LoadFromFile('D:/Winnt/Greenstone.bmp');
FActivePoint:=Point(0,0);
FMoveXSpace:=10;
FMoveYSpace:=10;
end;
function GetRandomSpace:Integer;
begin
//返回一个随机的移动距离
Randomize;
Result:=Random(30);
end;
procedure TForm1.RefreshActivePoint;
begin
//检查移动边界,确定下一个要移动到的位置
if (FActivePoint.X+FBitMap.Width>=Image1.Width) then
FMoveXSpace:=0-GetRandomSpace
else if FActivePoint.X<=0 then
FMoveXSpace:=GetRandomSpace;
if FActivePoint.Y+FBitMap.Height>=Image1.Height then
FMoveYSpace:=0-GetRandomSpace
else if FActivePoint.Y<=0 then
FMoveYSpace:=GetRandomSpace;
FActivePoint:=Point(FActivePoint.X+FMoveXSpace,FActivePoint.Y+FMoveYSpace);
if FActivePoint.X+FBitMap.Width>Image1.Width then
FActivePoint.X:=FActivePoint.X-(FActivePoint.X+FBitMap.Width-Image1.Width)
else if FActivePoint.X<0 then
FActivePoint.X:=0;
if FActivePoint.Y+FBitMap.Height>Image1.Height then
FActivePoint.Y:=FActivePoint.Y-(FActivePoint.Y+FBitMap.Height-Image1.Height)
else if FActivePoint.Y<0 then
FActivePoint.Y:=0;
end;
procedure TForm1.MoveImage;
begin
//先绘制缓冲区中的图形
With FBitMapBuff do
begin
FreeImage;
CleanupInstance;
Canvas.FillRect(Rect(0,0,FBitMapBuff.Width,FBitMapBuff.Height));
Canvas.Draw(FActivePoint.X,FActivePoint.Y,FBitMap);
end;
//然后直接给Image 绘制
Image1.Canvas.Draw(0,0,FBitMapBuff);
RefreshActivePoint;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//循环移动
FStop:=Not FStop;
while FStop do
begin
MoveImage;
Application.ProcessMessages;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
//定时器移动
Timer1.Enabled:=Not Timer1.Enabled;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
MoveImage;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Timer1: TTimer;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
procedure RefreshActivePoint;
procedure MoveImage;
{ Private declarations }
public
{ Public declarations }
FBitMap:TBitMap; //保存图形
FBitMapBuff:TBitMap; //缓冲区
FActivePoint:TPoint; //当前移动到的位置
FMoveXSpace:Integer; //X方向要移动的距离
FMoveYSpace:Integer; //Y方向要移动的距离
FStop:Boolean;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FBitMapBuff:=TBitmap.Create;
FBitMap:=TBitmap.Create;
//缓冲区图大小和Image1大小相同
FBitMapBuff.Width:=Image1.Width;
FBitMapBuff.Height:=Image1.Height;
FBitMap.LoadFromFile('D:/Winnt/Greenstone.bmp');
FActivePoint:=Point(0,0);
FMoveXSpace:=10;
FMoveYSpace:=10;
end;
function GetRandomSpace:Integer;
begin
//返回一个随机的移动距离
Randomize;
Result:=Random(30);
end;
procedure TForm1.RefreshActivePoint;
begin
//检查移动边界,确定下一个要移动到的位置
if (FActivePoint.X+FBitMap.Width>=Image1.Width) then
FMoveXSpace:=0-GetRandomSpace
else if FActivePoint.X<=0 then
FMoveXSpace:=GetRandomSpace;
if FActivePoint.Y+FBitMap.Height>=Image1.Height then
FMoveYSpace:=0-GetRandomSpace
else if FActivePoint.Y<=0 then
FMoveYSpace:=GetRandomSpace;
FActivePoint:=Point(FActivePoint.X+FMoveXSpace,FActivePoint.Y+FMoveYSpace);
if FActivePoint.X+FBitMap.Width>Image1.Width then
FActivePoint.X:=FActivePoint.X-(FActivePoint.X+FBitMap.Width-Image1.Width)
else if FActivePoint.X<0 then
FActivePoint.X:=0;
if FActivePoint.Y+FBitMap.Height>Image1.Height then
FActivePoint.Y:=FActivePoint.Y-(FActivePoint.Y+FBitMap.Height-Image1.Height)
else if FActivePoint.Y<0 then
FActivePoint.Y:=0;
end;
procedure TForm1.MoveImage;
begin
//先绘制缓冲区中的图形
With FBitMapBuff do
begin
FreeImage;
CleanupInstance;
Canvas.FillRect(Rect(0,0,FBitMapBuff.Width,FBitMapBuff.Height));
Canvas.Draw(FActivePoint.X,FActivePoint.Y,FBitMap);
end;
//然后直接给Image 绘制
Image1.Canvas.Draw(0,0,FBitMapBuff);
RefreshActivePoint;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//循环移动
FStop:=Not FStop;
while FStop do
begin
MoveImage;
Application.ProcessMessages;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
//定时器移动
Timer1.Enabled:=Not Timer1.Enabled;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
MoveImage;
end;
end.
相关文章推荐
- Delphi 7 + GDIPlus 简单实现双缓冲绘制移动图形
- 双缓冲实现图形液晶的快速显示
- 图形编辑器之双缓冲实现绘图
- Android图形与图像处理-采用双缓冲实现画图板
- Android图形与图像处理-采用双缓冲实现画图板
- 浏览器实现ACDSEE看图功能之--用鼠标移动观看图形
- 使用SVG实现3D图形显示,移动和旋转
- Android图形系统的分析与移植 -- 六、双缓冲framebuffer的实现
- 开题了《面向移动设备的交互式图形平台设计与实现》
- android 自定义view实现图形移动
- Android图形系统的分析与移植--七、双缓冲framebuffer的实现
- python开发之tkinter实现图形随鼠标移动的方法
- python开发之tkinter实现图形随鼠标移动的方法
- 用LCD 实现文字图形的移动和闪烁
- DataGrid实现过多信息鼠标移动到记录上显示,可分页(转)
- c#实现随鼠标移动窗体
- QT无标题,透明,可以移动,可以伸缩基本实现
- IOS Quartz 各种绘制图形用法---实现画图片、写文字、画线、椭圆、矩形、棱形等
- iOS 之UIImageView实现图片的移动和缩放
- CBitmapButton实现图形按钮