Author: Lou Adler
How can I add my own custom menu item to another application - one I haven't
written?
Answer:
This tip is something that I've wanted to do for awhile, but kept on forgetting to
write the article for it. It involves adding a menu choice to the system menu of an
application. For the most part, you'll never have a need to do this. But there are
some things like setting a form style, or some other action that is more system
oriented than application oriented that just belong in the system menu. Well, here
it is folks, and as usual, it's pretty incredibly easy to implement.
If you've tried to do this before but couldn't, it's because there is no way to add
a menu item with standard Delphi calls. You have to trap Windows the windows
message WM_SYSCOMMAND and evaluate the wParam message element to see if your added
menu item was selected. Really folks, it's not that hard, and a little digging in
the API help was all I needed to do find out how to implement this in a program.
Basically, what you have to do is this:
Create a new form.
Override the OnMessage event by assigning a new event handler procedure for the
OnMessage event.
Create a constant that will be used as the ordinal identifier for your menu choice.
In the FormCreate, make your menu choice with the AppendMenu API call.
Here's the code to show you how to do it:
1 unit sysmenu;
2
3 interface
4
5 uses
6 SysUtils, WinTypes, WinProcs, Messages, Classes,
7 Graphics, Controls, Forms, Dialogs, Menus;
8
9 type
10 TForm1 = class(TForm)
11 procedure FormCreate(Sender: TObject);
12 private
13 { Private declarations }
14 public
15 {This declaration is of the type TMessageEvent which
16 is a pointer to a procedure that takes two variable
17 arguments of type TMsg and Boolean, respectively}
18
19 procedure WinMsgHandler(var Msg: TMsg;
20 var Handled: Boolean);
21 end;
22
23 var
24 Form1: TForm1;
25
26 const
27 MyItem = 100; {Here's the menu identifier.
28 It can be any WORD value}
29
30 implementation
31
32 {$R *.DFM}
33
34 procedure TForm1.FormCreate(Sender: TObject);
35 begin
36
37 {First, tell the application that its message
38 handler is different from the default}
39 Application.OnMessage := WinMsgHandler;
40
41 {Add a separator}
42 AppendMenu(GetSystemMenu(Self.Handle, False),
43 MF_SEPARATOR, 0, '');
44
45 {Add your menu choice. Since the Item ID is high,
46 using the MF_BYPOSITION constant will place
47 it last on the system menu}
48 AppendMenu(GetSystemMenu(Self.Handle, False),
49 MF_BYPOSITION, MyItem, 'My Men&u Choice');
50
51 end;
52
53 procedure TForm1.WinMsgHandler(var Msg: TMsg;
54 var Handled: Boolean);
55 begin
56 {if the message is a system one...}
57 if Msg.message = WM_SYSCOMMAND then
58 if Msg.wParam = MyItem then
59 {Put handling code here. I've opted for
60 a ShowMessage for demonstration purposes}
61 ShowMessage('You picked my menu!!!');
62 end;
63
64 end.
As you can see, this is fairly straight-forward. Granted, the tip is not very complicated. However, it does open up many doors to things you can do. In anticipation of some questions you might have later, The AppendMenu command can also be used with minimized apps. For instance, if you minimize your app, the icon represents the application, not your form. Therefore in order to make the system menu with your changes visible when in minimized form you would use Application.Handle instead of Self.Handle to deal with the application's system menu.
|