网站公告列表

  没有公告

加入收藏
设为首页
联系站长
您现在的位置: 网络学院 >> 程序设计 >> Delphi编程 >> 文章正文
  用双缓冲实现图形的移动            【字体:
用双缓冲实现图形的移动
作者:佚名    文章来源:不详    点击数:    更新时间:2007-8-1    
正在装载数据……

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. 




本文来源:http://blog.csdn.net/ZyxIp/archive/2007/03/30/1546379.aspx
站内文章搜索 高级搜索
文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    最新热点 最新推荐 相关文章
     在delphi中使用xml文档有…
     初探delphi 7 中的插件编…
     delphi 2006(dexter) & …
     获得windows的版本信息。
     “序列号输入助手”源代…
     rs232串口通讯模块
     ado方式下判断数据表是否…
  • Java Swing实现俄罗斯方块

  • c++实现系统托盘图标

  • Ajax - javascript之实现…

  • 汇编来实现OOP思想

  • 在C++实现C#中的属性(Proper…

  • 把游戏变成com组件,实现框架…

  • 在C++实现C#中的属性(Proper…

  • 如何实现那种流行的平面按钮

  • 如何用VB实现Modbus串行通讯

  • [VB][测试技术应用]VB与IE交…

  •   网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    网络学院©2007 www.23book.net
    为您提供web编程,vb编程,vc编程,服务器架设管理,数据库设计等方面的知识 站长:David