转帖: 创建像ACDSee那样的缩略图浏览器,这并不是一件简单的事。以下就是一个很原始的缩略图控件的源代码:uses jpegtype tbi_thumb=class(tcustomcontrol) fmypic : tjpegimage; private procedure getpic(value:tjpegimage); public procedure paint; override; constructor create(aowner:tcomponent); override; destructor destroy; override; published property pic:tjpegimage write getpic; end;constructor tbi_thumb.create(aowner:tcomponent); begin inherited; fmypic:=tjpegimage.create; end;destructor tbi_thumb.destroy; begin inherited; fmypic.free; end;procedure tbi_thumb.getpic(value:tjpegimage); begin fmypic.scale:=jshalf; fmypic.assign(value); fmypic.dibneeded; end;procedure tbi_thumb.paint; var arect : trect; ratio : single; begin arect:=clientrect; canvas.stretchdraw(arect,fmypic); frame3d(canvas,arect,clblack,clwhite,2); end;(这不是一个可视控件。)下面来实现新建一个Form,并在其上添加一个Directory List Box和一个Scrollbox,其中Scrollbox的宽度(Width)大约为430个象素。 当用户选定了一个新的目录,我们需要扫描这个目录,为其中每一个JPEG文件创建一个实例。为了加快这一过程的速度,可以将JPEG参数设为jpbestspeed。JPEG文件将被读入一个临时缓冲区,然后被赋值给一个新的缩略图对象的pic属性。在unit中添加一个新的变量: var buffer_jpeg:tjpegimage;下面的代码用来创建和释放buffer_jpeg变量:procedure TForm1.FormCreate(Sender: TObject); begin buffer_jpeg:=tjpegimage.create; buffer_jpeg.Performance:=jpbestspeed; end;procedure TForm1.FormDestroy(Sender: TObject); begin buffer_jpeg.free; while scrollbox1.ComponentCount>0 do scrollbox1.components[0].free; end;以下的代码在文件夹中检索JPEG文件并创建缩略图:procedure TForm1.DirectoryListBox1Change(Sender: TObject); var i,x,y : integer; anewthumb : tbi_thumb; foundlist : tstringlist; sr : TSearchRec;begin foundlist:=tstringlist.create; try //检索JPEG文件 if FindFirst(directorylistbox1.Directory+'\*.jpg', faAnyFile, sr) = 0 then begin foundlist.add(sr.name); while FindNext(sr) = 0 do foundlist.add(sr.name); FindClose(sr); end; x:=0; y:=0; //创建缩略图 for i:=0 to FoundList.count-1 do begin anewthumb:=tbi_thumb.create(self); buffer_jpeg.loadfromfile(foundlist[i]); with anewthumb do begin pic:=buffer_jpeg; parent:=scrollbox1; left:=x; top:=y; width:=120; height:=90; visible:=true; inc(x,122); if x>244 then begin x:=0; inc(y,92); end; application.processmessages; end; end; finally foundlist.free; end;end;以上就是一个极简单的缩略图浏览器实现,你可以继续在里面添加需要的功能。
创建像ACDSee那样的缩略图浏览器,这并不是一件简单的事。以下就是一个很原始的缩略图控件的源代码:uses jpegtype
tbi_thumb=class(tcustomcontrol)
fmypic : tjpegimage;
private
procedure getpic(value:tjpegimage);
public
procedure paint; override;
constructor create(aowner:tcomponent); override;
destructor destroy; override;
published
property pic:tjpegimage write getpic;
end;constructor tbi_thumb.create(aowner:tcomponent);
begin
inherited;
fmypic:=tjpegimage.create;
end;destructor tbi_thumb.destroy;
begin
inherited;
fmypic.free;
end;procedure tbi_thumb.getpic(value:tjpegimage);
begin
fmypic.scale:=jshalf;
fmypic.assign(value);
fmypic.dibneeded;
end;procedure tbi_thumb.paint;
var
arect : trect;
ratio : single;
begin
arect:=clientrect;
canvas.stretchdraw(arect,fmypic);
frame3d(canvas,arect,clblack,clwhite,2);
end;(这不是一个可视控件。)下面来实现新建一个Form,并在其上添加一个Directory List Box和一个Scrollbox,其中Scrollbox的宽度(Width)大约为430个象素。
当用户选定了一个新的目录,我们需要扫描这个目录,为其中每一个JPEG文件创建一个实例。为了加快这一过程的速度,可以将JPEG参数设为jpbestspeed。JPEG文件将被读入一个临时缓冲区,然后被赋值给一个新的缩略图对象的pic属性。在unit中添加一个新的变量:
var
buffer_jpeg:tjpegimage;下面的代码用来创建和释放buffer_jpeg变量:procedure TForm1.FormCreate(Sender: TObject);
begin
buffer_jpeg:=tjpegimage.create;
buffer_jpeg.Performance:=jpbestspeed;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
buffer_jpeg.free;
while scrollbox1.ComponentCount>0 do
scrollbox1.components[0].free;
end;以下的代码在文件夹中检索JPEG文件并创建缩略图:procedure TForm1.DirectoryListBox1Change(Sender: TObject);
var
i,x,y : integer;
anewthumb : tbi_thumb;
foundlist : tstringlist;
sr : TSearchRec;begin
foundlist:=tstringlist.create; try
//检索JPEG文件
if FindFirst(directorylistbox1.Directory+'\*.jpg', faAnyFile, sr) = 0 then
begin
foundlist.add(sr.name);
while FindNext(sr) = 0 do
foundlist.add(sr.name);
FindClose(sr);
end; x:=0;
y:=0; //创建缩略图
for i:=0 to FoundList.count-1 do
begin
anewthumb:=tbi_thumb.create(self);
buffer_jpeg.loadfromfile(foundlist[i]);
with anewthumb do
begin
pic:=buffer_jpeg;
parent:=scrollbox1;
left:=x;
top:=y;
width:=120;
height:=90;
visible:=true;
inc(x,122);
if x>244 then
begin
x:=0;
inc(y,92);
end;
application.processmessages;
end;
end; finally
foundlist.free;
end;end;以上就是一个极简单的缩略图浏览器实现,你可以继续在里面添加需要的功能。