Author: William Gerbert
How do I add some items to Explorer's right-click menu, which appears when the user
right-clicks on a certain file type?
Answer:
1 procedure add_context_menu;
2 type
3 extns = (wav, png, bmp, jpg);
4 const
5 ext_names: array[extns] of string = ('.wav', '.png', '.bmp', '.jpg');
6 var
7 ext: extns;
8 reg: TRegistry;
9 name: string;
10 command: string;
11 begin
12 reg := TRegistry.Create;
13 reg.RootKey := HKEY_CLASSES_ROOT;
14 {Build the command string we want to store}
15 command := '"' + Application.ExeName + '" "%1"';
16 {Loop over extensions we can handle}
17 for ext := wav to jpg do
18 begin
19 {See if this extension is already known in HKEY_CLASSES_ROOT}
20 if reg.OpenKeyReadOnly('\' + ext_names[ext]) then
21 begin
22 name := reg.ReadString(''); {Get the name of this type}
23 if name <> '' then
24 {If not blank, open this type's shell key, but don't create it}
25 if reg.OpenKey('\' + name + '\shell', False) then
26 {Try to create a new key called "APTprocess". Note that for Delphi5 we
27 need to set the access explicitly}
28 reg.Access := KEY_READ or KEY_WRITE;
29 if reg.OpenKey('APTprocess', True) then
30 begin
31 {The default value will be displayed in the context menu}
32 reg.WriteString('', '&APT process');
33 {So now open the command key, creating it if required}
34 reg.Access := KEY_READ or KEY_WRITE;
35 if reg.OpenKey('command', True) then
36 {and write the command string as the default value}
37 reg.WriteString('', command);
38 end;
39 end;
40 end;
41 reg.Free;
42 end;
|