Delphi programming implementation program shortcut

zhaozj2021-02-16  111

Abstract: This article mainly introduces a shortcut method for programming the program with Delphi, which is mainly discussed in Delphi's method, shortcut implementation method, and gives an instance program.

Keywords: COM programming, shortcut

The Windows system provides shortcut to simplify the user's execution of the application, and the user does not hang the relationship with the specific location of the application, which greatly facilitates the user's operation. So, under the Windows system, most installed programs generally provide creating program groups and shortcuts. However, have you ever thought about it, how is the shortcut implementation?

Below, I will discuss the principle about the shortcut programming, and give you a specific sample program.

First, programming principle

1. First find the shortcut storage location and create a program group.

Under the Windows system, the so-called program group is a folder, but the storage position of these folders is quite special, and the "D: / Documents and Settings / All Users.Winnt /" Start "menu / program" folder ( Under Windows2000), you can open this folder, is it possible to see the items that I can only see before "Start-Program" ?! So, you must first get a shortcut storage location, to solve this problem It can have two solutions.

First, the API function under Windows 2000 is implemented:

HRESULT SHGETSPECIALFOLDERLOCATION

HWND HWNDOWNER,

Int nfolder,

LPITEMIDLIST * PPIDL

);

Second, through the registry, open the registry, then open "HKEY_CURRENT_USER / SOFTWARE / Microsoft / Windows / CurrentVersion / Explorer / shell folders", you can find the storage location such as "Start Menu", "Programs" and other folders .

I understand that the program group is a folder, and it is very simple to build a program group, as long as you create a folder named after this folder in the "program" folder. For example, we store the file folder where the "program" found is located in Directory, and the name of the program group is saved in GroupName, the following code can be implemented in the Program folder:

Directory: = reg.readstring ('programs');

Subdir: = Directory '/' groupname;

CreatedIR (Subdir);

The general application is implemented by the API function. In this example, we are implemented in the second method.

2, create shortcuts

Solved two small questions in front, let's see how to specifically implement the creation of shortcuts. If you write a program frequently, you may think of using the API function, unfortunately, you can't find a shortcut to create a shortcut in the API function, because this feature needs to be implemented with COM. Some people may feel strange and fear for COM. In fact, COM programming is not complicated, you don't need to know COM, you can use it well.

Windows provides a COM interface ishelllink, which can help us create, modify, and delete shortcuts. To use the function provided by the COM interface, you must first call the CreateComObject function to create an instance of an interface. Delphi provides a base class iUnknown, which creates COM, and the specific sample program is as follows:

VAR

MyObject: iunknown; // iUnknown is the base class of COM myslink: ishelllink; // True objects for creating shortcuts

MyObject: = CreateComobject (CLSID_SHELLLINK);

Myoslink: = myObject as ishelllink;

After creating an instance, by calling the setPath () method to set the path of the source program, the shortcut storage location should be: "program" position program group name shortcut name extension .Lnk; via setDescription Method Setting up a shortcut description, of course, you can also call such as setHotKey () and other methods to set the hotkey, the displayed icon (the icon for the actual program), etc. In fact, this interface function is far more than this, limited to space, and slightly somed directly.

Setting the shortcuts are not saved to the disk, but also implement it through the Save () method of the IPersistFile object. As of the following code:

Mypfile: = myObject as IPERSISTFILE;

Mypfile.save (Pwchar (WFileName), TRUE;

It should be noted that: shortcuts should be stored in Unicode, so you must call the PWCHAR () function to convert it.

Second, the programming example

Below, let us use Delphi6 to programmatically implement a simple sample program. The functionality of the program is as follows: You can enter the location of the source program in the three text boxes of the window (you can also implement the name and shortcut name by browsing the button), the program group name, and shortcut name, select the shortcut storage position, single Create a program group and shortcut to create a "Create" button. Is it cool? The heart is not as good as action, let's take a step by step.

1. Start Delphi6.0 to open a new standard project. Add the required controls in the form.

First add four Label objects, three Edit objects, a drop-down frame object ComboBox1, three button objects, and an OpenDialog object. Set its related properties in sequence, the adjusted form interface is shown in Figure 1.

2, add a function to the TForm1 object:

Function CreateShortcut (SourceFileName: String; // Location of the source program

Shortcutname: String; // Shortcut

Shortcutlocation: SHORTCUTTYPE; // Receipt location

Subdirectory: String // Name of the program group

: Boolean;

Where ShortcutType is a customized enumeration class in the program:

Type

Shortcuttype = (_ Desktop, // Desktop

_StartMenu, // Start menu

_Programs, //

_Startup, // Start

_QuickLaunch // Quick Start Bar

);

The createshortcut () function is used to implement the creation of shortcuts and program groups. If you create success, return a true value (TRUE), otherwise return to false. This is the most important code segment in this article!

Function TFORM1.CREATESHORTCUT (SOURTCUTNAME: STRING; Shortcutlocation: ShortcutType; Subdirectory: String): boolean;

Const

REG_SHELLFOLDERS =

'Software / Microsoft / Windows / CurrentVersion / Explorer / Shell Folders'; VAR

MyObject: iunknown; // iUnknown is the basic COM class

MySLink: ishelllink; // True objects for creating shortcuts

Mypfile: ipersistfile; // Types for saving shortcuts

Subdir, Directory, LinkName: String

WFileName: WideString;

REG: TREGISTRY;

Begin

Result: = FALSE;

MyObject: = CreateComobject (CLSID_SHELLLINK); // Establish a shortcut COM object

Myoslink: = myObject as ishelllink;

Mypfile: = myObject as IPERSISTFILE;

MySLink.SetPath (Pchar (SourceFileName)); // Set the location of the shortcut source file

REG: = Tregistry.create;

Reg.rootkey: = HKEY_CURRENT_USER;

Try

IF reg.openkey (reg_shellfolders, false) THEN

Begin

IF SourceFileName <> 'THEN

If Shortcutname <> '' THEN

LinkName: = Shortcutname

Else

LinkName: = extractFileName (SourceFileName)

Else // When the source path is an null value, it actually points to "My Computer"

LinkName: = 'My Computer';

MySLink.SetDescription (PCHAR (LINKNAME)); // Set shortcut description

LinkName: = ChangefileExt (LinkName, '. Lnk'); // Modify the extension

// The following is the actual establishment location of the shortcut through the registry.

Case Shortcutlocation of

_Desktop: Directory: = reg.readstring ('desktop ");

_StartMenu: Directory: = reg.readstring ('startmenu');

_Programs: Directory: = reg.readstring ('programs');

_Startup: Directory: = reg.readstring ('startup');

_QuickLaunch: Directory: = reg.readstring ('appdata')

'/ Microsoft / Internet Explorer / Quick Launch'; // The path is relatively special

END;

IF Directory <> '' THEN

Begin

If Subdirectory <> '' THEN

Begin

Subdir: = Directory '/' Subdirectory;

WFileName: = Subdir '/' linkname

If not createdir (subdir) THEN / / creation directory failed

Begin

Result: = FALSE;

EXIT;

END;

Endelse

WFileName: = Directory '/' linkname

IF mypfile.save (pwchar (wfilename), true) <> s_ok then

Begin

Result: = FALSE;

EXIT;

end

Else

RESULT: = TRUE

end

Else

Result: = FALSE;

END;

Finally

Reg.free; // Release space

END;

END;

3. Write the code of the browse file button, as follows:

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

IF openDialog1.execute the

Edit1.Text: = Opendialog1.FileName;

END;

4, the same step to write the "exit" button code:

Procedure TFORM1.BUTTON3CLICK (Sender: TOBJECT);

Begin

CLOSE;

END;

5. The following is the code of the "Create" button, call the CREATESHOUT () function we create directly to implement:

Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);

VAR

SlinkType: Shortcuttype;

Begin

Case ComboBox1.ItemIndex of

0: SlinkType: = _desktop;

1: SlinkType: = _StartMenu;

2: SlinkType: = _programs;

3: SlinkType: = _Startup;

4: SlinkType: = _quicklaunch;

END;

If Createshortcut (Edit1.Text, Edit3.Text, SlinkType, Edit2.Text) THEN

Application.MessageBox (pchar ('shortcut creation success!'), Pchar ('system message'), MB_OK)

Else

Application.MessageBox (Pchar ('shortcut creation failed!'), PCHAR ('System Messages'), MB_OK;

END;

Now press F9 to compile this program, you can try to create a shortcut effect. The operation of the program is shown in Figure 2:

Of course, you can also modify the program after your installation system, realize a personalized installer, that feels better!

The program is compiled under Windows2000, Delphi6.0.

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

New Post(0)