根据一个字符串(如“aaa.doc”)获得windows内与之相对应的图标的问题!超级疑难,非高手莫入! (200分)

  • 主题发起人 主题发起人 chemstar
  • 开始时间 开始时间
FUNCTION FtImageListAdd(vFile:STRING;vImageList:TImageList;vForm:TForm):INTEGER;<br>VAR icoTemp:TIcon; imgList:TImageList; tshIcon: TSHFileInfo;<br>BEGIN<br>&nbsp; &nbsp; icoTemp:=TIcon.Create;<br>&nbsp; &nbsp; imgList:=TImageList.Create(vForm);//<br>&nbsp; &nbsp; imgList.Handle:=SHGetFileInfo(PCHAR(vFile),0,tshIcon,SizeOf(tshIcon),SHGFI_SYSICONINDEX+SHGFI_SMALLICON);<br>&nbsp; &nbsp; imgList.ShareImages:=TRUE;<br>&nbsp; &nbsp; imgList.GetIcon(tshIcon.iIcon,icoTemp);<br>&nbsp; &nbsp; Result:=vImageList.AddIcon(icoTemp);<br>&nbsp; &nbsp; icoTemp.Free;<br>&nbsp; &nbsp; imgList.Free;<br>END;<br>第二个问题.<br><br>如果你想做类似文件夹流览器建议使用 控件.<br>关于PIDL因所设计的问题太多,推荐看一下,<br><br>{这不是我写的}<br>implementation<br><br>uses<br>&nbsp; FileCtrl, ShellAPI, ActiveX, ShlObj, ComObj;<br><br>{$R *.DFM}<br><br>var<br>&nbsp; ShellMalloc: IMalloc;<br>&nbsp; DesktopFolder: IShellFolder;<br>&nbsp; CS : TRTLCriticalSection;<br>&nbsp; FileInfo: TSHFileInfo;<br><br>function NextPIDL(PIDL: PItemIDList): PItemIDList;<br>begin<br>&nbsp; Result := PIDL;<br>&nbsp; Inc(PChar(Result), PIDL^.mkid.cb);<br>end;<br><br>function GetPIDLSize(PIDL: PItemIDList): Integer;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; if Assigned(PIDL) then begin<br>&nbsp; &nbsp; Result := SizeOf(PIDL^.mkid.cb);<br>&nbsp; &nbsp; while PIDL^.mkid.cb &lt;&gt; 0 do begin<br>&nbsp; &nbsp; &nbsp; Result := Result + PIDL^.mkid.cb;<br>&nbsp; &nbsp; &nbsp; PIDL := NextPIDL(PIDL);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>function CreatePIDL(Size: Integer): PItemIDList;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; Result := nil;<br>&nbsp; &nbsp; Result := ShellMalloc.Alloc(Size);<br>&nbsp; &nbsp; if Assigned(Result) then FillChar(Result^, Size, 0);<br>&nbsp; finally<br>&nbsp; end;<br>end;<br><br>procedure FreePIDL(PIDL: PItemIDList);<br>begin<br>&nbsp; if PIDL &lt;&gt; nil then ShellMalloc.Free(PIDL);<br>end;<br><br>function ConcatPIDLs(PIDL1, PIDL2: PItemIDList): PItemIDList;<br>var cb1, cb2: Integer;<br>begin<br>&nbsp; if Assigned(PIDL1) then cb1 := GetPIDLSize(PIDL1) - SizeOf(PIDL1^.mkid.cb) else cb1 := 0;<br>&nbsp; cb2 := GetPIDLSize(PIDL2);<br>&nbsp; Result := CreatePIDL(cb1 + cb2);<br>&nbsp; if Assigned(Result) then begin<br>&nbsp; &nbsp; if Assigned(PIDL1) then CopyMemory(PChar(Result), PIDL1, cb1);<br>&nbsp; &nbsp; CopyMemory(PChar(Result) + cb1, PIDL2, cb2);<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; PIDL, RelativePIDL, NewPIDL: PItemIDList;<br>&nbsp; Eaten, Attributes: Cardinal;<br>&nbsp; path: string;<br>&nbsp; P: PWideChar;<br>&nbsp; ShellFolder: IShellFolder;<br>&nbsp; EnumIDList: IEnumIDList;<br>&nbsp; NumIDs: Cardinal;<br>begin<br>&nbsp; // 设置要显示的路径,取其 PItemIDList<br>&nbsp; path := 'C:/Documents and Settings/Administrator/My Documents/Delphi_Tmp';<br>&nbsp; Eaten := Length(path);<br>&nbsp; P := StringToOleStr(path);<br>&nbsp; Attributes := 0;<br>&nbsp; OLECheck(DesktopFolder.ParseDisplayName(Application.Handle, nil, P, Eaten, PIDL, Attributes));<br>&nbsp; // 根据 PItemIDList 得到 IShellFolder<br>&nbsp; // 可以直接取文件的PIDL,但是存在与以前一样的现象,现在取其目录<br>&nbsp; DesktopFolder.BindToObject(PIDL, nil, IID_IShellFolder, Pointer(ShellFolder));<br>&nbsp; // 枚举目录下的文件到 EnumIDList<br>&nbsp; ShellFolder.EnumObjects(Application.Handle, SHCONTF_NONFOLDERS, EnumIDList);<br>&nbsp; NewPIDL := nil;<br>&nbsp; while EnumIDList.Next(1, RelativePIDL, NumIDs) = S_OK do<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; // 将列出的文件PIDL与目录合并得到文件的 PItemIDList<br>&nbsp; &nbsp; &nbsp; NewPIDL := ConcatPIDLs(PIDL, RelativePIDL);<br>&nbsp; &nbsp; &nbsp; // 根据合并出的完整文件PItemIDList取文件名和属性!<br>&nbsp; &nbsp; &nbsp; FillChar(FileInfo, SizeOf(FileInfo), 0);<br>&nbsp; &nbsp; &nbsp; SHGetFileInfo(PChar(NewPIDL), 0, FileInfo, SizeOf(FileInfo), SHGFI_PIDL or SHGFI_DISPLAYNAME or SHGFI_TYPENAME);<br>&nbsp; &nbsp; &nbsp; ListBox1.Items.Add( FileInfo.szDisplayName + ' - ' + FileInfo.szTypeName );<br>&nbsp; &nbsp; finally<br>&nbsp; &nbsp; &nbsp; FreePIDL(NewPIDL);<br>&nbsp; &nbsp; &nbsp; FreePIDL(RelativePIDL);<br>&nbsp; &nbsp; end;<br>end;<br><br>initialization<br>&nbsp; OLECheck(SHGetDesktopFolder(DesktopFolder));<br>&nbsp; InitializeCriticalSection(CS);<br>&nbsp; OleInitialize(nil);<br>&nbsp; OLECheck(ShGetMalloc(ShellMalloc));<br><br>finalization<br>&nbsp; DesktopFolder := nil;<br>&nbsp; ShellMalloc := nil;<br>&nbsp; OleUninitialize;<br>&nbsp; DeleteCriticalSection(CS);<br>end.<br><br>新的一个问题,说起来容易做起来难. <br>用SaveToStream(); 和LoadfromStream();存取TStream变量.<br><br>to zjfeng, 快捷方式那个图标确实是叠加的.<br>
 
to 我爱PASCAL<br>我这个函数完全满足你的要求:<br>function GetFileIcon(const DocFileNameExt: string): string; <br>var <br>FileClass: string; <br>Reg: TRegistry; <br>begin <br>Result := ''; <br>Reg := TRegistry.Create(KEY_EXECUTE); <br>Reg.RootKey := HKEY_CLASSES_ROOT; <br>FileClass := ''; <br>if Reg.OpenKeyReadOnly(DocFileNameExt) then //在我的计算机上这句话打开不成功,不知道为什么<br>begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //我只好用openkey打开 &nbsp;<br>FileClass := Reg.ReadString(''); <br>Reg.CloseKey; <br>end; <br>if FileClass &lt;&gt; '' then begin <br>if Reg.OpenKeyReadOnly(FileClass + '/Shell/DefaultIcon') then //这个地方好像不对,应该不要shell,是不是<br>begin <br>Result := Reg.ReadString(''); <br>Reg.CloseKey; <br>end; <br>end; <br>Reg.Free; <br>end;<br><br>
 
to chemstar<br>&nbsp; 我没有说哪个函数错了,我只是说SHGetFileInfo这个函数只能得到<br>&nbsp; 实际存在的文件的信息
 
var fileinfo:shfileinfo;<br>shgetfileinfo(pchar('wypeng.doc'),0,fileinfo,sizeof(fileinfo),shgfi_icon);<br>listfile.image1.Picture.Icon.Handle:=fileinfo.hIcon;//返回文件的图标<br>
 
别忘了加分噢!!!<br>Delphi 编写Windows 资源管理器<br><br>编写资源管理器,Delphi 有最简单的方法,就是在form上加上一个<br>TShellComboBox1控件,一个TShellTreeView1控件和一个TShellListView1控件。然后分别指定他们的属性值为对应的属性就可以了。<br>但是这不是一个程序员所要的。因为你不能对上述的这些内容进行操作,所以,我<br>在这里要说的是第二种,也就是最为激动人心的有自己写的代码来实现以上的功能。<br>用程序实现的好处是控制随心所欲。这把这种方法用到了我写的木马当中。<br>在服务器端有我的一个服务程序,在我的机子上有我的另一个客户端程序。<br>当我要用我的客户端连接服务器端的时候,比如202.206.242.119,服务器端就把<br>本机(202.206.242.119)上的所有磁盘信息发送给我。(比如有c:,d:,e:,f:....)<br>当我在本地(我的机子)上点击"c:"时候,服务器端就把202.206.242.119上的<br>c:盘上的所有文件和目录发送给我,我一目了然。我还可以在他的机子上新建、删除<br>运行等各种文件。好不好玩啊?~_~当然,他首先得运行我的木马程序。<br>国内赫赫有名的"冰河"我想大家都用过吧,不错吧。就像是管理自己机子的资料管理器,好了,不再废话了,让我们开始这激动人心的一刻吧!<br><br><br>首先要明白程序的流程,不然我不是在这瞎说一桶吗?<br><br>第一步就是找到机子上的所有的硬盘,第二就是找到机子上对应硬盘下的所有文件和图标。<br>找硬盘的方法很多,我用的是Winapi函数:getdrivetype,因为它比较简单。<br>打文件就是findfirst和findnext就可以了。<br>剩下的就是怎样向ttreeview中填充了。 <br>好了,开始吧!<br><br>新建一个工程,然后窗体的caption为"资源管理器"<br>在窗体上添加一个TTreeview控件命名为dir,一个tlistview控件命名为wfile,两个tpopupmenu控件<br>两个timagelist控件.dir 对应第一个tpopupmemu,wfile对应第二个tpopupmemu。<br>第一个tpopupmemu的Items分别为:删除,新建文件夹,重命名。<br>第二个tpopupmemu的Items分别为:查看,删除,新建文件,新建文件夹,重命名。其中,查看的子项为:<br>大图标,小图标,列表和详细资料。<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, ExtCtrls, ComCtrls, ImgList,shellapi, StdCtrls, Buttons, Menus,<br>&nbsp; Grids, DBGrids;<br><br>type<br>&nbsp; Tlistfile = class(TForm)<br>&nbsp; &nbsp; dir: TTreeView;<br>&nbsp; &nbsp; Wfile: TListView;<br>&nbsp; &nbsp; Splitter1: TSplitter;<br>&nbsp; &nbsp; ImageList1: TImageList;<br>&nbsp; &nbsp; PopupMenu1: TPopupMenu;<br>&nbsp; &nbsp; N1: TMenuItem;<br>&nbsp; &nbsp; Image1: TImage;<br>&nbsp; &nbsp; ImageList2: TImageList;<br>&nbsp; &nbsp; N2: TMenuItem;<br>&nbsp; &nbsp; R1: TMenuItem;<br>&nbsp; &nbsp; PopupMenu2: TPopupMenu;<br>&nbsp; &nbsp; D1: TMenuItem;<br>&nbsp; &nbsp; N3: TMenuItem;<br>&nbsp; &nbsp; R2: TMenuItem;<br>&nbsp; &nbsp; M1: TMenuItem;<br>&nbsp; &nbsp; zt: TStatusBar;<br>&nbsp; &nbsp; V1: TMenuItem;<br>&nbsp; &nbsp; N4: TMenuItem;<br>&nbsp; &nbsp; N5: TMenuItem;<br>&nbsp; &nbsp; N6: TMenuItem;<br>&nbsp; &nbsp; M2: TMenuItem;<br>&nbsp; &nbsp; L1: TMenuItem;<br>&nbsp; &nbsp; N7: TMenuItem;<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure dirCollapsed(Sender: TObject; Node: TTreeNode);<br>&nbsp; &nbsp; procedure dirExpanded(Sender: TObject; Node: TTreeNode);<br>&nbsp; &nbsp; procedure WfileDblClick(Sender: TObject);<br>&nbsp; &nbsp; procedure dirClick(Sender: TObject);<br>&nbsp; &nbsp; procedure N1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure dirEdited(Sender: TObject; Node: TTreeNode; var S: String);<br>&nbsp; &nbsp; procedure WfileEdited(Sender: TObject; Item: TListItem; var S: String);<br>&nbsp; &nbsp; procedure dirKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);<br>&nbsp; &nbsp; procedure D1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure R2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure M1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure L1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure M2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure N7Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; procedure filedir(dirname:string;node:ttreenode);//查找文件<br>&nbsp; &nbsp; procedure wmdropfiles(var msg:twmdropfiles);message wm_dropfiles;<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; listfile: Tlistfile;<br>&nbsp; fileinfo:shfileinfo;<br>&nbsp; f:array [0..0,0..2] of string;<br>&nbsp; ListItem: TListItem;<br>&nbsp; filename,names:string;<br>&nbsp; pathname:string;<br>&nbsp; node1,node3:ttreenode;<br>implementation<br><br>{$R *.dfm}<br><br>//以上是窗体上的控件和属性.<br><br>function IsValidDir(SearchRec:TSearchRec):Boolean;<br>begin<br>&nbsp; if ((SearchRec.Attr=16)or (searchrec.Attr =17)or<br>&nbsp; &nbsp; &nbsp;(searchrec.Attr =18)or (searchrec.Attr =22)<br>&nbsp; &nbsp; &nbsp;or (searchrec.Attr =49)or (searchrec.Attr =48))<br>&nbsp; &nbsp; &nbsp; and (SearchRec.Name&lt;&gt;'.')<br>&nbsp; &nbsp; &nbsp;and (SearchRec.Name&lt;&gt;'..') then<br>&nbsp; &nbsp; &nbsp; Result:=True<br>&nbsp; else<br>&nbsp; &nbsp; &nbsp;Result:=False;<br>&nbsp; //showmessage(inttostr(searchrec.Attr ));<br>end;<br>////////以上是一个函数,功能是判断一个文件是不是文件夹。<br><br>function panel(node:ttreenode):string;<br>var str:string;<br>node2:ttreenode;<br>begin<br>&nbsp; try<br>&nbsp; node2:=node.Parent;//返回父节点<br>&nbsp; str:=node2.Text+str;<br>&nbsp; node1:=node2;<br><br>&nbsp; filename:=str+'/'+filename;<br>&nbsp; panel(node2);<br>&nbsp; except<br>&nbsp; &nbsp; panel:=filename;<br>&nbsp; end;<br>end;<br>///////一个递归函数,找到node的父节点。<br><br>procedure tlistfile.wmdropfiles(var msg:twmdropfiles);<br>var numfiles:longint;<br>&nbsp; &nbsp;i:longint;<br>&nbsp; &nbsp;buffer:array[0..255] of char;<br>begin<br>&nbsp; showmessage('asdfasdf');<br>end;<br>////////用兴趣的朋友可以在这处理鼠标拖放,本人没有处理,只是打到了。<br><br>procedure tlistfile.filedir(dirname:string;node:ttreenode);<br>var searchrec:tsearchrec;<br>&nbsp; &nbsp; filename:string;<br>&nbsp; &nbsp; node1:ttreenode;<br>&nbsp; &nbsp; i:integer;<br>begin<br>&nbsp; try<br>&nbsp; for i:=node.Count -1 downto 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; node.Item .Delete ;<br>&nbsp; &nbsp; end;<br>&nbsp; except<br>&nbsp; end;<br>&nbsp; filename:=dirname+'/*.*';<br>&nbsp; wfile.Clear;<br>&nbsp; listfile.ImageList2.Clear;<br>&nbsp; listitem:=wfile.Items.Add;<br>&nbsp; listitem.Caption:='..';<br>&nbsp; if findfirst(filename,faAnyFile,searchrec)=0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; while findnext(searchrec)=0 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if IsValidDir(searchrec) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node1:=listfile.dir.Items.AddChild(node,searchrec.Name );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node1.ImageIndex:=1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node1.SelectedIndex:=2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listfile.dir.Items.AddChild(node1,'' );<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shgetfileinfo(pchar(dirname+'/'+searchrec.Name ),0,fileinfo,sizeof(fileinfo),shgfi_icon);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listfile.image1.Picture.Icon.Handle:=fileinfo.hIcon;//返回文件的图标<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listfile.ImageList2.AddIcon(listfile.image1.Picture.Icon);//添加图标<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,0]:=searchrec.Name;//+' &nbsp; '+inttostr(searchrec.Attr );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,1]:=inttostr(searchrec.Size);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,2]:=datetimetostr(filedatetodatetime(searchrec.Time ));<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem:=wfile.Items.Insert(1);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.Caption:=f[0,0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.SubItems.Add('');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.SubItems.add(f[0,2]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wfile.Items[1].ImageIndex:=imagelist2.Count-1 ;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wfile.Items[0].ImageIndex:=imagelist2.Count-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if searchrec.Name &lt;&gt;'..' then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,0]:=searchrec.Name;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,1]:=inttostr(searchrec.Size);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,2]:=datetimetostr(filedatetodatetime(searchrec.Time ));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem:=listfile.wfile.Items.Add;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.Caption:=f[0,0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.SubItems.Add(f[0,1]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.SubItems.Add(f[0,2]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;shgetfileinfo(pchar(dirname+'/'+searchrec.Name ),0,fileinfo,sizeof(fileinfo),shgfi_icon);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listfile.image1.Picture.Icon.Handle:=fileinfo.hIcon;////返回文件图标<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listfile.ImageList2.AddIcon(listfile.image1.Picture.Icon);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;wfile.Items[wfile.Items.Count-1].ImageIndex:=imagelist2.Count -1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; findclose(searchrec);<br>end;<br>//////////////以上是查找文件的过程<br><br>procedure SelectNode(const Tx: string; Tree: TTreeView);<br>var www:Boolean;<br>begin<br>&nbsp; if Tx = '' then exit;<br>&nbsp; node3:=tree.Items.GetFirstNode;<br>&nbsp; while node3&lt;&gt;nil do<br>&nbsp; begin<br>&nbsp; &nbsp; if pathname+'/'+node3.Text = tx then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //node.Expanded:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; //listfile.filedir(pathname+'/'+node1.Text,node1);<br>&nbsp; &nbsp; &nbsp; &nbsp; node3.Expanded:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; www:=true;<br>&nbsp; &nbsp; &nbsp; &nbsp; break;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; node3:=node3.GetNext;<br>&nbsp; &nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; showmessage(tx);<br>&nbsp; &nbsp; &nbsp; &nbsp; shellexecute(listfile.handle,nil,pchar(tx ),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,nil,sw_shownormal)<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; if www=false then<br>&nbsp; shellexecute(listfile.handle,nil,pchar(tx ),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,nil,sw_shownormal)<br>end;<br>////////////以上是一个过程,处理鼠标在listviewh 双击事件过程<br>/////在树中查找 <br><br>procedure Tlistfile.FormCreate(Sender: TObject);<br>var i:integer;drivepath:string;<br>&nbsp; &nbsp; fname:string;<br>&nbsp; &nbsp; node:ttreenode;<br>&nbsp; &nbsp; rnode:ttreenode;<br>begin<br>&nbsp; shgetfileinfo(pchar('c:/'),0,fileinfo,sizeof(fileinfo),shgfi_icon);<br>&nbsp; listfile.image1.Picture.Icon.Handle:=fileinfo.hIcon;<br>&nbsp; listfile.ImageList1.AddIcon(listfile.image1.Picture.Icon);<br>&nbsp; listfile.ImageList2.AddIcon(listfile.image1.Picture.Icon);<br>&nbsp; shgetfileinfo(pchar('c:/Program Files'),0,fileinfo,sizeof(fileinfo),shgfi_icon);<br>&nbsp; listfile.image1.Picture.Icon.Handle:=fileinfo.hIcon;<br>&nbsp; listfile.ImageList1.AddIcon(listfile.image1.Picture.Icon);<br>&nbsp; shgetfileinfo(pchar('c:/Program Files'),0,fileinfo,sizeof(fileinfo),shgfi_icon);<br>&nbsp; listfile.image1.Picture.Icon.Handle:=fileinfo.hIcon;<br>&nbsp; listfile.ImageList1.AddIcon(listfile.image1.Picture.Icon);<br>///////////////////////////////////////////////<br>////因为有两个imagelist控件一个是树的,一个是listfile的,imagelist的图标库是空的。<br>////以上是为两个imagelist控件添加图标。以上的代码可以不要,但为了界面的美观,可以手工的为<br>////两个imagelist控件添加图标.<br>////////////////////以上添加的图标一个是硬盘的图标,一个是文件夹的图标.<br>&nbsp; dragacceptfiles(handle,true);<br>&nbsp; application.Title:='资源管理器';<br>&nbsp; for i:=0 to 25 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp;drivepath:=char(ord('A')+i)+':/';<br>&nbsp; &nbsp; &nbsp;case getdrivetype(pchar(drivepath))of<br>&nbsp; &nbsp; &nbsp; &nbsp;drive_removable:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//dir.Items.AddChild(nil,char(ord('A')+i)+':');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;;<br>&nbsp; &nbsp; &nbsp; &nbsp;drive_fixed:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node:=dir.Items.AddChild(nil,char(ord('A')+i)+':');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rnode:=dir.Items.AddChild(node,'');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rnode.ImageIndex:=-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fname:=char(ord('A')+i)+':';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,0]:=fname;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem:=wfile.Items.Add;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.Caption:=f[0,0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.SubItems.Add(f[0,1]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;drive_remote:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node:=dir.Items.AddChild(nil,char(ord('A')+i)+':');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rnode:=dir.Items.AddChild(node,'');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rnode.ImageIndex:=-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fname:=char(ord('A')+i)+':';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,0]:=fname;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem:=wfile.Items.Add;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.Caption:=f[0,0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.SubItems.Add(f[0,1]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;drive_cdrom:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node:=dir.Items.AddChild(nil,char(ord('A')+i)+':');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rnode:=dir.Items.AddChild(node,'');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rnode.ImageIndex:=-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fname:=char(ord('A')+i)+':';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,0]:=fname;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem:=wfile.Items.Add;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.Caption:=f[0,0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.SubItems.Add(f[0,1]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp;drive_ramdisk:<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node:=dir.Items.AddChild(nil,char(ord('A')+i)+':');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rnode:=dir.Items.AddChild(node,'');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rnode.ImageIndex:=-1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fname:=char(ord('A')+i)+':';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f[0,0]:=fname;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem:=wfile.Items.Add;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.Caption:=f[0,0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listitem.SubItems.Add(f[0,1]);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;end;<br>////////////以上的循环返回机器上的所有硬盘<br>&nbsp; &nbsp; end;<br>end;<br><br>/////////////以上是窗体加载时的事件<br><br>procedure Tlistfile.dirCollapsed(Sender: TObject; Node: TTreeNode);<br>var I:integer;<br>begin<br>&nbsp; for i:=node.Count -1 downto 1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; node.Item .Delete ;<br>&nbsp; &nbsp; end;<br>end;<br>////////当treeview收缩时的事件<br><br>procedure Tlistfile.dirExpanded(Sender: TObject; Node: TTreeNode);<br>begin<br>&nbsp; &nbsp;panel(node);<br>&nbsp; names:=filename;<br>&nbsp; if filename&lt;&gt;'' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; filedir(filename+node.Text &nbsp;,node);<br>&nbsp; &nbsp; &nbsp; pathname:=filename+node.Text;<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; filedir(node.Text ,node);<br>&nbsp; &nbsp; &nbsp; pathname:=node.Text;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp;filename:='';<br>end;<br>///////treeview展开时的事件<br>/////以上的填充有两种,第一种就是我这种,第二种是在窗体加载时填充。<br>////第一种节省速度。<br>///////因为是网络传输,第一要考虑的就是速度,所以,我考虑第一种。<br>/////在节点展开是填充,收缩时删除节点,但只保留了一个。<br><br><br>procedure Tlistfile.WfileDblClick(Sender: TObject);<br>var i:integer;<br>begin<br>&nbsp; try<br>&nbsp; if wfile.ItemIndex=0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; node3.Collapse(true);<br>&nbsp; &nbsp; //node3.Expanded:=true;<br>&nbsp; &nbsp; node3.Parent.Selected:=true;<br>&nbsp; &nbsp; dir.OnClick(node3.Parent );<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; SelectNode(pathname+'/'+wfile.Selected.Caption,dir);<br>&nbsp; except<br>&nbsp; end;<br>&nbsp;// &nbsp; shellexecute(handle,nil,pchar(pathname+'/'+wfile.Selected.Caption ),<br>&nbsp; &nbsp;// &nbsp; nil,nil,sw_shownormal)<br><br>end;<br>/////////鼠标在listview上双击时<br><br>procedure Tlistfile.dirClick(Sender: TObject);<br>begin<br>&nbsp; zt.Panels[0].Text:='';<br>&nbsp; panel(dir.Selected);<br>&nbsp; names:=filename;<br>&nbsp; if filename&lt;&gt;'' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; filedir(filename+dir.Selected.Text,dir.Selected);<br>&nbsp; &nbsp; &nbsp; pathname:=filename+dir.Selected.Text;<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; filedir(dir.Selected.Text ,dir.Selected);<br>&nbsp; &nbsp; &nbsp; pathname:=dir.Selected.Text;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp;filename:='';<br>end;<br>/////在treeview上单击时<br><br>procedure Tlistfile.N1Click(Sender: TObject);<br>begin<br>&nbsp; if deletefile(pathname+'/'+wfile.Selected.Caption) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; filedir(pathname,dir.Selected);<br>&nbsp; &nbsp; &nbsp; zt.Panels[0].Text:='删除成功';<br>&nbsp; &nbsp; end<br>&nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; zt.Panels[0].Text:='删除不成功';<br>&nbsp; &nbsp; end;<br>end;////处理在listview上删除一个对象是删除个文件<br><br>procedure Tlistfile.dirEdited(Sender: TObject; Node: TTreeNode;<br>&nbsp; var S: String);<br>begin<br>&nbsp; if RenameFile(pathname, names+s) then<br>&nbsp; &nbsp; zt.Panels[0].Text:='更名成功'<br>&nbsp; else<br>&nbsp; &nbsp; zt.Panels[0].Text:='更名不成功';<br>end;<br>//////在treeview上改名是更改对应的目录名<br><br>procedure Tlistfile.WfileEdited(Sender: TObject; Item: TListItem;<br>&nbsp; var S: String);<br>begin<br>&nbsp;if &nbsp;movefile(pchar(pathname+'/'+wfile.Selected.Caption),pchar(pathname+'/'+s)) then<br>&nbsp; &nbsp;zt.Panels[0].Text:='更名成功'<br>&nbsp;else<br>&nbsp; &nbsp;zt.Panels[0].Text:='更名不成功';<br>end;///////更改文件的文件名的事件<br><br>procedure Tlistfile.dirKeyUp(Sender: TObject; var Key: Word;<br>&nbsp; Shift: TShiftState);<br>Var<br>&nbsp; T:TSHFileOpStruct;<br>&nbsp; P:String;<br>begin<br>&nbsp; if key=46 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; With T do<br>&nbsp; &nbsp; &nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Wnd:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; wFunc:=FO_DELETE;<br>&nbsp; &nbsp; &nbsp; &nbsp; pFrom:=Pchar(pathname);<br>&nbsp; &nbsp; &nbsp; &nbsp; pTo:=nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; fFlags:=FOF_ALLOWUNDO+FOF_NOCONFIRMATION+FOF_NOERRORUI;//标志表明允许恢复,无须确认并不显示出错信息<br>&nbsp; &nbsp; &nbsp; &nbsp; hNameMappings:=nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; lpszProgressTitle:='正在删除文件夹...';<br>&nbsp; &nbsp; &nbsp; &nbsp; fAnyOperationsAborted:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; End;<br>&nbsp; &nbsp; &nbsp; &nbsp; if SHFileOperation(T)=0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zt.Panels[0].Text:='删除完毕';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dir.Selected.Delete;<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zt.Panels[0].Text:='删除不成功';<br>&nbsp; &nbsp; end;<br>end;//////删除节点时同时删除对应的目录,用户按了键盘上的[Delete]键.<br><br><br>procedure Tlistfile.D1Click(Sender: TObject);<br>var key: word;Shift: TShiftState;<br>begin<br>&nbsp; key:=46;<br>&nbsp; dir.OnKeyUp(sender,key,shift);<br>end;////点击tpopupmemu1的删除时。<br><br>procedure Tlistfile.R2Click(Sender: TObject);<br>begin<br>&nbsp; dir.Selected.EditText;<br>end;////////点击重命名时。<br><br>procedure Tlistfile.N3Click(Sender: TObject);<br>var node:ttreenode;<br>&nbsp;s:string;i:integer;<br>begin<br>&nbsp; i:=pos(dir.Selected.Text,pathname);<br>&nbsp; s:=pathname;<br>&nbsp; delete(s,i,length(dir.Selected.Text ));<br>&nbsp; //showmessage(s+'新建文件夹');<br>&nbsp; mkdir(s+'新建文件夹');<br>&nbsp; node:=dir.Items.AddChild(dir.Selected.Parent,'新建文件夹');<br>&nbsp; node.ImageIndex:=1;<br>&nbsp; node.SelectedIndex:=1;<br>&nbsp; node.EditText;<br>end;/////////点击新建文件夹时。<br><br>procedure Tlistfile.M1Click(Sender: TObject);<br>begin<br>&nbsp; mkdir(pathname+'/'+'新建文件夹');<br>&nbsp; listitem:=wfile.Items.Add;<br>&nbsp; listitem.Caption:='新建文件夹';<br>&nbsp; listitem.ImageIndex:=0;<br>end;///////在wfile上新建文件夹时.<br><br>procedure Tlistfile.N2Click(Sender: TObject);<br>var &nbsp; f:textfile;<br>begin<br>&nbsp; assignfile(f,pathname+'/'+'新建文件.Wxx');<br>&nbsp; rewrite(f);<br>&nbsp; closefile(f);<br>&nbsp; listitem:=wfile.Items.Add;<br>&nbsp; listitem.Caption:='新建文件.Wxx';<br>&nbsp; listitem.EditCaption;<br>end;////新建文件时。<br><br>procedure Tlistfile.N4Click(Sender: TObject);<br>begin<br>&nbsp; wfile.ViewStyle:=vsIcon;<br>end;/////////大图标<br><br>procedure Tlistfile.L1Click(Sender: TObject);<br>begin<br>&nbsp; wfile.ViewStyle:=vslist;<br>end;////////////列表<br><br>procedure Tlistfile.M2Click(Sender: TObject);<br>begin<br>&nbsp; wfile.ViewStyle:=vsSmallIcon;<br>end;///////////小图标<br><br>procedure Tlistfile.N7Click(Sender: TObject);<br>begin<br>&nbsp; wfile.ViewStyle:=vsReport;<br>end;///////////详细资料。<br><br>end.
 
to yanghai0437, <br> 看看我的开头的帖子的代码,那是我已经整理好了的!你老兄是不是不试代码呀?<br>上面不是已经说的很明白了吗?在98下运行正常,但在NT下不保证!<br>要在win2000下运行,需要做如下改动:<br>&nbsp;ShGetFileInfo('*.*',0,SHFileInfo...)<br>改成<br>ShGetFileInfo('',0,SHFileInfo...)<br><br><br>
 
to 天宇天蓝, <br>&nbsp; 你要是早来一步,我可就省了老大的事了!现在我除了存取图标的问题外别的早就搞定了!<br>但还是谢谢你!看了你的代码后仍有很大的收获!<br> 这个问题你会吗?能否告诉我:<br> 问:怎样将取得的图标(我是用ExtractAssociatedIcon从盘上的一个文件提取的)存入一个数据库,如aaa.mdb?<br>  而后又怎样从库中取出来然后将它加到一个imagelist里面?<br>  其实不用数据库也行,只要能把很多的图标存入一个文件就行。<br><br> 
 
to ligia:<br>&nbsp; 不好意思,还是没解决问题。看了你给的函数后,发现不太适合自己的要求。<br>我的意思是这样的:在一个listview内有些items,但是它们的图标要根据情况随时变化,<br>有的时候需要从系统imagelist内取相应的图标,但有的时候需要从自已预定的一个imagelist<br>内取(里面早已预置了很多的图标)。我的问题是怎么样可以使listview的Largeimagelist<br>和smallimagelist在系统的imsgelist和自已的imamelist两边方便的转换?<br> 盼指点一二,谢谢!
 
http://www.iligia.com/chinese/index.asp &nbsp;你可以下载 xpanels xp<br>我是这样作的.<br>1.首先用FkListViewImageInit对ListView初使话.<br>2.需要使用系统图标的时候使用下边的函数添加到 ListView.SmallImages.<br>FUNCTION FtImageListAdd(vFile:STRING;vImageList:TImageList;vForm:TForm):INTEGER;<br>VAR icoTemp:TIcon; imgList:TImageList; tshIcon: TSHFileInfo;<br>BEGIN<br>&nbsp; &nbsp; icoTemp:=TIcon.Create;<br>&nbsp; &nbsp; imgList:=TImageList.Create(vForm);//<br>&nbsp; &nbsp; imgList.Handle:=SHGetFileInfo(PCHAR(vFile),0,tshIcon,SizeOf(tshIcon),SHGFI_SYSICONINDEX+SHGFI_SMALLICON);<br>&nbsp; &nbsp; imgList.ShareImages:=TRUE;<br>&nbsp; &nbsp; imgList.GetIcon(tshIcon.iIcon,icoTemp);<br>&nbsp; &nbsp; Result:=vImageList.AddIcon(icoTemp);<br>&nbsp; &nbsp; icoTemp.Free;<br>&nbsp; &nbsp; imgList.Free;<br>END;<br><br>3.使用自己的图标的时候,如 ImageListDefault .<br>VAR icoTemp:TIcon; iIndex:INTEGER;<br>BEGIN<br>&nbsp; &nbsp; icoTemp:=TIcon.Create;<br>&nbsp; &nbsp; ImageListDefault.GetIcon(使用的那个图标的Index,icoTemp);<br>&nbsp; &nbsp; iIndex:=ListViewMain.SmallImages.AddIcon(icoTemp);<br>&nbsp; &nbsp; icoTemp.Free;<br>END;<br>iIndex就是新的Index.<br><br><br><br><br>
 
to ligia, <br>&nbsp; 你的第一个函数里面的这个 vImageList 是做什么用的?哪儿来的?我用了后还是不行。<br>看来似乎是listview的问题。<br> 比如:在一个listview内有两个items,分别为aaa.doc和direction。<br> aaa.doc的图标从系统内取得,应为一个word图标,但direction的图标是一个自定义的图标,<br>从自已的imagelist里面取第一个。<br> 这根本是不可能的!<br> 程序运行后,要么都是系统内的图标,要么都是自己的imagelist里面的!也就是说,同一时刻,<br>listview.smallimages只能付给一个imagelist!<br> 这个问题可是困死我了!
 
你等一下,我给你造个小程序.
 
到下边下载,包含测试程序+源码.Delphi7<br>&nbsp;www.iligia.com/public/download/testicon/ligia_icon_test.rar<br>
 
to ligia:<br>&nbsp; 非常感谢您不怕麻烦的解答我的问题。我试了你的代码,基本上可以,看来我前面的看法是错误的。<br>但仍有一点小问题:我作了如下改动后:<br>procedure TFormMain.OpenFileClick(Sender: TObject);<br>var sFile:STRING; &nbsp; lItem:TListItem;icoTemp:TIcon; imgList:TImageList; tshIcon: TSHFileInfo;<br>&nbsp;<br>&nbsp;// IF NOT FormMain.OpenDialogMain.Execute THEN Exit;<br>&nbsp; // sFile:=FormMain.OpenDialogMain.FileName;<br>&nbsp; //将上面原来的两句改成以面这一句<br>&nbsp; &nbsp; &nbsp;sFile:='aaa.doc';//aaa.doc并不是一个实际存在的文件<br>&nbsp; lItem:=FormMain.ListViewMain.Items.Add;<br>&nbsp; lItem.Caption:=sFile;<br>&nbsp; &nbsp; icoTemp:=TIcon.Create;<br>&nbsp; imgList:=TImageList.Create(FormMain);//<br>//imgList.Handle:=SHGetFileInfo(PCHAR(sFile),0,tshIcon,SizeOf(tshIcon),SHGFI_SYSICONINDEX+SHGFI_SMALLICON); &nbsp; <br>//将上面一句改为下面这句:(这样才能获得并不实际存在的文件的图标)<br>imgList.Handle:=SHGetFileInfo(PCHAR(sFile),0,tshIcon,SizeOf(tshIcon),SHGFI_SYSICONINDEX or SHGFI_TYPENAME or SHGFI_USEFILEATTRIBUTES or SHGFI_SMALLICON);<br>&nbsp; <br> imgList.ShareImages:=TRUE;<br>&nbsp; imgList.GetIcon(tshIcon.iIcon,icoTemp);<br>&nbsp; FormMain.ListViewMain.SmallImages.AddIcon(icoTemp);<br>&nbsp; lItem.ImageIndex:=FormMain.ListViewMain.SmallImages.Count-1;<br>&nbsp; icoTemp.Free;<br>&nbsp; imgList.Free;<br>end;<br>作了如上改动后,运行。出现了下面的情况:<br> 1、若先单击OpenFile按钮,再击myicon按钮,没有任何问题。listview内会出现一个work图标后,<br> 又出现了一个自己的ImageListDefault内的图标。<br> 2、但如果先击myicon按钮,再击OpenFile按钮,则出现了两个跟myicon一样的图标,(第二个应该是word图标)。<br> 3、如果再加入一个button2,将openfile按钮内的代码加到里面(只把aaa.doc改为aaa.txt),运行<br> 先击OpenFile按钮,再击button2,这时只出现两个word的图标,而不会出现txt图标。<br> <br> 这说明您给出的代码似乎还有不妥之处,昐望您好人做到底,给予解答!<br><br>
 
我按照你说修改了,没有出现你说的问题.可能与操作系统有关系.我的是XPpro+SP1.<br>修改以下内容.这一段去掉的内容不是必须的,只是为了保证图标色彩正常.<br><br>FUNCTION FkListViewImageInit(vListView:TListView):BOOL;<br>VAR &nbsp;SysImageList: UINT; &nbsp;SFI: TSHFileInfo;<br>BEGIN<br>&nbsp; vListView.LargeImages:=TImageList.Create(vListView);<br>&nbsp; vListView.SmallImages:=TImageList.Create(vListView);<br>&nbsp; //vListView.PopupMenu:=TPopupMenu.Create(vListView);<br>&nbsp; &nbsp;//去掉开始&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;<br>&nbsp;{ WITH vListView DO<br>&nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; //[BIG]<br>&nbsp; &nbsp; SysImageList := SHGetFileInfo('',0,SFI,SizeOf(SFI),SHGFI_SYSICONINDEX OR SHGFI_LARGEICON);<br>&nbsp; &nbsp; IF SysImageList &lt;&gt; 0 THEN<br>&nbsp; &nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; LargeImages.Handle := SysImageList;<br>&nbsp; &nbsp; &nbsp; Largeimages.ShareImages := TRUE;<br>&nbsp; &nbsp; &nbsp; END;<br>&nbsp; &nbsp; //[SMALL]<br>&nbsp; &nbsp; SysImageList := SHGetFileInfo('',0,SFI,SizeOf(SFI),SHGFI_SYSICONINDEX OR SHGFI_SMALLICON);<br>&nbsp; &nbsp; IF SysImageList &lt;&gt; 0 THEN<br>&nbsp; &nbsp; &nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp; SmallImages.Handle := SysImageList;<br>&nbsp; &nbsp; &nbsp; SmallImages.ShareImages := TRUE;<br>&nbsp; &nbsp; &nbsp; END;<br>&nbsp; &nbsp; //[END OF WITH]<br>&nbsp; &nbsp; END;}<br>&nbsp; &nbsp;//去掉结束&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;<br>END;<br>//-----------------------------------------------------------------------------------<br>2.因为我的测试没有出现你说的错误,我也只能给你推断一下可能出现的原因.<br>在<br> imgList.ShareImages:=TRUE;<br>&nbsp; ShowMessage(IntToStr(tshIcon.iIcon));//加入测试,如果数值&lt;=0则,在系统中没有找到索引值.<br>&nbsp; imgList.GetIcon(tshIcon.iIcon,icoTemp);<br>&nbsp; ShowMessage(IntToStr(icoTemp.handle));//同样,加入测试,如果数值&lt;=0则获得图标失败.<br><br>&nbsp; <br>SHGetFileInfo情况这个函数不是每次都执行成功的.<br>
 
文档读注册表,程序从文件中提取
 
<br>[HKEY_CLASSES_ROOT/.doc]<br>@="Word.Document.8"<br>"Content Type"="application/msword"<br><br>[HKEY_CLASSES_ROOT/.doc/PersistentHandler]<br>@="{98de59a0-d175-11cd-a7bd-00006b827d94}"<br><br>[HKEY_CLASSES_ROOT/.doc/ShellEx]<br><br>[HKEY_CLASSES_ROOT/.doc/ShellEx/{BB2E617C-0920-11d1-9A0B-00C04FC2D6C1}]<br>@="{9DBD2C50-62AD-11d0-B806-00C04FD706EC}"<br><br>[HKEY_CLASSES_ROOT/.doc/ShellNew]<br><br>[HKEY_CLASSES_ROOT/.doc/Word.Document.6]<br><br>[HKEY_CLASSES_ROOT/.doc/Word.Document.6/ShellNew]<br>"FileName"="winword.doc"<br><br>[HKEY_CLASSES_ROOT/.doc/Word.Document.8]<br><br>[HKEY_CLASSES_ROOT/.doc/Word.Document.8/ShellNew]<br>"FileName"="winword8.doc"<br><br>[HKEY_CLASSES_ROOT/.doc/WordDocument]<br><br>[HKEY_CLASSES_ROOT/.doc/WordDocument/ShellNew]<br>"FileName"="winword2.doc"<br><br>[HKEY_CLASSES_ROOT/.doc/WordPad.Document.1]<br><br>[HKEY_CLASSES_ROOT/.doc/WordPad.Document.1/ShellNew]<br>"NullFile"=""<br><br>
 
to ligia, <br>&nbsp; 非常感谢你!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!<br> 我已经彻底解决这个问题了!受到你的启发,我改了一种思路。我是这样做的:<br> 1、在设计期间就给listview.smallimages加一个空的imagelist1,从面省去了FkListViewImageInit初始化。<br> 2、在程序中根据情况不同而随时往imagelist1里面加图标。比如要从系统中取图标时,先用SHGetFileInfo取出,<br>  后将它加到imagelist1里面,并得到它的索引,后付给item。<br> 3、用自己的图标时,也是先把它加入到imagelist1里面,而后再从里面取出。<br> 这是我在开会时想出的办法(哈哈,看来有些会还是有一定作用的!)。这么做来,使程序显得更简练一些,<br> 而且绝不出问题。<br> <br> 好了,应该散分了。给你150分(别嫌少),剩下的给别的热心的朋友。<br> 再一次谢谢大家!<br><br>
 
后退
顶部