Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Customizing the System Menu

By Zarko Gajic, About.com

Custom System Menu

The BorderIcons property of a Delphi Form is used to determine if a form shows the standard Windows system (control) menu (the one in the title bar of a form) and maximize and minimize buttons.

If you want to remove the maximize button from a form (and therefore disable maximizing of a form) we have to set BorderIcons property biMaximize value to False.

What if you want to add a custom item to the system menu of an application, or delete/change the default Close caption, or even add some bitmap to it? This article shows how to handle (add, delete, change, add bitmap,...) your own system menu items and how and where to process them, by using Windows API and Delphi.

API calls and Messages

In order to get the system menu we will use the GetSystemMenu API function. Further more, in the following few examples we will use AppendMenu, SetMenuItemInfo along with the Delphi record structure TMenuItemInfo. After adding the menu item, we will trap the WM_SYSCOMMAND message to test if our new menu item was selected.

Add Menu Item

The following code demonstrates using the AppendMenu function to append a new menu item to the system menu. First, we have define a new system command constant SC_MyMenuItem1 that will be used to identify our new item to the system.
...
const
  SC_MyMenuItem1 = WM_USER + 1;
...
procedure TForm1.btnAddClick(Sender: TObject) ;
const
  sMyMenuCaption1 = 'My new system menu item';
var
  SysMenu : HMenu;
begin
  {Get system menu}
  SysMenu := GetSystemMenu(Handle, FALSE) ;
  {Add a seperator bar to main form-form1}
  AppendMenu(SysMenu, MF_SEPARATOR, 0, '') ;
  {add our menu}
  AppendMenu(SysMenu, MF_STRING, SC_MyMenuItem1, sMyMenuCaption1) ;
end;
After adding the menu item, have to trap Windows for the WM_SYSCOMMAND message to test if our added new menu item was selected.
...
private
  procedure WMSysCommand(var Msg: TWMSysCommand) ; message WM_SYSCOMMAND;
...
procedure TForm1.WMSysCommand(var Msg : TWMSysCommand) ;
begin
  if Msg.CmdType = SC_MyMenuItem1 then
   {some code to handle my menu item}
   ShowMessage('My system menu 1 clicked!') ;
  else
   inherited;
end;
Note: When you minimize your application, the icon represents the application, not your (main) form. Therefore in order to make the system menu with your changes visible when in minimized form you would use Application.Handle instead of just Handle to deal with the application's system menu.

To fully customize the menu that appears when the Taskbar button is clicked and still operate on the TMenuItem objects you might need a custom menu component: TTaskBarMenu.

Delete Sytem Menu Item

By deleting a SystemMenu menu item we also remove its option to be executed from the system menu of the form. The DeleteMenu function used here deletes the Move item from the system menu. Notice that after removing the Move option our form cannot be moved any more, this is the dirtiest way to keep the window from moving around.
procedure TForm1.btnDeleteClick(Sender: TObject) ;
const
  MnuCommand = SC_MOVE;
var
  SysMenu : HMenu;
begin
  SysMenu := GetSystemMenu(Handle, FALSE) ;

  DeleteMenu(SysMenu, MnuCommand, MF_BYCOMMAND) ;
end;

Change Item's Text (Caption)

To change the menu item caption we have to make a call to SetMenuItemInfo sending in our TMenuItemInfo structure which contains information about a menu item.
procedure TForm1.btnChangeClick(Sender: TObject) ;
const
  strCLOSE = 'Close This Window';
var
  SysMenu : HMenu;
  MenuItemInfo : TMenuItemInfo;
begin
  SysMenu := GetSystemMenu(Handle, FALSE) ;

  FillChar(MenuItemInfo,SizeOf(TMenuItemInfo), #0) ;
  MenuItemInfo.cbSize := SizeOf(TMenuItemInfo) ;
  MenuItemInfo.fMask := MIIM_TYPE or MIIM_ID or MIIM_STATE;
  MenuItemInfo.fType := MFT_STRING;

  MenuItemInfo.wId := SC_CLOSE;
  MenuItemInfo.dwTypeData := strCLOSE;
  MenuItemInfo.cch := Length(strCLOSE) ;

  SetMenuItemInfo(SysMenu, SC_CLOSE, FALSE, MenuItemInfo) ;
end;
The accompanying sample project also contains the code to add the custom bitmap to the system menu item.

Explore Delphi Programming

About.com Special Features

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Advanced Delphi Techniques
  5. Customizing the System Menu for a Delphi application

©2009 About.com, a part of The New York Times Company.

All rights reserved.