Главная страница | назад





Article #17064: adding system menu items to a form

 Question and Answer Database

FAQ2064D.txt   adding system menu items to a form
Category   :Windows API
Platform    :All
Product    :All 32 bit  

Question:
How do I add a custom item to the system menu of my application?

Answer:
The following example demonstrates using the Windows API 
function AppendMenu() to append a new menu item to the system 
menu. We will define a new system command 
constant : SC_MyMenuItem that will be used to identify our 
new item to the system. After adding the menu item, we will 
trap the WM_SYSCOMMAND message to test if our new menu item was 
selected.

Example:

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

var
  Form1: TForm1;

implementation

{$R *.DFM}

const
  SC_MyMenuItem = WM_USER + 1;

procedure TForm1.FormCreate(Sender: TObject);
begin
  AppendMenu(GetSystemMenu(Handle, FALSE), MF_SEPARATOR, 0, '');
  AppendMenu(GetSystemMenu(Handle, FALSE),
             MF_STRING,
             SC_MyMenuItem,
             'My Menu Item');
end;

procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
begin
  if Msg.CmdType = SC_MyMenuItem then
    ShowMessage('Got the message') else
    inherited;
end;

7/16/98 4:31:28 PM
 

Last Modified: 01-SEP-99