请求:
我想利用这个程序实现双缓冲移动图片的功能,而且想在当图片运行到Form外的时候就把图片给释放掉,可是好像我运行的时候电脑总是迟疑了一段时间哦,也就是说这段时间相当于死机了。而且我竟然还有那么几次蓝屏哦,麻烦大家帮我看看,多谢了!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Timer1: TTimer;
Button2: TButton;
Button3: TButton;
Button1: TButton;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject); private procedure MoveImage;
{ Private declarations }
public
{ Public declarations }
FBitMap:TBitMap;//保存图形
FBitMapBuff:TBitMap;//缓冲区
FActivePoint:TPoint;//当前移动到的位置
FMoveXSpace:integer;//X方向要移动的距离
FMoveYSpace:integer;//Y方向要移动的距离
FStop:Boolean;
a:integer;
end;var
Form1: TForm1;implementation{$R *.dfm}
function GetRandomSpace:integer;
begin
//返回一个随机的移动距离
Randomize;
Result:=Random(2);
end;procedure TForm1.MoveImage;
begin
FBitMapBuff:=TBitMap.Create;
FBitMap:=TBitMap.Create;
//缓冲区图大小和Form1大小相同 就是说在整个Form中画图
FBitMapBuff.Width:=Form1.Width;
FBitMapBuff.Height:=Form1.Height;
FBitMap.LoadFromFile('Car1.bmp');
//先绘制缓冲区的图形
with FBitMapBuff do
begin
FreeImage;
CleanupInstance;
Canvas.FillRect(Rect(0,0,FBitMapBuff.Width,FBitMapBuff.Height));
Canvas.Draw(a,0,FBitMap);
a:=a+5;
end;
//然后直接给Image绘制
Form1.Canvas.Draw(0,0,FBitMapBuff);
if a>=500 then
begin
timer1.Enabled:=not timer1.Enabled;
end;
FBitMap.Free;
FBitMapBuff.Free;
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;procedure TForm1.Button1Click(Sender: TObject);
begin
Close;
Application.terminate;
end;
end.
人家告诉我去使用设置断点,可是我用了还是不能发现错误到底出现在哪里哦!悬赏你50分
我想利用这个程序实现双缓冲移动图片的功能,而且想在当图片运行到Form外的时候就把图片给释放掉,可是好像我运行的时候电脑总是迟疑了一段时间哦,也就是说这段时间相当于死机了。而且我竟然还有那么几次蓝屏哦,麻烦大家帮我看看,多谢了!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Timer1: TTimer;
Button2: TButton;
Button3: TButton;
Button1: TButton;
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button1Click(Sender: TObject); private procedure MoveImage;
{ Private declarations }
public
{ Public declarations }
FBitMap:TBitMap;//保存图形
FBitMapBuff:TBitMap;//缓冲区
FActivePoint:TPoint;//当前移动到的位置
FMoveXSpace:integer;//X方向要移动的距离
FMoveYSpace:integer;//Y方向要移动的距离
FStop:Boolean;
a:integer;
end;var
Form1: TForm1;implementation{$R *.dfm}
function GetRandomSpace:integer;
begin
//返回一个随机的移动距离
Randomize;
Result:=Random(2);
end;procedure TForm1.MoveImage;
begin
FBitMapBuff:=TBitMap.Create;
FBitMap:=TBitMap.Create;
//缓冲区图大小和Form1大小相同 就是说在整个Form中画图
FBitMapBuff.Width:=Form1.Width;
FBitMapBuff.Height:=Form1.Height;
FBitMap.LoadFromFile('Car1.bmp');
//先绘制缓冲区的图形
with FBitMapBuff do
begin
FreeImage;
CleanupInstance;
Canvas.FillRect(Rect(0,0,FBitMapBuff.Width,FBitMapBuff.Height));
Canvas.Draw(a,0,FBitMap);
a:=a+5;
end;
//然后直接给Image绘制
Form1.Canvas.Draw(0,0,FBitMapBuff);
if a>=500 then
begin
timer1.Enabled:=not timer1.Enabled;
end;
FBitMap.Free;
FBitMapBuff.Free;
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;procedure TForm1.Button1Click(Sender: TObject);
begin
Close;
Application.terminate;
end;
end.
人家告诉我去使用设置断点,可是我用了还是不能发现错误到底出现在哪里哦!悬赏你50分
1\为什么要用CleanupInstance;
{Do not call CleanupInstance directly. CleanupInstance is called automatically when the object instance is destroyed.CleanupInstance releases all long strings and variants. It sets long strings to empty and variants to Unassigned.}2\你这种情况根本就不需要用内存缓冲图,因为只有一次绘制动作:Canvas.Draw(...);
begin
FBitMapBuff:=TBitMap.Create;
FBitMap:=TBitMap.Create;
//缓冲区图大小和Form1大小相同 就是说在整个Form中画图
FBitMapBuff.Width:=Form1.Width;
FBitMapBuff.Height:=Form1.Height;
FBitMap.LoadFromFile('Car1.bmp');
//先绘制缓冲区的图形
with FBitMapBuff do
begin
FreeImage;
CleanupInstance;
Canvas.FillRect(Rect(0,0,FBitMapBuff.Width,FBitMapBuff.Height));
Canvas.Draw(a,0,FBitMap);
刚创建的FBitMapBuff你干嘛又FreeImage又CleanupInstance,而且你都CleanupInstance后面使用程序运行时不产生错误吗?
用双缓冲实现图形的移动
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.
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ZyxIp/archive/2007/03/30/1546379.aspx
借用一下你的程序哦
procedure TForm1.MoveImage;
begin
//先绘制缓冲区中的图形
With FBitMapBuff do
begin
FreeImage;
CleanupInstance;
很多人都属这个什么FreeImage,与CleanupInstance哦,都说不用,可是就是这个程序中就是有哦!你说呢?