Fix shortcuts in Delphi

zhaozj2021-02-08  261

Fix shortcuts in Delphi

Shortcuts reduce system repetitive files, is a quick starter or open file or folder method, shortcuts are very useful for frequently used programs, files, and folders. In the Windows system, it is full of shortcuts, so how to operate these shortcuts is a very headache problem. In Windows programming, it will undoubtedly encounter problems with the operation shortcut file, such as creating shortcuts for the program. Modify the shortcuts of the program, and so on. In order to make a shortcut, I encapsulate two functions and give a detailed example.

1. Basic information of shortcut file

The information contained in the shortcut is: the target file name, the program runtime, the shortcut, the status, the description, the working directory (starting position), the icon file name, and icon index, etc. When we are operating shortcuts, we should take into account this information.

2. Data structure

For convenience and quick operation, it is necessary to define a data structure so that the necessary information is passed when the function is called:

Const

CCH_MAXNAME = 255; // The size of the buffer described

LNK_RUN_MIN = 7; // Minimize time

LNK_RUN_MAX = 3; // Run is maximized

LNK_RUN_NORMAL = 1; // Normal window

TYPE LINK_FILE_INFO = Record

Filename: Array [0..max_path] of char; // target file name

Workdirectory: Array [0..max_path] of char; // work directory or start directory

IconLocation: array [0..max_path] of char; // icon file name

IconIndex: integer; // icon index

Arguments: array [0..max_path] of char; // The parameters running

Description: array [0..cch_maxname] of char; // shortcut description

Itemidlist: pitemidlist; // only for reading

RelativePath: Array [0..255] of char; // relative catalog, only set

ShowState: integer; // Run window status

Hotkey: Word; // Shortcut

END;

3. Function

1) Net shortcut or modify the information function LINKFILEINFO

It is to be noted that the three units need to add COMOBJ, ACTIVEX, and SHLOBJ in the USES section, but in this example, because of the use of hot keys to be text and using Windows API SHELLEXECUTE, you have to add Menus and SHELLAPI units. At the same time, it is not perfect for exceptional processing. You can modify it according to the actual situation.

Description: The function has three parameters, where the first parameter is a file name of the shortcut to perform, the second is the structure of link_file_info, for receiving information or input information, modifying the shortcut, the third parameter indicates that it is Read shortcuts or set shortcuts. Returns true when the function is successful, otherwise false.

prototype:

Function LinkFileinfo (const lnkfilename: string; var info: link_file_info; const boolean: boolean; var

HR: hRESULT;

PSL: ishelllink;

WFD: Win32_find_data;

PPF: IPERSISTFILE;

LPW: PWIDECHAR;

BUF: PWIDECHAR;

Begin

Result: = FALSE;

GetMem (buf, max_path);

Try

If succeededed (Coinitialize (nil)) THEN

IF (CoCreateInstance (CLSID_SHELLINK, NIL, CLSCTX_INPROC_SERVER, IID_ID_ILLLLINKA, PSL)) Then

Begin

HR: = psl.queryinterface (IPERSISTFILE, PPF);

If succeeded (hr) THEN

Begin

LPW: = StringTowideChar (lnkfilename, buf, max_path);

HR: = PPF.LOAD (LPW, STGM_READ);

If succeeded (hr) THEN

Begin

HR: = psl.resolve (0, SLR_NO_UI);

If succeeded (hr) THEN

Begin

IF bset kil

Begin

Psl.setrGuments; Info.arguments

PSL.SetDescription; Info.description;

PSL.SETHOTKEY (Info.HotKey);

Psl.SeticonLocation (Info.iconLocation, Info.iconIndex);

Psl.setidList (Info.ItemidList);

Psl.SetPath (Info.FileName);

psl.setshowcmd (info.showstate);

Psl.setRelativePath (Info.RaaturePath, 0);

Psl.SetworkDirectory; Info.workdirectory;

Result: = succeeded (PSL.Resolve (0, SLR_UPDATE));

end

Else

Begin

PSL.GetPath (Info.fileName, Max_Path, WFD, SLGP_SHORTPATH);

PSL.GeticonLocation (Info.iconLocation, Max_Path, Info.iconIndex);

PSL.GetWorkingDirectory (Info.workdirectory, Max_Path);

PSL.GetDescription (Info.description, CCH_MaxName);

PSL.Getarguments (Info.arguments, Max_Path);

PSL.GethTKey (Info.HotKey);

PSL.GetidList (Info.ientmidList);

Psl.getshowcmd (info.showstate);

RESULT: = TRUE;

END;

END;

END;

END;

END;

Finally

FreeMem (buf);

END;

END;

2) Establish a shortcut function CreatelinkFile

Description: The first parameter is a link_file_info structure, you must initialize, the second parameter is the file name to save shortcuts, the default to the same name of the LNK file in the same directory.

prototype:

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 ashelllink;

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.SetHotKey (Info.HotKey);

SHLINK.SETSHOWCMD (Info.showState);

Shlink.setRelativePath (Info.RaaturePath, 0);

IF destfilename = '' THEN

WFileName: = ChangefileExt (Info.fileName, 'lnk');

Result: = succeeded (pfile.save (pwchar (wfilename), false);

END;

3) Function shortcuttostring is used to convert the hot bond in the shortcut to string to facilitate display, Delphi itself does not provide the associated function.

Function ShortcuttoTString (const hotkey: word): string;

VAR

Shift: TshiftState;

Begin

SHIFT: = [];

IF (WordRec (Hotkey) .hi shr 0) and 1) <> 0 THEN

INCLUDE (Shift, Ssshift);

IF ((Hotkey) .hi shr 1) and 1) <> 0 THEN

Include (Shift, SSCtrl);

IF (WordRec (Hotkey) .hi Shr 2) and 1) <> 0 THEN

Include (Shift, SSALT);

Result: = Shortcuttotext (Shortc (Wordrec (Wordrec (WordRec (Hotkey) .lo, Shift);

END;

4. Call instance:

Create a new Application, put an OpenDialog, three button, one edit on the form, setting OpenDialog's properties Option part of the OfnodeReferenceLinks for True, Filter property is lnk file | * .Lnk; then add button1, button2 and button3 additionally added Code (Note Modify the corresponding file name and path):

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

VAR

INFO: LINK_FILE_INFO;

Begin

IF openDialog1.execute the

IF linkfileinfo (OpenDialog1.FileName, Info) THEN

begin showmessage ( 'FileName:' info.filename # 13 'Description:' info.Description # 13 'IconFilename:' info.IconLocation ',' inttostr (info.IconIndex) # 13 'WorkDir: ' info.workdirectory # 13 ' arguments: ' info.Arguments # 13 ' Shorcuts: ' ShortcuttoString (Info.Hotkey) # 13 ' WindowsTate: ' INTOSTR (Info.showState) # 13 ' ItemidList: (' ItemIdlist.mkid.cb) ', (' ItemidList.mkid.abid [0]) ')) '); info.workdirectory: = Edit1.Text;

LinkfileInfo (OpenDialog1.FileName, Info, True);

END;

END;

Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);

Begin

Shellexecute (Handle, 'Open', 'Start.exe', 'C: /Windows/desktop/aaa.lnk', '', sw_hide);

END;

Procedure TFORM1.BUTTON3CLICK (Sender: TOBJECT);

VAR

INFO: LINK_FILE_INFO;

Begin

Strpcopy (Info.fileName, paramstr (0)); // Shortcut target file name

INFO.Arguments: = '/ paramstr'; // Setting the parameters running

Info.description: = 'Test for link file'; // Description

Info.HotKey: = 0; // No hotkey

Info.iconindex: = 0; // First icon

Info.iiconLocation: = '; // Set the icon file as itself

Info.RaaturePath: = 'windows'; // relative path

Info.showState: = lnk_run_max; // Display Maximization Window

Info.itemidlist: = nil; // Keep unused, can not be set

Strpcopy (info.workdirectory, extractfilepath); // Settings a work directory

CreatelinkFile (Info, 'C: /Windows/desktop/project.lnk'); // Establish shortcut

END;

5. Express operation

There are two ways:

The first is relatively simple, call directly, but subject to some constraints - the START.EXE file must be present in the system:

Shellexecute (Handle, 'Open', 'Start.exe', 'C: /Windows/desktop/aaa.lnk', '', sw_hide); the second is to use SHELLEXECUTE to be called after obtaining the LNK information! For simplicity, no detailed example is given.

The above procedures are passed under Delphi 5.0 Windows 98. If there is any comments and suggestions, welcome to the letter discussion:

E_mail: kingron@163.net

转载请注明原文地址:https://www.9cbs.com/read-2679.html

New Post(0)