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');
The item IDs you use ($100 in this example) need to be multiples of 16, in hex
notation that means the last digit is 0. Take care not to conflict with the
predefined SC_ menu constants for the system menu. All of these have values above
61000.
To get informed when the user selects your new item you need to hande the
WM_SYSCOMMAND message send to the Application window. For this you attach a hook to
it, using Application.HookMainWindow.
50 procedure TForm1.FormCreate(Sender: TObject);
51 var
52 hSysMenu: HMENU;
53 begin
54 hSysMenu := getSystemMenu(application.handle, false);
55 AppendMenu(hSysmenu, MF_STRING, $100, 'My Item');
56 Application.HookMainWindow(AppHook);
57 end;
58
59 function Tform1.AppHook(var message: TMessage): Boolean;
60 begin
61 if (message.Msg = WM_SYSCOMMAND) and ((message.WParam and $FFF0) = $100) then
62 begin
63 Result := true;
64 ShowMessage('Hi, folks');
65 end
66 else
67 Result := false;
68 end;
|