Own items in application system menu

When you rightclick application title bar (or leftclick icon on title bar), the system menu will appear. Here are common commands to close, minimize or maximize window. But you can add your own commands if you like. Here is how.

Following example shows how to add “Info” item to the system menu. All you need is to write few lines to OnCreate event of main form and write a special WMSysCommand procedure.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

const Info = 100;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    hSysMenu: hmenu;
    procedure WMSysCommand(var Message: TMessage); message WM_SYSCOMMAND;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WMSysCommand(var Message : TMessage);
begin
  if Message.WParam = Info then ShowMessage('Info');
  inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  hSysMenu := GetSystemMenu(Form1.Handle, False);
  AppendMenu(hSysMenu, MF_SEPARATOR, 0, '');
  AppendMenu(hSysMenu, MF_STRING, Info, '&Info');
end;

end.

Using this method, you can add as many items as you want.

Leave a Reply