Delphi add options to the form's system menu

Source: Internet
Author: User
Tags exit integer response code

Typically in an application, if you click the icon to the left of the title bar, a form's system menu pops up, where you can minimize, maximize, and close the operation. This example shows how to add your own options to the form's system menu.

Add a Tmainmenu component to the form where the options in the Tmainmenu component will be added to the form's system menu. The form after you add the component is shown in Figure 1.

Figure 1 form after adding a component

Add a File menu to the menu generated by the Tmainmenu component, and add an exit option below the menu.

The menu generated by the Tmainmenu component is added to the form's system menu when the program starts running, and the response code is as follows:

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  AppendMenu (GetSystemMenu (Handle, FALSE), MF_SEPARATOR, 0, '');
  with MainMenu1 do
  begin
   for I := 0 to Items.Count - 1 do
    AppendMenu(GetSystemMenu(self.Handle,FALSE),mf_Popup,
    Items[I].Handle,PAnsiChar(Items[I].Caption));
  end;
end;

The program first obtains the system menu of the form through GetSystemMenu (Handle, FALSE) and calls the AppendMenu function to add an option of mf_separator type, which is the menu separator bar. Then through a loop, call the AppendMenu function to add the menu generated by the Tmainmenu component to the form's system menu individually.

Now, although the options have been added to the form's system menu, the options are not yet responsive to the user's actions. In order to be able to respond to the user's actions normally, you need to intercept the WM_SYSCOMMAND message, so the process to add the blocking message--procedure Wmsyscommand (var msg:twmsyscommand); Syscommand, its response code is as follows:

procedure TForm1.WMSysCommand (var Msg: TWMSysCommand);
var
  Item: TMenuItem;
begin
  Item := MainMenu1.FindItem(Msg.CmdType, fkCommand);
  if Item <> nil then
   Item.Click;
   inherited;
end;

This way, the newly added options will work correctly.

Click the exit option in the form system menu file to end the program running.

The program code is as follows:

Unit Unit1;
Interface
Uses
Sysutils, Windows, Messages, Classes, Graphics, Controls, Forms, Dialogs,
Menus, Stdctrls;
Type
TForm1 = Class (Tform)
Mainmenu1:tmainmenu;
File1:tmenuitem;
Exit1:tmenuitem;
Procedure Exit1click (Sender:tobject);
Procedure Formcreate (Sender:tobject);
Private
{Private declarations}
Public
Procedure Wmsyscommand (var msg:twmsyscommand); message wm_syscommand;
End
Var
Form1:tform1;
Implementation
{$R *. DFM}
Procedure Tform1.wmsyscommand (var msg:twmsyscommand);
Var
Item:tmenuitem;
Begin
Item: = Mainmenu1.finditem (Msg.cmdtype, Fkcommand);
If Item <> Nil Then
Item.click;
inherited;
End
Procedure Tform1.exit1click (Sender:tobject);
Begin
Close;
End
Procedure Tform1.formcreate (Sender:tobject);
Var
I:integer;
Begin
AppendMenu (GetSystemMenu (Handle, FALSE), Mf_separator, 0, "");
With MainMenu1 do
Begin
For I: = 0 to Items.count-1 do
AppendMenu (GetSystemMenu) (self. Handle,false), Mf_popup,
Items[i]. Handle,pansichar (Items[i]. Caption));
End
End
End.

Save the file, and then press F9 to run the program, and the results of the program run as shown in Figure 2.

Figure 2 Program Run results

This example shows how to add an option to the system menu of a form. After you get a handle to the system options, you can add options and modify the options so that the reader can more fully control the system menu of the form in the program that you write.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.