1 unit Unit001;
2
3 interface
4
5 uses
6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
8
9 type
10 TForm1 = class(TForm)
11 procedure FormCreate(Sender:TObject);
12 procedure sysmenu(var msg:twmmenuselect);message wm_syscommand;
13 private
14 { Private declarations }
15 public
16 { Public declarations }
17 end;
18
19 var
20 Form1: TForm1;
21
22 implementation
23
24 {$R *.dfm}
25 procedure TForm1.FormCreate(Sender:TObject);
26 var
27 i:integer;
28 begin
29 i:= getsystemmenu(handle,false);
30 appendmenu(i,mf_separator,0,nil); //添加分割线
31 appendmenu(i,mf_string,100,‘我的菜单(&E)‘); //添加菜单项
32 end;
33 procedure TForm1.sysmenu(var msg:twmmenuselect);
34 begin
35 if msg.IDItem = 100 then
36 ShowMessage(‘您选择了自己添加的菜单!‘)
37 else
38 inherited;
39 end;
40 end.
API函数
function GetSystemMenu(hWnd:HWND;bRevert:BOOL):HMENU;stdcall;
参数说明:
hWnd: 所要取得系统菜单句柄的目标窗口句柄。
bRevert:是否修改原始菜单。
返回一个HMENU型的菜单句柄。
通过GetSystemMenu得到句柄后可以使用AppendMenu函数为系统菜单增加一个菜单项,该函数原型为:
function
AppendMenu(hMenu:HMENU;uFlags,uIDNewItem:UNIT;lpNewItem:Pchar):BOOL;stdcall;
参数说明:
hMenu: 用GetSystemMenu函数得到的菜单句柄。
uFlag,uIDNewItem:菜单唯一标志,弹出菜单唯一标志。
lpNewItem:菜单的类型。
容易出现问题:
缺少代码 message wm_syscommand;这句代码的主要作用是:A window receives this message when the user
chooses a commond from the window menu.而如果注释了inherited ,则所有系统菜单都不能用。
OnCreate关联
当窗体初始化创建时,就会发生OnCreate事件,对特定窗体的任何实例,OnCreate事件只发生一次,OnCreate处理程序只是用来执行任何启动任务,这些启动任务对于窗体来说是需要的。
Delphi 在系统菜单中添加菜单项,布布扣,bubuko.com
时间: 2024-08-08 01:29:40