需要在程序中加载gif图片,用于显示程序的进度, 但是在主线程中初始化非常的占用资源,导致如果是在主线程中加载gif,根本就不能刷新。
后来尝试在子线程通过线程加载每一帧的图片,以实现动态的效果, 但是造成了频繁向主线程申请资源,造成了初始化程序更慢。
代码如下, 各位还有别的思路吗? unit TRBProcessing;interfaceuses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.ImgList, Vcl.StdCtrls, Vcl.Imaging.GIFImg;type
TRunImage = class(TThread)
private
FBox: TPaintBox;
FImageList: TImageList;
FStep: Integer;
FisSetTransparentColor: Boolean;
protected
procedure Execute; override;
public
constructor Create(ABox: TPaintBox; AImageList: TImageList);
property isSetTransparentColor: Boolean read FisSetTransparentColor write FisSetTransparentColor;
end;type
TTRBFrmProcessing = class(TForm)
ImgRun: TImageList;
ImgDog: TPaintBox;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
oRunImage: TRunImage;
{ Private declarations }
protected
public
{ Public declarations }
end;var
TRBFrmProcessing: TTRBFrmProcessing;procedure ProcessingWaitMsg2(AFlag: Integer; AMsg: string = '');const
isProcMsgShow: Integer = 1;
isProcMsgHide: Integer = 0;
isFormTransparentColor: Integer = 2;
isFormNoTransparentColor: Integer = 3;implementation{$R *.dfm}procedure ProcessingWaitMsg2(AFlag: Integer; AMsg: string = '');
begin
if AFlag = isProcMsgHide then
begin
if Application.FindComponent('TRBFrmProcessing') <> nil then
begin
TRBFrmProcessing.TransparentColor := True;
Sleep(100);
TRBFrmProcessing.Refresh;
TRBFrmProcessing.Hide;
TRBFrmProcessing.Close;
FreeAndNil(TRBFrmProcessing);
end;
Exit;
end
else if AFlag = isFormTransparentColor then
begin
if Application.FindComponent('TRBFrmProcessing') <> nil then
begin
TRBFrmProcessing.TransparentColor := True;
TRBFrmProcessing.oRunImage.Suspended:= True;
TRBFrmProcessing.oRunImage.isSetTransparentColor := True; TRBFrmProcessing.oRunImage.Resume;
end;
end
else if AFlag = isFormNoTransparentColor then
begin
if Application.FindComponent('TRBFrmProcessing') <> nil then
begin
TRBFrmProcessing.TransparentColor := False;
end;
end
else
begin
if Application.FindComponent('TRBFrmProcessing') = nil then
begin
TRBFrmProcessing := TTRBFrmProcessing.Create(Application);
end;
TRBFrmProcessing.TransparentColor := True;
TRBFrmProcessing.TransparentColorValue := clBtnFace;
with TRBFrmProcessing do
begin
Show;
Update;
end;
end;
end;{ TRunDog }constructor TRunImage.Create(ABox: TPaintBox; AImageList: TImageList);
begin
FBox := ABox;
FImageList := AImageList;
FStep := 0;
FreeOnTerminate := True;
FisSetTransparentColor := False;
Inherited Create(False);
end;procedure TRunImage.Execute;var
oIcon: TIcon;
oBitMap: TBitmap;
begin
oIcon := TIcon.Create;
oBitMap := TBitmap.Create;
try
repeat
// if FisSetTransparentColor then
// TRBFrmProcessing_Two.TransparentColor := True
// else
// TRBFrmProcessing_Two.TransparentColor := False; FBox.Canvas.Lock;
FImageList.GetIcon(FStep, oIcon);
// FBox.Canvas.Brush.Color := TPanel(FBox.Parent).Color; //clBtnFace;
FBox.Canvas.FillRect(FBox.Canvas.ClipRect);
FBox.Canvas.Draw(0, 0, oIcon);
FBox.Canvas.Unlock;
Application.ProcessMessages; Sleep(50);
FStep := FStep + 1;
if FStep > 45 then
begin
Sleep(100);
FStep := 0;
end;
until Terminated; finally
oIcon.Free;
oBitMap.Free;
end;
end;procedure TTRBFrmProcessing.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
oRunImage.Terminate;
Sleep(20);
TRBFrmProcessing.TransparentColor := True;
TRBFrmProcessing.Color := clBlack;
end;procedure TTRBFrmProcessing.FormShow(Sender: TObject);
begin
oRunImage := TRunImage.Create(ImgDog, ImgRun);
ImgDog.Left := Self.Left;
ImgDog.Top := self.Top;
self.Width := ImgDog.Width;
self.Height := ImgDog.Height;
end;end.dfm
object TRBFrmProcessing: TTRBFrmProcessing
Left = 0
Top = 0
BorderStyle = bsNone
Caption = 'TRBFrmProcessing'
ClientHeight = 166
ClientWidth = 168
Color = clBtnFace
TransparentColor = True
TransparentColorValue = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = False
Position = poMainFormCenter
OnClose = FormClose
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object ImgDog: TPaintBox
Left = 8
Top = 9
Width = 153
Height = 152
end
object ImgRun: TImageList
ColorDepth = cd32Bit
Height = 128
Width = 128
Left = 152
Top = 32
Bitmap = {
494C01012E003100040080008000FFFFFFFF2110FFFFFFFFFFFFFFFF424D3600
0000000000003600000028000000000200000006000001002000000000000000
3000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000
后来尝试在子线程通过线程加载每一帧的图片,以实现动态的效果, 但是造成了频繁向主线程申请资源,造成了初始化程序更慢。
代码如下, 各位还有别的思路吗? unit TRBProcessing;interfaceuses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.ImgList, Vcl.StdCtrls, Vcl.Imaging.GIFImg;type
TRunImage = class(TThread)
private
FBox: TPaintBox;
FImageList: TImageList;
FStep: Integer;
FisSetTransparentColor: Boolean;
protected
procedure Execute; override;
public
constructor Create(ABox: TPaintBox; AImageList: TImageList);
property isSetTransparentColor: Boolean read FisSetTransparentColor write FisSetTransparentColor;
end;type
TTRBFrmProcessing = class(TForm)
ImgRun: TImageList;
ImgDog: TPaintBox;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormShow(Sender: TObject);
private
oRunImage: TRunImage;
{ Private declarations }
protected
public
{ Public declarations }
end;var
TRBFrmProcessing: TTRBFrmProcessing;procedure ProcessingWaitMsg2(AFlag: Integer; AMsg: string = '');const
isProcMsgShow: Integer = 1;
isProcMsgHide: Integer = 0;
isFormTransparentColor: Integer = 2;
isFormNoTransparentColor: Integer = 3;implementation{$R *.dfm}procedure ProcessingWaitMsg2(AFlag: Integer; AMsg: string = '');
begin
if AFlag = isProcMsgHide then
begin
if Application.FindComponent('TRBFrmProcessing') <> nil then
begin
TRBFrmProcessing.TransparentColor := True;
Sleep(100);
TRBFrmProcessing.Refresh;
TRBFrmProcessing.Hide;
TRBFrmProcessing.Close;
FreeAndNil(TRBFrmProcessing);
end;
Exit;
end
else if AFlag = isFormTransparentColor then
begin
if Application.FindComponent('TRBFrmProcessing') <> nil then
begin
TRBFrmProcessing.TransparentColor := True;
TRBFrmProcessing.oRunImage.Suspended:= True;
TRBFrmProcessing.oRunImage.isSetTransparentColor := True; TRBFrmProcessing.oRunImage.Resume;
end;
end
else if AFlag = isFormNoTransparentColor then
begin
if Application.FindComponent('TRBFrmProcessing') <> nil then
begin
TRBFrmProcessing.TransparentColor := False;
end;
end
else
begin
if Application.FindComponent('TRBFrmProcessing') = nil then
begin
TRBFrmProcessing := TTRBFrmProcessing.Create(Application);
end;
TRBFrmProcessing.TransparentColor := True;
TRBFrmProcessing.TransparentColorValue := clBtnFace;
with TRBFrmProcessing do
begin
Show;
Update;
end;
end;
end;{ TRunDog }constructor TRunImage.Create(ABox: TPaintBox; AImageList: TImageList);
begin
FBox := ABox;
FImageList := AImageList;
FStep := 0;
FreeOnTerminate := True;
FisSetTransparentColor := False;
Inherited Create(False);
end;procedure TRunImage.Execute;var
oIcon: TIcon;
oBitMap: TBitmap;
begin
oIcon := TIcon.Create;
oBitMap := TBitmap.Create;
try
repeat
// if FisSetTransparentColor then
// TRBFrmProcessing_Two.TransparentColor := True
// else
// TRBFrmProcessing_Two.TransparentColor := False; FBox.Canvas.Lock;
FImageList.GetIcon(FStep, oIcon);
// FBox.Canvas.Brush.Color := TPanel(FBox.Parent).Color; //clBtnFace;
FBox.Canvas.FillRect(FBox.Canvas.ClipRect);
FBox.Canvas.Draw(0, 0, oIcon);
FBox.Canvas.Unlock;
Application.ProcessMessages; Sleep(50);
FStep := FStep + 1;
if FStep > 45 then
begin
Sleep(100);
FStep := 0;
end;
until Terminated; finally
oIcon.Free;
oBitMap.Free;
end;
end;procedure TTRBFrmProcessing.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
oRunImage.Terminate;
Sleep(20);
TRBFrmProcessing.TransparentColor := True;
TRBFrmProcessing.Color := clBlack;
end;procedure TTRBFrmProcessing.FormShow(Sender: TObject);
begin
oRunImage := TRunImage.Create(ImgDog, ImgRun);
ImgDog.Left := Self.Left;
ImgDog.Top := self.Top;
self.Width := ImgDog.Width;
self.Height := ImgDog.Height;
end;end.dfm
object TRBFrmProcessing: TTRBFrmProcessing
Left = 0
Top = 0
BorderStyle = bsNone
Caption = 'TRBFrmProcessing'
ClientHeight = 166
ClientWidth = 168
Color = clBtnFace
TransparentColor = True
TransparentColorValue = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
FormStyle = fsStayOnTop
OldCreateOrder = False
Position = poMainFormCenter
OnClose = FormClose
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object ImgDog: TPaintBox
Left = 8
Top = 9
Width = 153
Height = 152
end
object ImgRun: TImageList
ColorDepth = cd32Bit
Height = 128
Width = 128
Left = 152
Top = 32
Bitmap = {
494C01012E003100040080008000FFFFFFFF2110FFFFFFFFFFFFFFFF424D3600
0000000000003600000028000000000200000006000001002000000000000000
3000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
00000000000000000000
解决方案 »
- 保存文件使用write和read的方法,不能完全读取出来
- 在FastReport 2.53里面固定单张打印行数时候的问题
- 【请问】这样数据表怎么做一棵树(Treeview)呢?
- s 声明为widestring 提示:String literals may have at most 255 elements 在线等待
- 哪有QUICKREPORT3。5FORD6下载,十万火急
- 请教一个关于版权的问题
- 如何将HTML代码直接写到TWebBrowser里?
- 如何实现在一台计算机上对另一太计算机的监控?(有点难哦)
- 关于sql server 连接不上
- 有DELPHI中文吗?
- 解决照片中电话号码识别问题
- 急急急求高手解决java转delphi7DES加密算法编码转换问题
而你窗体初始化肯定也是主进程里进行的, 也就是说你的初始化是一种主进程的阻塞操作, 此时消息队列没有循环, time消息不会被处理 GIF自然也就不会动一般来说解决方案有2种
1.把操作分块, 执行完一部分就刷新下界面显示进度百分比(类似delphi/photoshop的启动界面)
2.把耗时操作放到线程里, 主进程等待线程结束同时显示GIF(一般等待网络初始化的多用这种方案)
不是反应不过来, 而是你连续执行没有处理消息循环, 所以不触发GIF刷新
FImageList.GetIcon(FStep, oIcon);
// FBox.Canvas.Brush.Color := TPanel(FBox.Parent).Color; //clBtnFace;
FBox.Canvas.FillRect(FBox.Canvas.ClipRect);
FBox.Canvas.Draw(0, 0, oIcon);
FBox.Canvas.Unlock;
**********************************
Canvas画完了,没有刷新
试加上一句刷新:
Canvas.Refresh
而你窗体初始化肯定也是主进程里进行的, 也就是说你的初始化是一种主进程的阻塞操作, 此时消息队列没有循环, time消息不会被处理 GIF自然也就不会动一般来说解决方案有2种
1.把操作分块, 执行完一部分就刷新下界面显示进度百分比(类似delphi/photoshop的启动界面)
2.把耗时操作放到线程里, 主进程等待线程结束同时显示GIF(一般等待网络初始化的多用这种方案)