我从FTP服务器上取的文件列表后,怎么按扩展名和本机的图标联系起来显示。(100分)

  • 主题发起人 主题发起人 乖乖兔
  • 开始时间 开始时间

乖乖兔

Unregistered / Unconfirmed
GUEST, unregistred user!
我从FTP服务器上取的文件列表后,怎么按扩展名和本机的图标联系起来显示。
 
用个mftp控件算了,这东东5个控件,它的tmftplistview和tmftptreeview直接就可以显示
文件图标的。
; TMFtp
; TMFtpSearch
; TMFtpListView
; TMFtpMessenger
; TMFtpTreeView
http://homepages.msn.com/RedmondAve/mftp/
 
procedure TForm1.FormCreate(Sender: TObject);
var
; FileInfo: TSHFileInfo;
begin
; //取得小图标集句柄
; S_ImageListHandle := SHGetFileInfo('C:/',0,FileInfo,
; SizeOf(FileInfo),SHGFI_SYSICONINDEX or SHGFI_SMALLICON);
; //将小图标集句柄指给列表
; SendMessage(Listview1.Handle, LVM_SETIMAGELIST, LVSIL_SMALL, S_ImageListHandle);
end;

使用的时候:
; ListItem:=ListView1.Items.Add;
; FillChar(FileInfo, SizeOf(FileInfo), #0);
; if Ty='<DIR>' then ; //取目录图标
; ; SHGetFileInfo(Pchar(extractfilepath(application.ExeName )),0,FileInfo,sizeof(FileInfo),
; ; ; ; ; ; ; ; ; SHGFI_SYSICONINDEX or SHGFI_SMALLICON)
; else//取文件图标
; ; SHGetFileInfo(Pchar(N),0,FileInfo,sizeof(FileInfo),
; ; ; ; ; ; ; ; ; SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES or SHGFI_SMALLICON);
; ListItem.ImageIndex :=FileInfo.iIcon ;
里面有些变量是我的局部变量,你看一下意思就OK了
 
那多麻烦啊,目录也要自己取图标,特别是系统是没有注册的图标,你还要自己搞个未知
类型文件的图标上去,简直是累死你。而且你这是直接取文件图标,只对于那些自己本身带
图标的文件而言,像txt、word、bmp这些非可执行文件,你直接getfileinfo的话,能取
图标才怪呢。
 
to honghs:
你试过这段程序么?你认为它是直接从文件取图标么?
系统没有注册的图标,系统自己有未知类型图标呀,为什么要自己做?
 
xiao_min:
你这个办法是只有在本机中存在的文件才会有iIcon Idx,
而我只取得了服务器上的文件列表,并没有下载这些文件到本机中,
所以,这个iIcon会一直是0,你这个程序是不实用的。

另外,我提一下,你的取目录图标和取文件图标是可以不分开进行的。
因为在WIN32中用<DIR>来判断是否是一个目录是不可取的。
//----------------------------------------------------
现在我更明白的说明一下我的问题:
比如:我有一个文件名字符串 'bolenl.txt',其实,在磁盘中并不存在这个文件,
就是说这个文件是没有的。那么用什么函数来取得'Txt'这个类型图标的索引。
{我已经取得了系统图标库的句柄}
 
免子兄,你直接用mftpxxx控件组就可以解决问题了,
但如果不想用这个控件的话,你可以这样做,就是直接取注册表中该类型文件所注册的
图标。留下你的email,我把这个控件发给你吧。

把atshell控件的一段代码给你看看吧
1、取远程文件列表,当然直接取不了图标,因为文件还没下载嘛
2、取远程文件列表的扩展名,然后到本地的注册表中找该文件所对应的图标类型,如果有
的话就直接显示出来,如果没有该文件,则用其它图标来代替(你可以把最常用的
exe、com、dll、sys之类的图标设成缺省的嘛)。
function TATShell.ReadIcon: TIcon;
var
; Buff: array[0..255] of char;
; windir,inStr:string;

; function GetIcoIdx(instr:string):integer;
; ;var
; ; ;e:integer;
; ; begin
; ; ; result:=0;
; ; ; e:=pos(',',instr);
; ; ; if e<>0 then
; ; ; ; begin
; ; ; ; ;try
; ; ; ; ; ;result:=strtoint(trim(copy(instr,e+1,length(instr))));
; ; ; ; ;except
; ; ; ; ; ;result:=0;
; ; ; ; ;end;
; ; ; ; end;
; ; end;
;function GetIco(s:string):string;
; ;var
; ; ;e:integer;
; ; begin
; ; ; result:=s;
; ; ; e:=pos(',',s);
; ; ; if e<>0 then
; ; ; ; ;result:=copy(s,1,e-1);
; ; end;
begin
; { Create the new icon }
; Result := TIcon.Create;
; GetAssociation;
; instr:=IconPath;
; {get windows dir}
; GetWindowsDirectory(buff, 255);
; {convert to string}
; windir:=strpas(buff);
; if not fileexists(getico(instr)) then
; ;begin
; ; {no file to get icon from so use apps icon}
; ; Result.Handle := ExtractIcon(Application.Mainform.Handle, strpCopy(buff,PathtoApp), 0);
; ;if (Result.Handle<2) then
; ; Result.Handle := ExtractIcon(Application.Mainform.Handle, strpCopy(buff,Windir+'/system/shell32.dll'), 0);
; ;end
; else
; ; { Assign it the icon handle }
; ; ;Result.Handle := ExtractIcon(Application.Mainform.Handle, StrPCopy(Buff, GetIco(instr)), GetIcoIdx(instr));
end;
 
天哪
你们有没有真正试过呀,我是试过的,这是从我的FTP程序里COPY下来的,都成功了的
这种方法只是取扩展名所示文件的图标,完全完全不需要文件存在呀。。。。

气愤。。。。不回答了。。。
 
刚查了msdn,看来你的代码是正确的。我以前只用过Extracticon,没想到shgetfileino
也可以取文件的图标。真是不好意思啦!!!!
SHGetFileInfo

WINSHELLAPI DWORD WINAPI SHGetFileInfo(
; ; LPCTSTR pszPath,
; ; DWORD dwFileAttributes,
; ; SHFILEINFO FAR *psfi,
; ; UINT cbFileInfo,
; ; UINT uFlags
);

Retrieves information about an object in the file system, such as a file, a folder, a directory, or a drive root.

Returns a value whose meaning depends on the uFlags parameter. If uFlags contains the SHGFI_EXETYPE flag, the return value specifies the type of the executable file. It will be one of the following values: Value ;Executable File Type
0 ;Nonexecutable file or an error condition
LOWORD = NE or PE and HIWORD = 3.0, 3.5, or 4.0 ;Windows application
LOWORD = MZ and HIWORD = 0 ;MS-DOS .exe, .com, or .bat file
LOWORD = PE and HIWORD = 0 ;Win32 console application

If uFlags contains SHGFI_SYSICONINDEX, the return value is the handle to the system image list that contains the large icon images. If SHGFI_SMALLICON is included with SHGFI_SYSICONINDEX, the return value is the handle to the image list that contains the small icon images.

If uFlags does not contain SHGFI_EXETYPE or SHGFI_SYSICONINDEX, the return value is nonzero if successful, or zero otherwise.

pszPath
Address of a buffer that contains the path and file name. Both absolute and relative paths are valid.
If the uFlags parameter includes the SHGFI_PIDL flag, this parameter must be the address of an ITEMIDLIST (PIDL) structure that contains the list of item identifiers that uniquely identifies the file within the shell's namespace. The PIDL must be a fully qualified PIDL. Relative PIDLs are not allowed.

If the uFlags parameter includes the SHGFI_USEFILEATTRIBUTES flag, this parameter does not have to be a valid file name. The function will proceed as if the file exists with the specified name and with the file attributes passed in the dwFileAttributes parameter. This allows you to obtain information about a file type by passing just the extension for pszPath and passing FILE_ATTRIBUTE_NORMAL in dwFileAttributes.

This string can use either short (the 8.3 form) or long file names.

dwFileAttributes
Combination of one or more file attribute flags (FILE_ATTRIBUTE_ values). If uFlags does not include the SHGFI_USEFILEATTRIBUTES flag, this parameter is ignored.
psfi
Address of a SHFILEINFO structure to receive the file information.
cbFileInfo
Size, in bytes, of the SHFILEINFO structure pointed to by the psfi parameter.
uFlags
Flags that specify the file information to retrieve. This parameter can be a combination of the following values: SHGFI_ATTR_SPECIFIED ;Modifies SHGFI_ATTRIBUTES. Indicates that the dwAttributes member of the SHFILEINFO structure at psfi contains the specific attributes that are desired. These attributes will be passed to IShellFolder::GetAttributesOf. If this flag is not specified, 0xFFFFFFFF will be passed to GetAttributesOf, requesting all attributes. This flag cannot be specified with the SHGFI_ICON flag. ;
SHGFI_ATTRIBUTES ;Retrieve the item attributes. The attributes are copied to the dwAttributes member of the structure specified in the psfi parameter. These are the same attributes that are obtained from IShellFolder::GetAttributesOf. ;
SHGFI_DISPLAYNAME ;Retrieve the display name for the file. The name is copied to the szDisplayName member of the structure specified in psfi. The returned display name uses the long file name, if there is one, rather than the 8.3 form of the file name. ;
SHGFI_EXETYPE ;Retrieve the type of the executable file if pszPath identifies an executable file. This flag cannot be specified with any other flags. ;
SHGFI_ICON ;Retrieve the handle to the icon that represents the file and the index of the icon within the system image list. The handle is copied to the hIcon member of the structure specified by psfi, and the index is copied to the iIcon member. The return value is the handle to the system image list. ;
SHGFI_ICONLOCATION ;Retrieve the name of the file that contains the icon representing the file. The name is copied to the szDisplayName member of the structure specified in psfi. ;
SHGFI_LARGEICON ;Modify SHGFI_ICON, causing the function to retrieve the file's large icon. ;
SHGFI_LINKOVERLAY ;Modify SHGFI_ICON, causing the function to add the link overlay to the file's icon. ;
SHGFI_OPENICON ;Modify SHGFI_ICON, causing the function to retrieve the file's open icon. A container object displays an open icon to indicate that the container is open. ;
SHGFI_PIDL ;Indicate that pszPath is the address of an ITEMIDLIST structure rather than a path name. ;
SHGFI_SELECTED ;Modify SHGFI_ICON, causing the function to blend the file's icon with the system highlight color. ;
SHGFI_SHELLICONSIZE ;Modify SHGFI_ICON, causing the function to retrieve a shell-sized icon. If this flag is not specified the function sizes the icon according to the system metric values. ;
SHGFI_SMALLICON ;Modify SHGFI_ICON, causing the function to retrieve the file's small icon. ;
SHGFI_SYSICONINDEX ;Retrieve the index of the icon within the system image list. The index is copied to the iIcon member of the structure specified by psfi. The return value is the handle to the system image list. ;
SHGFI_TYPENAME ;Retrieve the string that describes the file's type. The string is copied to the szTypeName member of the structure specified in psfi. ;
SHGFI_USEFILEATTRIBUTES ;Indicates that the function should not attempt to access the file specified by pszPath. Rather, it should act as if the file specified by pszPath exists with the file attributes passed in dwFileAttributes. This flag cannot be combined with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, or SHGFI_PIDL flags. ;


--------------------------------------------------------------------------------
 
xiao_min:
你不要气麻,大家都还不是再讨论问题嘛:)
但是我还是要说,
SHGetFileInfo(Pchar(N),0,FileInfo,sizeof(FileInfo),
; SHGFI_SYSICONINDEX or SHGFI_USEFILEATTRIBUTES or SHGFI_SMALLICON);
这个函数,这个 Pchar(N) 的 N,如果是一个本地磁盘中不存在的文件名,那么
取的 FileInfo.iIocn 值绝对是0。不信你试试。

honghs:
我的邮箱是 bolenl@etang.com,先谢谢你了/
 
我试过的,绝对不是0
 
好了,问题解决了。
哎,我真的是错了。
原来我是少了 SHGFI_USEFILEATTRIBUTES 这个参数。
谢谢 xiao_min 了。非常!!!
也谢谢 honghs 。
 
接受答案了.
 
后退
顶部