Create Shortcut

Unit : ShlObj, ActiveX, ComObj

In the Delphi programming, to create a shortcut can try the example program below:

function GetDesktopDir:string;
var mDesktopDir : array[0..MAX_PATH] of Char;
    mPIDL       :PItemIDList;
begin
 //obtain the directory path to the desktop
 SHGetSpecialFolderLocation(0,CSIDL_DESKTOPDIRECTORY,mPIDL);
 //change special folder into a string
 SHGetPathFromIDList(mPIDL,mDesktopDir);
 result:=mDesktopDir;
end;

procedure TF_MembuatShortcut.Button1Click(Sender: TObject);
begin
 if OpenDialog1.Execute then begin
  Edit1.Text := OpenDialog1.FileName;
  Edit2.Text := ExtractFileDir(OpenDialog1.FileName);
  Edit3.Text := ExtractFileName(OpenDialog1.FileName);
  Edit4.Text := '';
 end;
end;

procedure TF_MembuatShortcut.Button2Click(Sender: TObject);
var mObjectTemp   : IUnknown;
    mSLinkTemp    : IShellLink;
    mFileTemp     : IPersistFile;
    mLinkFileName : WideString;
begin
 
//ComObject initialization that will be used to shortcut
 mObjectTemp := CreateComObject(CLSID_ShellLink);
 mSLinkTemp  := mObjectTemp as IShellLink;
 mFileTemp   := mObjectTemp as IPersistFile;
 //determine information shortcuts
 with mSLinkTemp do
 begin
  SetPath(PChar(Edit1.Text));
  SetWorkingDirectory(PChar(Edit2.Text));
  SetDescription(PChar(Edit4.Text));
 end;
 //determine the shortcut file
 mLinkFileName := GetDesktopDir+'\'+Edit3.Text+'.lnk';
 mFileTemp.Save(PWChar(mLinkFileName),False);
end;

No comments:

Post a Comment