热情软件屋

 

使用Delphi建立文件的快捷方式


编号:QA000360
建立日期: 1999年1月18日 最后修改日期:2000年2月20日
所属类别:

drx:
    工具:Delphi 3
    操作系统: Win97
    我如何在 Windows 下使用 Delphi 建立文件的快捷方式,就是扩展名为 .lnk的文件,是不是得调用 Windows Api函数,如果使用,将是哪一个?谢谢!!

回答:

    要建立快捷方式,并不是直接调用哪个API函数,而是要利用CreateComObject(CLSID_ShellLink)建立对象ShellLink。下面就是一段程序,它可以在Win95启动目录下建立快捷方式。如果想知道C++和VB怎么做这一工作,可以参考QA000083 "使用IShellLink来控制快捷方式文件"QA000154 "用VB创建快捷方式"
    要使用有关对象,不要忘记在引用单元部分加上ShlObj和ComOb。
    unit Unit1;
    
    interface
    
    uses
     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
     Dialogs, StdCtrls;
    
    type
     TForm1 = class(TForm)
     Button1: TButton;
     procedure Button1Click(Sender: TObject);
     private
     { Private declarations }
     public
     { Public declarations }
     end;
    
    var
     Form1: TForm1;
    
    implementation
    
    {$R *.DFM}
    
    uses
     ShlObj, ActiveX, ComObj, Registry;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
     MyObject : IUnknown;
     MySLink : IShellLink;
     MyPFile : IPersistFile;
     FileName : String;
     Directory : String;
     WFileName : WideString;
     MyReg : TRegIniFile;
    begin
     MyObject := CreateComObject(CLSID_ShellLink);
     MySLink := MyObject as IShellLink;
     MyPFile := MyObject as IPersistFile;
     FileName := 'NOTEPAD.EXE';
     with MySLink do begin
     SetArguments('C:\AUTOEXEC.BAT');
     SetPath(PChar(FileName));
     SetWorkingDirectory(PChar(ExtractFilePath(FileName)));
     end;
     MyReg := TRegIniFile.Create(
     'Software\MicroSoft\Windows\CurrentVersion\Explorer');
    
    // Use the next line of code to put the shortcut on your desktop
     Directory := MyReg.ReadString('Shell Folders','Desktop','');
    
    // Use the next three lines to put the shortcut on your start menu
    // Directory := MyReg.ReadString('Shell Folders','Start Menu','')+
    // '\Whoa!';
    // CreateDir(Directory);
    
     WFileName := Directory+'\FooBar.lnk';
     MyPFile.Save(PWChar(WFileName),False);
     MyReg.Free;
    end;
    
    end.
    如果要得到快捷文件的属性,则先应调用IPersistFile对象的Load,然后调用IShellLink的GetPath等函数以获得各种属性(详见Win32 API帮助)。如:
     // Load .lnk file
     WFileName := ExpandFileName(Sr.Name);
     MyPFile.Load(PWChar(WFileName), STGM_DIRECT);
    
     // Retrieve the hotkey.
     MySLink.GetHotKey(wHotKey);
    
     // Retrieve the icon.
     MySLink.GetIconLocation(Filename, 255, nIndex);
     if strLen(Filename) <> 0 then
     begin
     MyIcon := TIcon.Create;
     MyIcon.Handle := ExtractIcon(hInstance, Filename, nIndex);
     ListItem.ImageIndex := frmMain.ImageList1.AddIcon(MyIcon);
     MyIcon.Free;
     end
     else
     begin
     MySLink.GetPath(Filename, 255, fd, SLGP_UNCPRIORITY);
     MyIcon := TIcon.Create;
     nIndex2 := 0;
     MyIcon.Handle := ExtractAssociatedIcon(hInstance, Filename, nIndex2);
     ListItem.ImageIndex := frmMain.ImageList1.AddIcon(MyIcon);
     MyIcon.Free;
     end;
    另外,可以在http://delphi.icm.edu.pl/ftp/d30free/PDJ_Shortcut.zip下载免费的建立快捷方式的VCL控件。

此问题由李海回答。

 
把这个问题推荐给朋友
   
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio