{-------------------------------------------------------------------------------
@過程名: slpert -> TFm_main.SetAssociatedExec
@作者: Gavin
@日期: 2004.09.08
@功能描述:
@參數: FileExt, Filetype, FileDescription, MIMEType, ExecName: string
@返回值: Boolean
-------------------------------------------------------------------------------}
Function TFm_main.SetAssociatedExec(FileExt, Filetype, FileDescription,
MIMEType, ExecName: String): Boolean; {修改成功,返回True,否則False}
Var
Reg: TRegistry;
ShFileInfo: TSHFILEINFO;
IconIndex: integer;
Begin
Result := False; {}
// ShGetFileInfo(Pchar(ExecName), 0, SHFileInfo,SizeOf(SHFileInfo), SHGFI_LARGEICON or SHGFI_SYSICONINDEX or SHGFI_TYPENAME or SHGFI_SMALLICON);
//IconIndex:=SHFileInfo.iIcon;
//showmessage(inttostr(iconIndex));
If (FileExt = '') Or (ExecName = '') Then
Exit; {如果文件類型為空或者沒有定義執行程序就退出,FileExt必須帶″.″,如.BMP}
Reg := TRegistry.Create;
Try
Reg.RootKey := HKey_Classes_Root;
If Not Reg.OpenKey(FileExt, True) Then
Exit; {當不能正確找到或創建FileExt鍵時退出,這種情況一般是注冊表有錯誤,以下同}
Reg.WriteString('', FileType);
If MIMEType <> '' Then
Begin
Reg.WriteString('Content Type', MIMEType);
End;
Reg.CloseKey;
If Not Reg.OpenKey(FileType, True) Then
Exit;
Reg.WriteString('', FileDescription);
If Not Reg.OpenKey('shell\open\command', True) Then
Exit;
Reg.WriteString('', ExecName + ' "%1"');
{執行程序一般都有參數,例如WinZip的“winzip32.exe ″%1″”,″%1″參數指ZIP文件的文件名。因此ExecName應視情況加入參數}
Reg.CloseKey;
If Not Reg.OpenKey(FileType + '\DefaultIcon', True) Then
Exit;
Reg.WriteString('',ExecName+' ,1'); ///ExtractFilePath(
Reg.CloseKey;
Result := true;
Finally
Reg.Free;
End;
End;