我原先用循化编写的程序,现改成线程,加入总程序后,老是出现内存问题,请高手解决!
线程程序如下:(如需要循环做的也可贴出来)
//******************************************************
// 该程序动态显示Image图片,实现BMP图片的动画功能
//
// 编制:张根正 时间:2008-10-28
//******************************************************
unit ImageMotion;interfaceuses
Windows,Messages,SysUtils,Stdctrls,dialogs,Classes,Controls, ExtCtrls,Graphics;type
TImageThread = class(TThread) { 声明线程类 }
private
FStopPro :Boolean;
FPicture :TPicture;
FImageName :String;
FImageInterval :Integer;
FImageMax :Integer;
protected
procedure Execute; override;{ 执行线程的方法 }
public
constructor Create(StopPro:Boolean;Picture:TPicture;ImageName:string;ImageInterval:Integer;ImageMax:Integer); virtual; { 线程构造器 }
end; TImageMotion = class(TImage)
private
FimageName :string; //图片在动态库中的文件名前缀
FImageMax :integer; //图片总数量
FImageInterval :integer; //Timer 计时器
FImageState :Boolean;
FAutoSize :Boolean; //取消该功能
StopPro:Boolean;
Q1:TImageThread;
procedure setImageInterval(Value:integer);
{ Private declarations }
protected
{ Protected declarations }
procedure SetAutoSize(Value:Boolean); override;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
procedure Play;
procedure Stop;
{ Public declarations }
published
property ImageName : String read FimageName write FimageName ;
property ImageMax : Integer read FImageMax write FImageMax default 1;
property ImageInterval :Integer read FImageInterval write setImageInterval default 1000;
property ImageState :Boolean read FImageState write FImageState Default False;
property AutoSize :Boolean read FAutoSize write SetAutoSize default False;
{ Published declarations }
end;procedure Register;implementation
procedure Register;
begin
RegisterComponents('AAA Components', [TImageMotion]);
end;
constructor TImageMotion.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
ImageMax :=1;
ImageInterval :=100;
ImageState :=False;
end;
destructor TImageMotion.Destroy;
begin
inherited Destroy;
end;
//--
constructor TImageThread.Create(StopPro:Boolean;Picture:TPicture;ImageName:string;ImageInterval:Integer;ImageMax:Integer);
begin
inherited Create(False);
FStopPro :=StopPro;
FPicture :=Picture;
FImageName :=ImageName;
FImageInterval :=ImageInterval;
FImageMax :=ImageMax;
FreeOnTerminate :=False;
end;
procedure TImageThread.Execute;
Var
i:Integer;
begin
i:=1;
While Not Terminated do
begin
// i :=(i mod FImageMax)+1;
FPicture.Bitmap.Handle :=LoadBitmap(hinstance,pChar(FImageName+inttostr(i)));
Inc(i);
IF i>=FImageMax then
i:=1;
Sleep(FImageInterval);
end;
FPicture.Bitmap.Handle :=0;
end;
//---
procedure TImageMotion.Play;
begin
ImageState :=True;
Stoppro :=False;
IF not Assigned(Q1) then
Q1:=TImageThread.Create(Stoppro,Picture,ImageName,ImageInterval,ImageMax);
Sleep(100);
end;
procedure TImageMotion.Stop;
begin
ImageState :=False;
Q1.FStopPro :=True;
Q1.Terminate;
IF Assigned(Q1) then
FreeAndNil(Q1);
Sleep(100);
end;procedure TImageMotion.setImageInterval(Value:integer);
begin
FImageInterval :=Value;
end;
procedure TImageMotion.SetAutoSize(Value:Boolean);
begin
MessageBox(0,'禁止使用该功能!','系统开发员提示',MB_OK+MB_ICONEXCLAMATION+MB_DEFBUTTON1);
end;end.
线程程序如下:(如需要循环做的也可贴出来)
//******************************************************
// 该程序动态显示Image图片,实现BMP图片的动画功能
//
// 编制:张根正 时间:2008-10-28
//******************************************************
unit ImageMotion;interfaceuses
Windows,Messages,SysUtils,Stdctrls,dialogs,Classes,Controls, ExtCtrls,Graphics;type
TImageThread = class(TThread) { 声明线程类 }
private
FStopPro :Boolean;
FPicture :TPicture;
FImageName :String;
FImageInterval :Integer;
FImageMax :Integer;
protected
procedure Execute; override;{ 执行线程的方法 }
public
constructor Create(StopPro:Boolean;Picture:TPicture;ImageName:string;ImageInterval:Integer;ImageMax:Integer); virtual; { 线程构造器 }
end; TImageMotion = class(TImage)
private
FimageName :string; //图片在动态库中的文件名前缀
FImageMax :integer; //图片总数量
FImageInterval :integer; //Timer 计时器
FImageState :Boolean;
FAutoSize :Boolean; //取消该功能
StopPro:Boolean;
Q1:TImageThread;
procedure setImageInterval(Value:integer);
{ Private declarations }
protected
{ Protected declarations }
procedure SetAutoSize(Value:Boolean); override;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
procedure Play;
procedure Stop;
{ Public declarations }
published
property ImageName : String read FimageName write FimageName ;
property ImageMax : Integer read FImageMax write FImageMax default 1;
property ImageInterval :Integer read FImageInterval write setImageInterval default 1000;
property ImageState :Boolean read FImageState write FImageState Default False;
property AutoSize :Boolean read FAutoSize write SetAutoSize default False;
{ Published declarations }
end;procedure Register;implementation
procedure Register;
begin
RegisterComponents('AAA Components', [TImageMotion]);
end;
constructor TImageMotion.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
ImageMax :=1;
ImageInterval :=100;
ImageState :=False;
end;
destructor TImageMotion.Destroy;
begin
inherited Destroy;
end;
//--
constructor TImageThread.Create(StopPro:Boolean;Picture:TPicture;ImageName:string;ImageInterval:Integer;ImageMax:Integer);
begin
inherited Create(False);
FStopPro :=StopPro;
FPicture :=Picture;
FImageName :=ImageName;
FImageInterval :=ImageInterval;
FImageMax :=ImageMax;
FreeOnTerminate :=False;
end;
procedure TImageThread.Execute;
Var
i:Integer;
begin
i:=1;
While Not Terminated do
begin
// i :=(i mod FImageMax)+1;
FPicture.Bitmap.Handle :=LoadBitmap(hinstance,pChar(FImageName+inttostr(i)));
Inc(i);
IF i>=FImageMax then
i:=1;
Sleep(FImageInterval);
end;
FPicture.Bitmap.Handle :=0;
end;
//---
procedure TImageMotion.Play;
begin
ImageState :=True;
Stoppro :=False;
IF not Assigned(Q1) then
Q1:=TImageThread.Create(Stoppro,Picture,ImageName,ImageInterval,ImageMax);
Sleep(100);
end;
procedure TImageMotion.Stop;
begin
ImageState :=False;
Q1.FStopPro :=True;
Q1.Terminate;
IF Assigned(Q1) then
FreeAndNil(Q1);
Sleep(100);
end;procedure TImageMotion.setImageInterval(Value:integer);
begin
FImageInterval :=Value;
end;
procedure TImageMotion.SetAutoSize(Value:Boolean);
begin
MessageBox(0,'禁止使用该功能!','系统开发员提示',MB_OK+MB_ICONEXCLAMATION+MB_DEFBUTTON1);
end;end.
//******************************************************
// 该程序动态显示Image图片,实现BMP图片的动画功能
//
// 编制:张根正 时间:2008-10-28
//******************************************************
unit ImageMotion;interfaceuses
Windows,Messages,SysUtils,Stdctrls,dialogs,Classes, Controls, ExtCtrls,Graphics;type TImageMotion = class(TImage)
private
MyTimer :TTimer;
ImageLoop :integer; //图像循环变量
FimageName :string; //图片在动态库中的文件名前缀
FImageMax :integer; //图片总数量
FImageHandle : Hmodule; //已打开的动态文件句柄
FImageInterval :integer; //Timer 计时器
FImageState :Boolean;
FAutoSize :Boolean; //取消该功能 procedure setImageInterval(Value:integer);
procedure MyTimerTimer(Sender:TObject);
// procedure LoadPicture(Value:integer);
{ Private declarations }
protected
{ Protected declarations }
procedure SetAutoSize(Value:Boolean); override;
public
constructor Create(AOwner:TComponent); override;
destructor Destroy; override;
procedure Play;
procedure Stop;
{ Public declarations }
published
property ImageName : String read FimageName write FimageName ;
property ImageMax : Integer read FImageMax write FImageMax default 1;
property ImageInterval :Integer read FImageInterval write setImageInterval default 1000;
property ImageHandle :Hmodule read FImageHandle write FImageHandle;
property ImageState :Boolean read FImageState write FImageState Default False;
property AutoSize :Boolean read FAutoSize write SetAutoSize default False;
{ Published declarations }
end;procedure Register;implementation
//Var
// Ret :HWND;
// sdll:Hmodule;
procedure Register;
begin
RegisterComponents('AAA Components', [TImageMotion]);
end;
constructor TImageMotion.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
MyTimer :=TTimer.Create(Self);
Mytimer.OnTimer :=MyTimerTimer;
Mytimer.Interval :=1000;
Mytimer.Enabled :=False;
ImageMax :=1;
ImageInterval :=1000;
ImageLoop :=1;
ImageState :=False;
end;
destructor TImageMotion.Destroy;
begin
Mytimer.Free;
inherited Destroy;
end;procedure TImageMotion.Play;
begin
ImageLoop :=1;
ImageState :=True;
Mytimer.Enabled :=True;
end;
procedure TImageMotion.Stop;
begin
ImageState :=False;
Mytimer.Enabled :=False;
Picture.Bitmap.Handle :=0;
end;procedure TImageMotion.MyTimerTimer(Sender:TObject);
{var
TempStr:string; }
begin
ImageLoop :=(ImageLoop mod ImageMax)+1;
// TempStr :=ImageName+inttostr(ImageLoop);
Picture.Bitmap.Handle :=LoadBitmap(hinstance,pChar(ImageName+inttostr(ImageLoop)));
end;
procedure TImageMotion.setImageInterval(Value:integer);
begin
FImageInterval :=Value;
Mytimer.Interval :=Value;
end;
procedure TImageMotion.SetAutoSize(Value:Boolean);
begin
MessageBox(0,'禁止使用该功能!','系统开发员提示',MB_OK+MB_ICONEXCLAMATION+MB_DEFBUTTON1);
end;end.