如何获取文件的系统图标
比如txt就显示文档图标,exe显示程序图标,未知显示未知。
[解决办法]
新建工程、双击窗体后,用下列代码覆盖unit1的内容:
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,ShellAPI, ExtCtrls, ComCtrls;type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } procedure Button1Click(Sender: TObject); public { Public declarations } end;var Form1: TForm1;implementationvar Button1: TButton; Image1: TImage;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);var FileInfo : TSHFileInfo; OpenDialog1: TOpenDialog; hc: TIcon; Bitmap: TBitmap; Mem: TMemoryStream;begin Mem:=Tmemorystream.Create; try OpenDialog1:=TOpenDialog.Create(self); try if OpenDialog1.Execute then begin FileInfo.iIcon := 0; SHGetFileInfo(pchar(OpenDialog1.filename), 0, FileInfo, SizeOf(TSHFileInfo), SHGFI_ICON or SHGFI_LARGEICON); Image1.Picture.Icon.Handle := FileInfo.hIcon; hc := TIcon.Create; try Bitmap := TBitmap.Create; try hc.Handle := FileInfo.hIcon; Bitmap.width := hc.width; Bitmap.height := hc.height; Bitmap.Canvas.Draw(0, 0, hc); Bitmap.SaveToStream(Mem); Mem.SaveToFile(ExtractFilePath(paramstr(0)) + 'qinxh.bmp'); Image1.Picture.LoadfromFile(ExtractFilePath(paramstr(0)) + 'qinxh.bmp'); finally Bitmap.Free; end; finally hc.Free; end; end; finally OpenDialog1.Free; end; finally Mem.Free; end;end;procedure TForm1.FormCreate(Sender: TObject);begin Button1:=TButton.Create(self); with Button1 do begin Parent:=Form1; Caption:='取图标'; Left:=88; Button1.Top:=96; OnClick:=Button1Click; end; Image1:=TImage.Create(self); with Image1 do begin Parent:=Form1; Left:=110; Top:=32; end;end;end.