{"id":178,"date":"2009-01-03T16:50:00","date_gmt":"2009-01-03T08:50:00","guid":{"rendered":""},"modified":"2013-11-18T21:40:47","modified_gmt":"2013-11-18T13:40:47","slug":"%e5%bb%ba%e7%ab%8b%e5%bf%ab%e6%8d%b7%e6%96%b9%e5%bc%8f%e7%9a%84%e5%87%bd%e6%95%b0-createshortcut","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/178.html","title":{"rendered":"\u5efa\u7acb\u5feb\u6377\u65b9\u5f0f\u7684\u51fd\u6570: CreateShortcut"},"content":{"rendered":"<pre class=\"brush: delphi; title: ; notranslate\" title=\"\">\r\nuses\r\nShlObj, ActiveX, ComObj; {\u8be5\u51fd\u6570\u4f7f\u7528\u7684\u5355\u5143}\r\n{\u51fd\u6570\u8bf4\u660e:}\r\n{\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u8981\u5efa\u7acb\u5feb\u6377\u65b9\u5f0f\u7684\u6587\u4ef6, \u8fd9\u662f\u5fc5\u987b\u7684; \u5176\u4ed6\u90fd\u662f\u53ef\u9009\u53c2\u6570}\r\n{\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u5feb\u6377\u65b9\u5f0f\u540d\u79f0, \u7f3a\u7701\u4f7f\u7528\u53c2\u6570\u4e00\u7684\u6587\u4ef6\u540d}\r\n{\u7b2c\u4e09\u4e2a\u53c2\u6570\u662f\u6307\u5b9a\u76ee\u7684\u6587\u4ef6\u5939, \u7f3a\u7701\u76ee\u7684\u662f\u684c\u9762; \u5982\u679c\u6709\u7b2c\u56db\u4e2a\u53c2\u6570, \u8be5\u53c2\u6570\u5c06\u88ab\u5ffd\u7565}\r\n{\u7b2c\u56db\u4e2a\u53c2\u6570\u662f\u7528\u5e38\u6570\u7684\u65b9\u5f0f\u6307\u5b9a\u76ee\u7684\u6587\u4ef6\u5939; \u8be5\u7cfb\u5217\u5e38\u6570\u5b9a\u4e49\u5728 ShlObj \u5355\u5143, CSIDL_ \u6253\u5934}\r\nfunction CreateShortcut(Exe:string; Lnk:string = \u201d; Dir:string = \u201d; ID:Integer = -1):Boolean;\r\nvar\r\nIObj: IUnknown;\r\nILnk: IShellLink;\r\nIPFile: IPersistFile;\r\nPIDL: PItemIDList;\r\nInFolder: array&#x5B;0..MAX_PATH] of Char;\r\nLinkFileName: WideString;\r\nbegin\r\nResult := False;\r\nif not FileExists(Exe) then Exit;\r\nif Lnk = \u201d then Lnk := ChangeFileExt(ExtractFileName(Exe), \u201d);\r\n\r\nIObj := CreateComObject(CLSID_ShellLink);\r\nILnk := IObj as IShellLink;\r\nILnk.SetPath(PChar(Exe));\r\nILnk.SetWorkingDirectory(PChar(ExtractFilePath(Exe)));\r\n\r\nif (Dir = \u201d) and (ID = -1) then ID := CSIDL_DESKTOP;\r\nif ID &gt; -1 then\r\nbegin\r\n    SHGetSpecialFolderLocation(0, ID, PIDL);\r\n    SHGetPathFromIDList(PIDL, InFolder);\r\n    LinkFileName := Format(\u2018%s\\%s.lnk\u2019, &#x5B;InFolder, Lnk]);\r\nend else\r\nbegin\r\n    Dir := ExcludeTrailingPathDelimiter(Dir);\r\n    if not DirectoryExists(Dir) then Exit;\r\n    LinkFileName := Format(\u2018%s\\%s.lnk\u2019, &#x5B;Dir, Lnk]);\r\nend;\r\n\r\nIPFile := IObj as IPersistFile;\r\nif IPFile.Save(PWideChar(LinkFileName), False) = 0 then Result := True;\r\nend; {CreateShortcut \u51fd\u6570\u7ed3\u675f}\r\n\r\n{\u6d4b\u8bd5 1: \u628a\u5f53\u524d\u7a0b\u5e8f\u5728\u684c\u9762\u4e0a\u5efa\u7acb\u5feb\u6377\u65b9\u5f0f}\r\nprocedure TForm1.Button1Click(Sender: TObject);\r\nbegin\r\nCreateShortcut(Application.ExeName);\r\nend;\r\n\r\n{\u6d4b\u8bd5 2: \u5728\u684c\u9762\u4e0a\u5efa\u7acb\u5feb\u6377\u65b9\u5f0f, \u540c\u65f6\u6307\u5b9a\u5feb\u6377\u65b9\u5f0f\u540d\u79f0}\r\nprocedure TForm1.Button2Click(Sender: TObject);\r\nbegin\r\nCreateShortcut(Application.ExeName, \u2018NewLinkName\u2019);\r\nend;\r\n\r\n{\u6d4b\u8bd5 3: \u5728 C:\\ \u4e0b\u5efa\u7acb\u5feb\u6377\u65b9\u5f0f}\r\nprocedure TForm1.Button3Click(Sender: TObject);\r\nbegin\r\nCreateShortcut(Application.ExeName, \u201d, \u2018C:\\\u2019);\r\nend;\r\n\r\n{\u6d4b\u8bd5 3: \u5728\u5f00\u59cb\u83dc\u5355\u7684\u7a0b\u5e8f\u6587\u4ef6\u5939\u4e0b\u5efa\u7acb\u5feb\u6377\u65b9\u5f0f}\r\nprocedure TForm1.Button4Click(Sender: TObject);\r\nbegin\r\nCreateShortcut(Application.ExeName, \u201d, \u201d, CSIDL_PROGRAMS);\r\nend;\r\n\r\nend.\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>uses ShlObj, ActiveX, ComObj; {\u8be5\u51fd\u6570\u4f7f\u7528\u7684\u5355\u5143} {\u51fd\u6570\u8bf4\u660e:} {\u7b2c\u4e00\u4e2a\u53c2\u6570 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-178","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=178"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":4804,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/178\/revisions\/4804"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}