procedure CreateShortcut(FileName :string; Description :string;
arguements :string; Location :ShortcutType);
var
cObj :IUnknown;
sLink :IShellLink;
pFile :IPersistFile;
sDir :string;
lName :string;
spath :string;
wFileName :WideString;
mReg :TRegistry;
key :string;
tmp :string;
begin
cObj :=CreateComObject(CLSID_ShellLink); //創建COM對象
sLink :=cObj as IShellLink; //COM對象轉化為IShellLink型接口
pFile :=cObj as IPersistFile; //COM對象轉化為IPersistFile型接口
//獲取路徑
sPath :=ExtractFilePath(FileName);
with sLink do begin
SetPath(PChar(FileName)); //設置執行文件名
SetArguments(PChar(arguements)); //設置執行參數
SetDescription(Pchar(Description)); //設置描述信息
SetWorkingDirectory(PChar(sPath)); //設置工作路徑,即執行程序所在目錄
end;
//獲取各快捷方式的實際目錄
mReg :=TRegistry.Create;
with mReg do begin
RootKey :=HKEY_CURRENT_USER;
key :=REGSTR_PATH_EXPLORER; //Delphi在單元RegStr中定義的常量
tmp :=key + 'Shell Folders';
OpenKey(tmp, false);
case Location of
ST_DESKTOP: sDir :=ReadString('Desktop');
ST_SENDTO: sDir :=ReadString('SendTo');
ST_STARTMENU: sDir :=ReadString('Start Menu');
ST_QUICKLAUNCH:
begin
sDir :=ReadString('AppData');
sDir :=sDir + 'MicrosoftInternet ExplorerQuick Launch';
end;
end;
//生成快捷方式文件名
lName :=ChangeFileExt(FileName, '.Lnk');
lName :=ExtractFileName(lName);
if sDir<>'' then
begin
//生成快捷方式全路徑名
wFileName :=sDir + '' + lName;
//保存生成的快捷方式文件
pFile.Save(PWChar(wFileName), false);
end;
Free;
end;
end;
上面聲明了一個過程
下面應用它
var
fName :string;
fDesc :string;
fArgu :string;
begin
fName :=Application.ExeName;
fDesc :='Delphi 7.0 創建的快捷方式--桌面';
fArgu :='無參數';
CreateShortcut(fName,fDesc,fArgu, ST_DESKTOP);
end;
procedure TForm1.SpeedButton2Click(Sender: TObject);
var
fName :string;
fDesc :string;
fArgu :string;
begin
fName :=Application.ExeName;
fDesc :='Delphi 7.0 創建的快捷方式--發送到。。。';
fArgu :='無參數';
CreateShortcut(fName,fDesc,fArgu, ST_SENDTO);
end;
procedure TForm1.SpeedButton3Click(Sender: TObject);
var
fName :string;
fDesc :string;
fArgu :string;
begin
fName :=Application.ExeName;
fDesc :='Delphi 7.0 創建的快捷方式--開始菜單';
fArgu :='無參數';
CreateShortcut(fName,fDesc,fArgu, ST_STARTMENU);
end;
procedure TForm1.SpeedButton4Click(Sender: TObject);
var
fName :string;
fDesc :string;
fArgu :string;
begin
fName :=Application.ExeName;
fDesc :='Delphi 7.0 創建的快捷方式--快速啟動';
fArgu :='無參數';
CreateShortcut(fName,fDesc,fArgu, ST_QUICKLAUNCH);
end;
procedure TForm1.SpeedButton5Click(Sender: TObject);
var
sName :string;
sAddress :string;
begin
sName :='P哥網';
sAddress :='http://www.pggpjj.com';
CreateInternetShortcut(sName, sAddress);
end;