如何替一个文件建立一个快捷方式?(*.lnk)(123分)

  • 主题发起人 主题发起人 3h
  • 开始时间 开始时间
3

3h

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,如何替一个文件建立一个快捷方式?(*.lnk)<br>我快急狂了,还是找不到这个函数,各位大侠帮帮忙。
 
我这里有一个范例程序,不过有些早了,你可以把TRegIniFile替换为TRegistry<br><br>uses ShellApi,ComObj,Ole;<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; MyObject &nbsp;: IUnknown;<br>&nbsp; MySLink &nbsp; : IShellLink;<br>&nbsp; MyPFile &nbsp; : IPersistFile;<br>&nbsp; FileName &nbsp;: String;<br>&nbsp; Directory : String;<br>&nbsp; WFileName : WideString;<br>&nbsp; MyReg &nbsp; &nbsp; : TRegIniFile;<br>begin<br>&nbsp; MyObject := CreateComObject(CLSID_ShellLink);<br>&nbsp; MySLink := MyObject as IShellLink;<br>&nbsp; MyPFile := MyObject as IPersistFile;<br>&nbsp; FileName := 'NOTEPAD.EXE';<br>&nbsp; with MySLink do begin<br>&nbsp; &nbsp; SetArguments('C:/AUTOEXEC.BAT');<br>&nbsp; &nbsp; SetPath(PChar(FileName));<br>&nbsp; &nbsp; SetWorkingDirectory(PChar(ExtractFilePath(FileName)));<br>&nbsp; end;<br>&nbsp; MyReg := TRegIniFile.Create(<br>&nbsp; &nbsp; 'Software/MicroSoft/Windows/CurrentVersion/Explorer');<br>// Use the next line of code to put the shortcut on your desktop<br>&nbsp; Directory := MyReg.ReadString('Shell Folders','Desktop','');<br>// Use the next three lines to put the shortcut on your start menu<br>// &nbsp;Directory := MyReg.ReadString('Shell Folders','Start Menu','')+<br>// &nbsp; &nbsp; &nbsp;'/Whoa!';<br>// &nbsp;CreateDir(Directory);<br>&nbsp; WFileName := Directory+'/FooBar.lnk';<br>&nbsp; MyPFile.Save(PWChar(WFileName),False);<br>&nbsp; MyReg.Free;<br>end;<br>另外可以用DDE不过有些不保险。
 
去 www.youseful.com 下载一个 youseful试试
 
看一看《delphi 3从入门到精通》李维写的!
 
要产生程序组, 常用的方法是与程序管理员进行 DDE 对话,呼叫程式管理员事先预备<br>好的宏集, 关於这些, 以下有一个例子您可以参考看看:<br>步骤:<br><br>1. File | New Project<br>2. 在 Form 中安置一个 TDDEClientConv 控件<br>3. 对於 DdeClientConv1, 在 Object Inspector 中点一下 DdeService 这个属性,<br>&nbsp; &nbsp;然后点一下在其右方'...'的按钮, 然后在 DdeService 这栏填入 ProgMan,在<br>&nbsp; &nbsp;DdeTopic 这栏填入 Progman 。<br>4. 对于 DdeClientConv1 将 ConnectMode 设为 ddeManual<br>5. 在 Form1 中安排一个 TButton<br>6. 在 Button1 的 OnClick 事件:<br>&nbsp; &nbsp;procedure TForm1.Button1Click(Sender: TObject);<br>&nbsp; &nbsp;var<br>&nbsp; &nbsp; &nbsp; sMacro: String;<br>&nbsp; &nbsp; &nbsp; szMacro: Array[0..254] of Char;<br>&nbsp; &nbsp; &nbsp; sGroupDesc, sGroupName: String;<br>&nbsp; &nbsp; &nbsp; sProgDesc: String;<br>&nbsp; &nbsp; &nbsp; sProgIcon: String;<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; // 打开对话通路 <br>&nbsp; &nbsp; &nbsp; if not DdeClientConv1.OpenLink then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ShowMessage('无法建立 DDE 连结');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Exit;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; // 建立组群 <br>&nbsp; &nbsp; &nbsp; sGroupDesc := '纯测试的 Group';<br>&nbsp; &nbsp; &nbsp; sGroupName := 'Test';<br>&nbsp; &nbsp; &nbsp; // 组成宏指令 <br>&nbsp; &nbsp; &nbsp; sMacro := '[CreateGroup(' + sGroupDesc + ',' + sGroupName + ')]';<br>&nbsp; &nbsp; &nbsp; StrPCopy(szMacro, sMacro); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 转成 Null-Term. 字串 <br>&nbsp; &nbsp; &nbsp; DdeClientConv1.ExecuteMacro(szMacro, False); // 执行宏指令 <br>&nbsp; &nbsp; &nbsp; // 建立 Icon 图像 <br>&nbsp; &nbsp; &nbsp; sProgIcon := 'c:/windows/notepad.exe';<br>&nbsp; &nbsp; &nbsp; sProgDesc := '记事本';<br>&nbsp; &nbsp; &nbsp; sMacro := '[AddItem(' + sProgIcon + ',' + sProgDesc + ')]';<br>&nbsp; &nbsp; &nbsp; StrPCopy(szMacro, sMacro); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // 转成 Null-Term. 字串 <br>&nbsp; &nbsp; &nbsp; DdeClientConv1.ExecuteMacro(szMacro, False);<br>&nbsp; &nbsp; &nbsp; // 关闭 DDE 对话 <br>&nbsp; &nbsp; &nbsp; DdeClientConv1.CloseLink;<br>&nbsp; &nbsp;end;<br>7. 保存后, 执行看看吧!<br>(这是我从别的地方学到的经验,全部原文摘抄,希望对你有用)
 
Borland Tech Doc #3234<br><br>This sample project demonstrates an easy way to add shortcuts to your Windows 95 or Windows NT 4.0 desktop or start<br>menu.<br><br>&nbsp; &nbsp;1.Launch Delphi 3. <br>&nbsp; &nbsp;2.2. In a new project, drop a TButton on the form (make sure it's called Button1). Then double click on Button1. Now<br>&nbsp; &nbsp; &nbsp;you can go ahead and directly replace the code for Unit1 with the code for Unit1 below. <br><br>The program will set up a shortcut either (see the code) on the desktop or on the start menu. The shortcut will be called<br>FooBar and it will open up your AUTOEXEC.BAT in NOTEPAD when executed.<br><br>It will read the value of the "Desktop" and "Start Menu" strings from the registry key named (under<br>HKEY_CURRENT_USER):<br><br>&nbsp;Software/MicroSoft/Windows/CurrentVersion/Explorer/Shell Folders<br><br><br><br>--------------<br>The Unit1 unit<br>--------------<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>uses<br>&nbsp; ShlObj, ActiveX, ComObj, Registry;<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; MyObject &nbsp;: IUnknown;<br>&nbsp; MySLink &nbsp; : IShellLink;<br>&nbsp; MyPFile &nbsp; : IPersistFile;<br>&nbsp; FileName &nbsp;: String;<br>&nbsp; Directory : String;<br>&nbsp; WFileName : WideString;<br>&nbsp; MyReg &nbsp; &nbsp; : TRegIniFile;<br>begin<br>&nbsp; MyObject := CreateComObject(CLSID_ShellLink);<br>&nbsp; MySLink := MyObject as IShellLink;<br>&nbsp; MyPFile := MyObject as IPersistFile;<br>&nbsp; FileName := 'NOTEPAD.EXE';<br>&nbsp; with MySLink do begin<br>&nbsp; &nbsp; SetArguments('C:/AUTOEXEC.BAT');<br>&nbsp; &nbsp; SetPath(PChar(FileName));<br>&nbsp; &nbsp; SetWorkingDirectory(PChar(ExtractFilePath(FileName)));<br>&nbsp; end;<br>&nbsp; MyReg := TRegIniFile.Create(<br>&nbsp; &nbsp; 'Software/MicroSoft/Windows/CurrentVersion/Explorer');<br><br>// Use the next line of code to put the shortcut on your desktop<br>&nbsp; Directory := MyReg.ReadString('Shell Folders','Desktop','');<br><br>// Use the next three lines to put the shortcut on your start menu<br>// &nbsp;Directory := MyReg.ReadString('Shell Folders','Start Menu','')+<br>// &nbsp; &nbsp; &nbsp;'/Whoa!';<br>// &nbsp;CreateDir(Directory);<br><br>&nbsp; WFileName := Directory+'/FooBar.lnk';<br>&nbsp; MyPFile.Save(PWChar(WFileName),False);<br>&nbsp; MyReg.Free;<br>end;<br><br>end.
 
好说,这是我用的通用过程:<br><br>以下程序段来自我的单元myUnit.pas<br>==================================<br>procedure MakeLink(proName,lnkName,Description : String);<br>var<br>&nbsp; WorkDir, DestName: String;<br>&nbsp; aReg &nbsp; : TRegistry;<br>&nbsp; aObj &nbsp; : IUnknown;<br>&nbsp; MyLink : IShellLink;<br>&nbsp; MyPFile: IPersistFile;<br>&nbsp; WFileName: WideString;<br>begin<br>&nbsp; &nbsp; //操作注册表:<br>&nbsp; &nbsp; //键'Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders'<br>&nbsp; &nbsp; //下的Programs串值指定了Windows启动目录的位置.<br>&nbsp; aReg := TRegistry.create;<br>&nbsp; aReg.RootKey:=HKey_Current_User;<br>&nbsp; aReg.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders', False);<br>&nbsp; DestName := aReg.ReadString('Programs') + '/'+lnkName;<br>&nbsp; aReg.Closekey;<br><br>&nbsp; aObj := CreateComObject(CLSID_ShellLink);<br>&nbsp; MyLink := aObj as IShellLink;<br>&nbsp; MyPFile := aObj as IPersistFile;<br><br>&nbsp; WorkDir:= ExtractFilePath(proName);<br>&nbsp; with MyLink do //操作IShellLink类<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //设置显示窗口的类型<br>&nbsp; &nbsp; &nbsp; SetShowCmd(SW_NORMAL);<br>&nbsp; &nbsp; &nbsp; &nbsp; { 对DOS程序,一般建议使用SetShowCmd(SW_SHOWMAXIMIZED); }<br>&nbsp; &nbsp; &nbsp; &nbsp; // 设置命令行参数<br>&nbsp; &nbsp; &nbsp; SetArguments('');<br>&nbsp; &nbsp; &nbsp; &nbsp; // 设置描述<br>&nbsp; &nbsp; &nbsp; SetDescription(pChar(Description));<br>&nbsp; &nbsp; &nbsp; &nbsp; // 设置程序名称[全路径,如果在系统Path中,可直接为程文件名]<br>&nbsp; &nbsp; &nbsp; SetPath(pChar(proName));<br>&nbsp; &nbsp; &nbsp; &nbsp; // 设置工作目录<br>&nbsp; &nbsp; &nbsp; SetWorkingDirectory(pChar(WorkDir));<br>&nbsp; &nbsp; end;<br><br>&nbsp; &nbsp; //取.LNK文件的位置<br>&nbsp; CreateDir(ExtractFilePath(DestName));<br>&nbsp; &nbsp; //建立快捷方式: .LNK文件<br>&nbsp; WFileName := DestName; &nbsp;//将一个String赋给WideString,转换过程由Delphi自动完成<br>&nbsp; MyPFile.Save(PWChar(WFileName), False);<br>end;<br><br>===========<br>&nbsp; &nbsp; 上面的这个.lnk建在win95的start按钮的菜单中,至于要建在其它指<br>定目录中,修改DestName := aReg.ReadString('Programs') + '/'+lnkName;<br>一行就成了。<br><br>===========<br>下面是测试程序,一看就明白了:<br>program test;<br>uses myUnit;<br>begin<br>&nbsp; MakeLink('c:/dos/edit.com','Edit.lnk','这是DOS的Edit.COM程序的测试');<br>&nbsp; MakeLink('c:/dos/scandisk.bat','这是另一个相关测试.lnk','c:/dos/scandisk.bat');<br>&nbsp; MakeLink('c:/dos/scandisk.bat','Aimingoo的程序/这是另一个相关测试.lnk','这样可建立一个文件夹!!!');<br>&nbsp; &nbsp;//如果你要将这个快捷方式放在桌面上,只需要将这个.LNK文件复制到相Windows的DeskTop目录中就可以了.<br>&nbsp; &nbsp;//DeskTop目录的位置可参考MakeLink()函数取得<br>&nbsp; Writeln('Now, You Press Start Button(Win95),Three Links Already Maked!!!');<br>end.<br><br>
 
基本上上朋友们的答案都是差不多的,不过以LARRY为主在的三个朋友是以API来实现,而CHENGANG是以DDE实现。答案我接受了,谢谢大家。
 
多人接受答案了。
 

Similar threads

回复
0
查看
815
不得闲
回复
0
查看
1K
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部