Board index » delphi » How to add items to system menu ?

How to add items to system menu ?

I'm happy to get an opportunity to ask my question on Delphi.

My question is that :
 1. How to add menu items to system menu which appears when we click
    the left-up located small icon.
 2. How to handle the menu item added to system menu.

Any remark would be appreciated.
Thanks in advance.

 

Re:How to add items to system menu ?


{ FILE:        SysMenu1.PAS       VERSION:     1.0.1
  AUTHOR:      Duncan McNiven     Tel:  +358 21 81 41 309
               L.E.C.S.  Ltd      Fax:  +358 21 43 56 489
                                  E-mAIL: duncan.mcni...@lecs.inet.fi
  LANGUAGE:    Delphi v.1.0 running under Dos 6.0, Windows 3.1

  PURPOSE:     Demo program.  Shows how to add items to system menu
               of form and application.  The system menus are
               different because the Application effectively puts a
               wrapper around the forms.

  CAVEATS:     The IDs for new menu items must be multiples of 16 (or
               in hex, have the form $XXX0).  This is because Windows
               uses the lower 4 bits of WParam in the WM_SYSCOMMAND
               message for internal purposes, so you have to mask them
               out if you trap this message and check WParam for your
ID.

               To check if a menu ID already exists, use
               GetMenuState( hMenu, ItemID, MY_BYCOMMAND);
               This returns -1 if an item with the passed ID does NOT
               already exist.                                     }

unit Sysmenu1;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    Procedure AppMessage( var msg:TMsg; var Handled : Boolean);
  public
  end;

var
  Form1: TForm1;

implementation

{ Add an item to the system menu and the form menu. }
procedure TForm1.FormCreate(Sender: TObject);
var
  FormSysMenu, AppSysMenu : THandle;
begin
  AppSysMenu := GetSystemMenu(Application.Handle,False);
  AppendMenu(AppSysMenu,mf_Separator,0,'');
  AppendMenu(AppSysMenu,mf_Enabled,$E010,'App Menu Item');

  FormSysMenu := GetSystemMenu(Form1.Handle,False);
  AppendMenu(FormSysMenu,mf_Separator,0,'');
  AppendMenu(FormSysMenu,mf_Enabled,$E020,'Form Menu Item');

  Application.OnMessage := AppMessage;
end;

{ Intercept Windows messages & find those from our new menu items }
Procedure TForm1.AppMessage( var msg:TMsg; var Handled : Boolean);
begin
  if msg.WParam = $E010 then begin
     Handled := True;
     MessageBox(Handle,'From Application Menu','Menu Result',mb_OK);
     end;

  if msg.WParam = $E020 then begin
     Handled := True;
     MessageBox(Handle,'From Form Menu','Menu Result',mb_OK);
     end;
end;

end.

Baek Chang Heum <chb...@maru.hit.co.kr> wrote:

Quote
>I'm happy to get an opportunity to ask my question on Delphi.
>My question is that :
> 1. How to add menu items to system menu which appears when we click
>    the left-up located small icon.
> 2. How to handle the menu item added to system menu.
>Any remark would be appreciated.
>Thanks in advance.

Re:How to add items to system menu ?


Here is some code that should help you.

Regards
Tim Hyder

unit Unit1;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, Menus;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
     procedure winmsg(var msg:tmsg;var handled:boolean);
     {This is what handles the messages}

     procedure DOWHATEVER;{procedure to do whatever}
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
const ItemID=99;{the ID number for your menu item--can be anything}

procedure tform1.winmsg(var msg:tmsg;var handled:boolean);
begin
  if msg.message=wm_syscommand then{if the message is a system one...}
   if msg.wparam = ItemID then DOWHATEVER;{then check if its parameter
                                           is your Menu items ID,}
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  application.onmessage:=winmsg;
  {tell your app that 'winmsg' is the application message handler}

  AppendMenu(GetSystemMenu(form1.handle,false),mf_separator,0,'');
  {Add a seperator bar to form1}

AppendMenu(GetSystemMenu(form1.handle,false),mf_byposition,ItemID,
  '&New Item');
{add your menu item to form1}

AppendMenu(GetSystemMenu(application.handle,false),mf_separator,0,'');
{Add a seperator bar to the application system menu(used when app
 is minimized)}

AppendMenu(GetSystemMenu(application.handle,false),mf_byposition,
ItemID,'&New Item'
{add your menu itemto the application system menu(used when app is
 minimized)}

{for more information on the AppendMenu and GetSystemMenu see online
 help}

end;
procdure TForm1.DOWHATEVER;
begin
 {add whatever you want to this procedure}
end;

end.

Other Threads