Mega Code Archive

 
Categories / Delphi / API
 

Adding system menu items to a form

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;