Author: Tomas Rutkauskas
How to add items to a program's system menu
Answer:
Solve 1:
1 unit Unit1;
2
3 interface
4
5 uses
6 SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms,
7 Dialogs, Menus;
8
9 type
10 TForm1 = class(TForm)
11 procedure FormCreate(Sender: TObject);
12 private
13 { Private declarations }
14 public
15 procedure winmsg(var msg: TMsg; var handled: boolean);
16 {This is what handles the messages}
17 procedure Anything; {Procedure to do whatever}
18 end;
19
20 var
21 Form1: TForm1;
22
23 implementation
24
25 {$R *.DFM}
26
27 const
28 ItemID = 99; {The ID number for your menu item - can be anything}
29
30 procedure TForm1.winmsg(var msg: TMsg; var handled: boolean);
31 begin
32 {If the message is a system one ...}
33 if msg.message = WM_SYSCOMMAND then
34 {... then check if its parameter is your Menu items ID}
35 if msg.wparam = ItemID then
36 Anything;
37 end;
38
39 procedure TForm1.FormCreate(Sender: TObject);
40 begin
41 application.onmessage := winmsg;
42 {Tell your app that 'winmsg' is the application message handler}
43 AppendMenu(GetSystemMenu(form1.handle, false), mf_separator, 0, '');
44 {Add a seperator bar to form1}
45 AppendMenu(GetSystemMenu(form1.handle, false), mf_byposition, ItemID, '&New
46 Item'
47 {Add your menu item to form1}
48 AppendMenu(GetSystemMenu(application.handle, false), mf_separator, 0, '');
49 {Add a seperator bar to the application system menu(used when app is minimized)}
50 AppendMenu(GetSystemMenu(application.handle, false), mf_byposition, ItemID, '&New
51 Item'
52 {Add your menu itemto the application system menu(used when app is minimized)}
53 end;
54
55 procedure TForm2.Anything;
56 begin
57 {Add whatever you want to this procedure}
58 end;
59
60 end.
Solve 2:
First, you need to add the items to the existing system menu:
61 var
62 SysMenu: HMenu;
63 { ... }
64 SysMenu := GetSystemMenu(Handle, False);
65 { Note: the low order four bits of the command values must be 0 }
66 AppendMenu(SysMenu, mf_String or mf_ByPosition, $F210, 'New Item 1');
67 AppendMenu(SysMenu, mf_String or mf_ByPosition, $F220, 'New Item 2');
68 { ... }
69
70
71 //Then you need a message handler for the WM_SYSCOMMAND message:
72
73
74 procedure WMSysCommand(var MSg: TWMSysCommand); message WM_SYSCOMMAND;
75
76
77 //Which you implement like so:
78
79
80 procedure TForm1.WMSysCommand(var MSg: TWMSysCommand);
81 begin
82 inherited;
83 case Msg.CmdType of
84 $F210:
85 begin
86 { Handle new item 1 here }
87 ShowMessage('New Item 1');
88 end;
89 $F220:
90 begin
91 { Handle new item 2 here }
92 ShowMessage('New Item 2');
93 end;
94 end;
95 end;
|