怎样为文件创建快捷方式?

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
    tmpObject : IUnknown;
    tmpSLink : IShellLink;
    tmpPFile : IPersistFile;
    PIDL : PItemIDList;
    StartupDirectory : array[0..MAX_PATH] of Char;
    StartupFilename : String;
    LinkFilename : WideString;
    begin
    StartupFilename := 'c:\windows\notepad.exe';
    tmpObject := CreateComObject(CLSID_ShellLink);//创建建立快捷方式的外壳扩展
    tmpSLink := tmpObject as IShellLink;//取得接口
    tmpPFile := tmpObject as IPersistFile;//用来储存*.lnk文件的接口
    tmpSLink.SetPath(pChar(StartupFilename));//设定notepad.exe所在路径
    tmpSLink.SetWorkingDirectory(pChar(ExtractFilePath(StartupFilename)));//设定工作目录
    SHGetSpecialFolderLocation(0,
    CSIDL_DESKTOPDIRECTORY,
    PIDL);//获得桌面的Itemidlist
    SHGetPathFromIDList(PIDL,
    StartupDirectory);//获得桌面路径
    LinkFilename := StartupDirectory + '\MyNotepad.lnk';
    tmpPFile.Save(pWChar(LinkFilename),FALSE);//保存*.lnk文件
    end;
      

  2.   

    const
      CCH_MAXNAME=255;
      LNK_RUN_MIN=7;
      LNK_RUN_MAX=3;
      LNK_RUN_NORMAL=1;type LINK_FILE_INFO=record
             FileName:array[0..MAX_PATH] of char;
             WorkDirectory:array[0..MAX_PATH] of char;
             IconLocation:array[0..MAX_PATH] of char;
             IconIndex:integer;
             Arguments:array[0..MAX_PATH] of char;
             Description:array[0..CCH_MAXNAME] of char;
             ItemIDList:PItemIDList;
             RelativePath:array[0..255] of char;
             ShowState:integer;
             HotKey:word;
         end;function CreateLinkFile(const info:LINK_FILE_INFO;const DestFileName:string=''):boolean;
    var
     anobj:IUnknown;
     shlink:IShellLink;
     pFile:IPersistFile;
     wFileName:widestring;
    begin
     wFileName:=destfilename;
     anobj:=CreateComObject(CLSID_SHELLLINK);
     shlink:=anobj as IShellLink;
     pFile:=anobj as IPersistFile;
     shlink.SetPath(info.FileName);
     shlink.SetWorkingDirectory(info.WorkDirectory);
     shlink.SetDescription(info.Description);
     shlink.SetArguments(info.Arguments);
     shlink.SetIconLocation(info.IconLocation,info.IconIndex);
    // shlink.SetIDList(info.ItemIDList);
     shlink.SetHotkey(info.HotKey);
     shlink.SetShowCmd(info.ShowState);
     shlink.SetRelativePath(info.RelativePath,0);
     if DestFileName='' then
      wFileName:=ChangeFileExt(info.FileName,'lnk');
     result:=succeeded(pFile.Save(pwchar(wFileName),false));
    end;