Author: Jonas Bilinkevicius
I want to add a menu item to the menu, which pops up when you right-click on the
taskbar button for your application. I know how to add item to form system menu,
but it doesn't change the popup menu on the taskbar.
Answer:
Solve 1:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls, ExtCtrls;
8
9 const
10 idSysAbout = 100;
11
12 type
13 TForm1 = class(TForm)
14 procedure FormCreate(Sender: TObject);
15 public
16 procedure AppMessage(var Msg: TMsg; var Handled: Boolean);
17 end;
18
19 var
20 Form1: TForm1;
21
22 implementation
23
24 {$R *.DFM}
25
26 procedure TForm1.AppMessage(var Msg: TMsg; var Handled: Boolean);
27 begin
28 if (Msg.message = WM_SYSCOMMAND) and (Msg.WParam = idSysAbout) then
29 begin
30 ShowMessage('This is a test');
31 Handled := True;
32 end;
33 end;
34
35 procedure TForm1.FormCreate(Sender: TObject);
36 begin
37 AppendMenu(GetSystemMenu(Application.Handle, False), MF_SEPARATOR, 0, '');
38 AppendMenu(GetSystemMenu(Application.Handle, False), MF_STRING, idSysAbout,
39 'Test');
40 Application.OnMessage := AppMessage;
41 end;
42
43 end.
Solve 2:
The popup menu for a Taskbar button is the Application windows system menu. To add
items to it you need to use API functions.
44 { ... }
45 var
46 hSysMenu: HMENU;
47 begin
48 hSysMenu := getSystemMenu(Application.handle, false);
49 AppendMenu(hSysmenu, MF_STRING, $100, 'My Item');
50
51 The item IDs you use ($100 in this example) need to be multiples of 16, in hex
52 notation that means the last digit is 0. Take care not to conflict with the
53 predefined SC_ menu constants for the system menu. All of these have values above
54 61000.
55
56 to get informed when the user selects your new item you need to hande the
57 WM_SYSCOMMAND message send to the Application window. for this you attach a hook to
58 it, using Application.HookMainWindow.
59
60 procedure TForm1.FormCreate(Sender: TObject);
61 var
62 hSysMenu: HMENU;
63 begin
64 hSysMenu := getSystemMenu(application.handle, false);
65 AppendMenu(hSysmenu, MF_STRING, $100, 'My Item');
66 Application.HookMainWindow(AppHook);
67 end;
68
69 function Tform1.AppHook(var message: TMessage): Boolean;
70 begin
71 if (message.Msg = WM_SYSCOMMAND) and ((message.WParam and $FFF0) = $100) then
72 begin
73 Result := true;
74 ShowMessage('Hi, folks');
75 end
76 else
77 Result := false;
78 end;
|