unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, ImgList,shellapi;
type
TForm1 = class(TForm)
ListView1: TListView;
ImageListLarge: TImageList;
OpenDialog1: TOpenDialog;
Button2: TButton;
ImageListSmall: TImageList;
BvsIcon: TButton;
BvsSmallIcon: TButton;
BvsList: TButton;
BvsReport: TButton;
procedure Button2Click(Sender: TObject);
procedure BvsIconClick(Sender: TObject);
procedure BvsSmallIconClick(Sender: TObject);
procedure BvsListClick(Sender: TObject);
procedure BvsReportClick(Sender: TObject);
procedure ListView1DblClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
procedure WMDROPFILES_L(var Msg: TMessage);
procedure LBWindowProc(var Message: TMessage);
public
{ Public declarations }
function GetFileSize(PFileName:string):int64;
procedure SetListviewFile(PFileName,Paddreadme:string;Plistview:Tlistview) ;
procedure ListViewAddIcon( IconHandle: HIcon;Plistview:Tlistview );
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
LWindowProc: TWndMethod;
procedure TForm1.Button2Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
//首先判断文件是否存在;
//加入打开日记的模块中
SetListviewFile(OpenDialog1.FileName,'手动打开加入的文件!',listview1);
end;
end;
function TForm1.GetFileSize(PFileName: string): int64;
var
hd : integer;
begin
try
hd:=fileopen(PFileName,fmShareDenyNone);
//要用 fmShareDenyNone,这样别的程序也能打开文件,
result:=fileseek(hd,0,2);
fileclose(hd);
except
result:=0;
fileclose(hd);
end;
end;
procedure TForm1.ListViewAddIcon( IconHandle: HIcon;Plistview:Tlistview );
var
aIcon: TIcon;
begin
// 将图标加入ListView //保留,加入函数库
if IconHandle <> 0 then
try
aIcon := TIcon.Create;
aIcon.Handle := IconHandle;
with Plistview.Items.Add do
begin
ImageIndex := Plistview.LargeImages.Count;
Plistview.LargeImages.InsertIcon( ImageIndex, aIcon );
Plistview.SmallImages.InsertIcon( ImageIndex, aIcon );
end;
finally
FreeAndNIL( aIcon );
end;
end;
procedure TForm1.SetListviewFile(PFileName, Paddreadme: string;Plistview:Tlistview);
var
info : tshfileinfo;
aIcon: TIcon;
TMPID: integer;
begin
fillchar(info,sizeof(tshfileinfo),0);
shgetfileinfo(pchar(PFileName),0,info,sizeof(info),shgfi_displayname or shgfi_typename or
shgfi_icon or shgfi_attributes);
with Plistview do
begin
Items.Add;
aIcon := TIcon.Create; //图标处理
aIcon.Handle := info.hicon;
TMPID :=ListView1.Items.Count-1;
Plistview.Items[TMPID].ImageIndex := Plistview.LargeImages.Count;
Plistview.LargeImages.InsertIcon( Plistview.Items[TMPID].ImageIndex, aIcon );
Plistview.SmallImages.InsertIcon( Plistview.Items[TMPID].ImageIndex, aIcon );
Plistview.Items[TMPID].Caption := info.szdisplayname;//文件名
Plistview.Items[TMPID].SubItems.add(Paddreadme); //文件说明
Plistview.Items[TMPID].SubItems.add(inttostr((GetFileSize(PFileName)+1023)div 1024)+'KB');//大小
Plistview.Items[TMPID].SubItems.add(info.sztypename); //文件类型
Plistview.Items[TMPID].SubItems.add(FormatDateTime('YY-MM-DD HH:MM',FileDateToDateTime(FileAge(PFileName))));;//修改日期
FreeAndNIL( aIcon );
end;
end;
procedure TForm1.BvsIconClick(Sender: TObject);
begin
ListView1.ViewStyle := vsIcon;
end;
procedure TForm1.BvsSmallIconClick(Sender: TObject);
begin
ListView1.ViewStyle := vsSmallIcon;
end;
procedure TForm1.BvsListClick(Sender: TObject);
begin
ListView1.ViewStyle := vsList;
end;
procedure TForm1.BvsReportClick(Sender: TObject);
begin
ListView1.ViewStyle := vsReport;
end;
procedure TForm1.ListView1DblClick(Sender: TObject);
begin
//双击或功能按钮打开文件
with sender as Tlistview do
begin
if selected= nil then exit;
showmessage(inttostr(ListView1.ItemIndex));
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
LWindowProc := ListView1.WindowProc;
ListView1.WindowProc := LBWindowProc;
DragAcceptFiles(ListView1.Handle, True);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ListView1.WindowProc := LWindowProc;
DragAcceptFiles(ListView1.Handle, False);
end;
procedure TForm1.LBWindowProc(var Message: TMessage);
begin
if Message.Msg = WM_DROPFILES then
WMDROPFILES_l(Message);
LWindowProc(Message);
end;
procedure TForm1.WMDROPFILES_L(var Msg: TMessage);
var
pcFileName: PChar;
i, iSize, iFileCount: integer;
begin
pcFileName := '';
iFileCount := DragQueryFile(Msg.wParam, $FFFFFFFF, pcFileName, 255);
for i := 0 to iFileCount - 1 do
begin
iSize := DragQueryFile(Msg.wParam, i, nil, 0) + 1;
pcFileName := StrAlloc(iSize);
DragQueryFile(Msg.wParam, i, pcFileName, iSize);
if FileExists(pcFileName) then
SetListviewFile(pcFileName,'拖放加入的文件',listview1);
StrDispose(pcFileName);
end;
DragFinish(Msg.wParam);
end;
end.