在delphi中添加一个菜单项到Windows的系统菜单

为什么Windows的系统菜单总是一成不变?这个例子教你如何往系统菜单添加一个菜单项如about或information等。

这个例子将一个菜单项加到系统菜单中去。我们需要两个东西,一个是项名,这可以是如何整数;我们还需要一个程序去收取Windows对确认点击我们创建的菜单项的信息。

Unit OhYeah;

Interface

Uses

SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, Menus;

Type

TForm1 = Class (TForm)

Procedure FormCreate (Sender : TObject);

Private {Private declarations}

Public {Public declarations}

Procedure WinMsg (Var Msg : TMsg; Var Handled : Boolean);

Procedure DoWhatEever;

End;

Var

Form1 : TForm1;

Implementation

{$R *.DFM}

Const

ItemID = 99; // 这个ID number代表你的菜单项,可以是任何值。

Procedure Tform1.WinMsg (Var Msg : TMsg; Var Handled : Boolean);

Begin

If Msg.Message = WM_SYSCOMMAND Then

If Msg.WParam = ItemID Then DoWhatEver;

End;

Procedure TForm1.FormCreate (Sender : TObject);

Begin

Application.OnMessage := WinMsg;

AppendMenu (GetSystemMenu (Form1.Handle, False), MF_SEPARATOR, 0, '');

AppendMenu (GetSystemMenu (Form1.Handle, False), MF_BYPOSITION, ItemID, '&My menu');

AppendMenu (GetSystemMenu (Application.Handle, False), MF_SEPARATOR, 0, '');

AppendMenu (GetSystemMenu (Application.Handle, False), MF_BYPOSITION, ItemID,'&My menu minimized');

End;

Procedure TForm1.DoWhatEver;

Begin

Exit; //你可以添加任何你想加的东西到这里

End;

End.