本例效果图:
代码文件:
代码文件:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; Button5: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button4Click(Sender: TObject); procedure Button5Click(Sender: TObject); procedure FormCreate(Sender: TObject); protected procedure WMSysCommand(var Message: TWMSysCommand); message WM_SYSCOMMAND; end; var Form1: TForm1; implementation {$R *.dfm} {定义两个菜单命令 ID 常数, 自定义的此类常数应该小于 $F000, 因为系统使用的都大于这个数} const MenuCmdID1 = 101; MenuCmdID2 = 102; {添加} procedure TForm1.Button1Click(Sender: TObject); var h: HMENU; begin {系统菜单句柄} h := GetSystemMenu(Handle, False); AppendMenu(h, MFT_STRING, MenuCmdID1, '新添加的菜单项'); end; {插入} procedure TForm1.Button2Click(Sender: TObject); var h: HMENU; begin h := GetSystemMenu(Handle, False); {这是插入在 "还原" 菜单(SC_RESTORE) 前面} InsertMenu(h, SC_RESTORE, MFT_STRING, MenuCmdID2, '插入的菜单项'); end; {修改} procedure TForm1.Button3Click(Sender: TObject); var h: HMENU; begin h := GetSystemMenu(Handle, False); ModifyMenu(h, SC_MOVE, MF_BYCOMMAND, SC_MOVE, '这是我改的'); end; {删除} procedure TForm1.Button4Click(Sender: TObject); var h: HMENU; begin h := GetSystemMenu(Handle, False); {这是删除的 "关闭" 菜单, 此时窗口的关闭按钮会变灰} DeleteMenu(h, SC_CLOSE, MF_BYCOMMAND); end; {恢复} procedure TForm1.Button5Click(Sender: TObject); begin GetSystemMenu(Handle, True); end; procedure TForm1.FormCreate(Sender: TObject); begin Button1.Caption := '添加'; Button2.Caption := '插入'; Button3.Caption := '修改'; Button4.Caption := '删除'; Button5.Caption := '恢复'; end; procedure TForm1.WMSysCommand(var Message: TWMSysCommand); var h: HMENU; buf: array[Byte] of Char; begin h := GetSystemMenu(Handle, False); case Message.CmdType of MenuCmdID1: begin GetMenuString(h, MenuCmdID1, buf, Length(buf), MF_BYCOMMAND); ShowMessage(buf); end; MenuCmdID2: begin GetMenuString(h, MenuCmdID2, buf, Length(buf), MF_BYCOMMAND); ShowMessage(buf); end; end; inherited; end; end.窗体文件:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 191 ClientWidth = 206 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 48 Top = 20 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 0 OnClick = Button1Click end object Button2: TButton Left = 48 Top = 51 Width = 75 Height = 25 Caption = 'Button2' TabOrder = 1 OnClick = Button2Click end object Button3: TButton Left = 48 Top = 82 Width = 75 Height = 25 Caption = 'Button3' TabOrder = 2 OnClick = Button3Click end object Button4: TButton Left = 48 Top = 113 Width = 75 Height = 25 Caption = 'Button4' TabOrder = 3 OnClick = Button4Click end object Button5: TButton Left = 48 Top = 144 Width = 75 Height = 25 Caption = 'Button5' TabOrder = 4 OnClick = Button5Click end end