//以下提到的3个函数合理利用,一定能够达到你的目的。 unit GraphicsExpand;interface uses Forms , Registry , Graphics , ShellAPI , Controls , SysUtils , Windows , Dialogs;//得到打开这个文件的Exe文件名 //如GetExeByOpenFile('xxx.DOC') = 'C:\WinWord\WinWord.Exe' Function GetExeByOpenFile(FileName:String):String;//得到设置的文件的图标号 Function GetExeIcoByOpenFile(FileName:String):integer;//把一个文件对应的图标加到ImageList中 Function AddFileIcoToImgLst(FileName : String ; ImageList : TImageList) : integer;implementationFunction AddFileIcoToImgLst(FileName : String ; ImageList : TImageList) : integer; var Count , IcoIndex : integer; Ico : Ticon; ExFile : String; begin Result := -1; ExFile := GetExeByOpenFile(FileName); IcoIndex := GetExeIcoByOpenFile(FileName); Count := ExtractIcon( Application.Handle, PChar(ExFile),$FFFFFFFF); Ico := Ticon.Create; if( IcoIndex < Count ) then begin Ico.Handle := ExtractIcon( Application.Handle, PChar(ExFile), IcoIndex ); ImageList.AddIcon(Ico); Result := ImageList.Count -1; end; Ico.Free; end;Function GetStrByOpenFile(FileName:String):String; var Reg : Tregistry; FEName , RegNe: String; begin Result := '%SystemRoot%\System32\shell32.dll,0'; FeName := ansiuppercase(Trim(extractfileext(FileName))); if FEName='.EXE' then Result := Trim(FileName) else if copy(Trim(FileName),Length(Trim(FileName)),1)='\' then Result := '%SystemRoot%\System32\shell32.dll,3' else if FEName='' then Result := '%SystemRoot%\System32\shell32.dll,0' else begin if copy(FEName,1,1)<>'.' then FEName := '.'+FEName; Reg := TRegistry.Create; Reg.RootKey := HKEY_CLASSES_ROOT; if Reg.OpenKey(FEName,True) then begin RegNE := Reg.ReadString('')+'\\DefaultIcon'; Reg.CloseKey; if RegNe <> '\\DefaultIcon' then begin Reg.OpenKey(RegNE,True); Result := Reg.ReadString(''); Reg.CloseKey; end; end; Reg.Free; end; end;Function GetExeIcoByOpenFile(FileName:String):integer; var Tmp , cStr : String; begin Result := 0; Tmp := ''; cStr := GetStrByOpenFile(FileName); if cStr<>'' then while True do begin if pos(copy(cStr,Length(cStr),1),'-0123456789')>0 then begin Tmp := copy(cStr,Length(cStr),1) + Tmp; cStr := copy(cStr,1,Length(cStr)-1) end else begin if Tmp<>'' then Result := strtoint(Tmp); Break; end; end; end;Function GetExeByOpenFile(FileName:String):String; var cStr : String; begin cStr := GetStrByOpenFile(FileName); Result := cStr; if (cStr<>'') and (Pos(',',cStr)>0) then while True do begin if copy(cStr,Length(cStr),1)<>',' then cStr := copy(cStr,1,Length(cStr)-1) else begin Result := copy(cStr,1,Length(cStr)-1); Break; end; end; end; end.
FileName是带有路径的。这样就自动用相关程序打开文件了。
unit GraphicsExpand;interface
uses
Forms , Registry , Graphics , ShellAPI , Controls , SysUtils , Windows , Dialogs;//得到打开这个文件的Exe文件名
//如GetExeByOpenFile('xxx.DOC') = 'C:\WinWord\WinWord.Exe'
Function GetExeByOpenFile(FileName:String):String;//得到设置的文件的图标号
Function GetExeIcoByOpenFile(FileName:String):integer;//把一个文件对应的图标加到ImageList中
Function AddFileIcoToImgLst(FileName : String ; ImageList : TImageList) : integer;implementationFunction AddFileIcoToImgLst(FileName : String ; ImageList : TImageList) : integer;
var
Count , IcoIndex : integer;
Ico : Ticon;
ExFile : String;
begin
Result := -1;
ExFile := GetExeByOpenFile(FileName);
IcoIndex := GetExeIcoByOpenFile(FileName);
Count := ExtractIcon( Application.Handle, PChar(ExFile),$FFFFFFFF);
Ico := Ticon.Create;
if( IcoIndex < Count ) then begin
Ico.Handle := ExtractIcon( Application.Handle, PChar(ExFile), IcoIndex );
ImageList.AddIcon(Ico);
Result := ImageList.Count -1;
end;
Ico.Free;
end;Function GetStrByOpenFile(FileName:String):String;
var
Reg : Tregistry;
FEName , RegNe: String;
begin
Result := '%SystemRoot%\System32\shell32.dll,0';
FeName := ansiuppercase(Trim(extractfileext(FileName)));
if FEName='.EXE' then Result := Trim(FileName)
else if copy(Trim(FileName),Length(Trim(FileName)),1)='\' then Result := '%SystemRoot%\System32\shell32.dll,3'
else if FEName='' then Result := '%SystemRoot%\System32\shell32.dll,0'
else begin
if copy(FEName,1,1)<>'.' then FEName := '.'+FEName;
Reg := TRegistry.Create;
Reg.RootKey := HKEY_CLASSES_ROOT;
if Reg.OpenKey(FEName,True) then begin
RegNE := Reg.ReadString('')+'\\DefaultIcon';
Reg.CloseKey;
if RegNe <> '\\DefaultIcon' then begin
Reg.OpenKey(RegNE,True);
Result := Reg.ReadString('');
Reg.CloseKey;
end;
end;
Reg.Free;
end;
end;Function GetExeIcoByOpenFile(FileName:String):integer;
var
Tmp , cStr : String;
begin
Result := 0;
Tmp := '';
cStr := GetStrByOpenFile(FileName);
if cStr<>'' then
while True do begin
if pos(copy(cStr,Length(cStr),1),'-0123456789')>0 then
begin
Tmp := copy(cStr,Length(cStr),1) + Tmp;
cStr := copy(cStr,1,Length(cStr)-1)
end
else begin
if Tmp<>'' then Result := strtoint(Tmp);
Break;
end;
end;
end;Function GetExeByOpenFile(FileName:String):String;
var
cStr : String;
begin
cStr := GetStrByOpenFile(FileName);
Result := cStr;
if (cStr<>'') and (Pos(',',cStr)>0) then
while True do begin
if copy(cStr,Length(cStr),1)<>',' then
cStr := copy(cStr,1,Length(cStr)-1)
else begin
Result := copy(cStr,1,Length(cStr)-1);
Break;
end;
end;
end;
end.