Create a shortcut function: createshortcut-very practical and convenient

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; button2: tbutton; button3: tbutton; button4: tbutton; Procedure submit (Sender: tobject ); end; var form1: tform1; implementation {$ R *. DFM} uses shlobj, ActiveX, comobj; {unit used by the function} {Function Description:} {the first parameter is required to create a shortcut file; others are optional parameter} {the second parameter is the shortcut name. By default, the file name of parameter 1 is used} {the third parameter is the specified destination folder, and the default destination is the desktop; if there is a fourth parameter, this parameter will be ignored} {the fourth parameter is to specify the destination folder as a constant; this series of constants are defined in the shlobj unit, csidl _ headers} function createshortcut (exe: string; lnk: String = ''; Dir: String =''; ID: integer =-1): Boolean; var iobj: iunknown; ilnk: ishelllink; ipfile: ipersistfile; pidl: pitemidlist; infolder: array [0 .. max_path] of char; linkfilename: widestring; begin result: = false; if not fileexists (exe) Then exit; If lnk = ''then lnk: = changefileext (extractfilename (exe ), ''); iobj: = createcomobject (clsid_shelllink); ilnk: = iobj as ishelllink; ilnk. setpath (pchar (exe); ilnk. setworkingdirectory (pchar (extractfilepath (exe); If (DIR = '') and (ID =-1) Then ID: = csidl_desktop; if Id>-1 then begin shgetspecialfolderlocation (0, ID, pidl); shgetpathfromidlist (pidl, infolder); linkfilename: = format ('% s \ % S. lnk ', [infolder, lnk]); End else begin dir: = excludetrailingpathdelimiter (DIR); if not directoryexists (DIR) Then exit; linkfilename: = format ('% s \ % S. lnk ', [Dir, lnk]); end; ipfile: = iobj as ipersistfile; If ipfile. save (pwidechar (linkfilename), false) = 0 then result: = true; end; {createshortcut function end} {Test 1: set the currentProgram Create a shortcut on the desktop} procedure tform1.button1click (Sender: tobject); begin createshortcut (application. exename); end; {Test 2: Create a shortcut on the desktop and specify the shortcut name} procedure tform1.button2click (Sender: tobject); begin createshortcut (application. exename, 'newlinkname'); end; {Test 3: Create a shortcut under c: \} procedure tform1.button3click (Sender: tobject); begin createshortcut (application. exename, '', 'c: \ '); end; {Test 3: Create a shortcut in the program folder of the Start Menu} procedure tform1.button4click (Sender: tobject ); begin createshortcut (application. exename, '','', csidl_programs); end.
  
   
 

For the third, four parameters can be referred to: http://www.cnblogs.com/del/archive/2008/09/23/1297323.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.