How to create
shortcuts?
Reid Roman <rkroman@pacbell.net>
Borland Tech Doc #3234
This sample
project demonstrates an easy way to add shortcuts to your Windows 95 or Windows
NT 4.0 desktop or start menu.
1.
Launch
Delphi 3.
2.
2. In
a new project, drop a TButton on the form (make sure it's called Button1). Then
double click on Button1. Now you can go ahead and directly replace the code for
Unit1 with the code for Unit1 below.
The program
will set up a shortcut either (see the code) on the desktop or on the start
menu. The shortcut will be called FooBar and it will open up your AUTOEXEC.BAT
in NOTEPAD when executed.
It will
read the value of the "Desktop" and "Start Menu" strings
from the registry key named (under HKEY_CURRENT_USER):
Software\MicroSoft\Windows\CurrentVersion\Explorer\Shell Folders
--------------
The Unit1 unit
--------------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses
ShlObj, ActiveX, ComObj, Registry;
procedure
TForm1.Button1Click(Sender: TObject);
var
MyObject : IUnknown;
MySLink : IShellLink;
MyPFile : IPersistFile;
FileName : String;
Directory : String;
WFileName : WideString;
MyReg : TRegIniFile;
begin
MyObject := CreateComObject(CLSID_ShellLink);
MySLink := MyObject as IShellLink;
MyPFile := MyObject as IPersistFile;
FileName := 'NOTEPAD.EXE';
with MySLink do begin
SetArguments('C:\AUTOEXEC.BAT');
SetPath(PChar(FileName));
SetWorkingDirectory(PChar(ExtractFilePath(FileName)));
end;
MyReg := TRegIniFile.Create(
'Software\MicroSoft\Windows\CurrentVersion\Explorer');
// Use the next line of code to put
the shortcut on your desktop
Directory := MyReg.ReadString('Shell Folders','Desktop','');
// Use the next three lines to put
the shortcut on your start menu
//
Directory := MyReg.ReadString('Shell Folders','Start Menu','')+
// '\Whoa!';
//
CreateDir(Directory);
WFileName := Directory+'\FooBar.lnk';
MyPFile.Save(PWChar(WFileName),False);
MyReg.Free;
end;
end.