Question: How do I add a system menu item for the "right click" of the application's task bar icon?

Answer: We describes how to add a custom menu item to the system menu of a form, but what if you want to add one to the task bar's right click?

The reason that this doesn't add anything to the task bar's right click menu is because Delphi creates a hidden window. You have to set the system menu for this hidden window to make it work.

Here is an example of how to do that, and then respond to the proper message:
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure OnAppMessage(var Msg: TMsg; var Handled: Boolean);
end;

var
Form1: TForm1;

implementation

const
SC_MyMenuItem = WM_USER + 1;

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
// Assign the application's OnMessage event to my own
// procedure so I can check for the hidden window's WM_SYSCOMMAND
// message.
Application.OnMessage := OnAppMessage;
AppendMenu(GetSystemMenu(Application.Handle, FALSE), MF_SEPARATOR, 0, '');
AppendMenu(GetSystemMenu(Application.Handle, FALSE),
MF_STRING,
SC_MyMenuItem,
'My Menu Item');
end;

procedure TForm1.OnAppMessage(var Msg: TMsg; var Handled: Boolean);
begin
// Check to see if the message is a system command
// and the message's wParam is SC_MyMenuItem
if (Msg.message = WM_SYSCOMMAND) and (Msg.wParam = SC_MyMenuItem) then
begin
ShowMessage('Got the message');
Handled := True;
end;
end;

4 Responses so far.

Post a Comment

Thank you for your comment.

Any request and idea are welcome.

CLICK TO REGISTER