在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
在delphi中动态创建菜单的Demo。 实现功能: 1) 在Form的Oncreate事件中,动态创建子菜单。 代码如下: unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, Vcl.StdCtrls; type TForm1 = class(TForm) mm1: TMainMenu; File1: TMenuItem; New1: TMenuItem; Open1: TMenuItem; Save1: TMenuItem; SaveAs1: TMenuItem; Print1: TMenuItem; PrintSetup1: TMenuItem; Exit1: TMenuItem; N1: TMenuItem; N2: TMenuItem; MenuItemSize: TMenuItem; lbl1: TLabel; procedure FormCreate(Sender: TObject); procedure CheckFontSize(MenuItem:TmenuItem); procedure MenuItemFontSizeClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} const FontSize : array[0..7] of Integer = (6,8,10,12,14,16,18,20); //这里,在Create时动态创建了8个子菜单 //同时,指定了OnClick事件 //并检测窗体内的一个label的字体大小 procedure TForm1.FormCreate(Sender: TObject); var i:integer; menuItem : TMenuItem; begin for i := low(FontSize) to high(FontSize) do begin menuItem := TMenuItem.Create(Self); menuItem.Caption := IntToStr(FontSize[i]); menuItem.Tag := FontSize[i]; menuItem.GroupIndex := 9; menuItem.RadioItem := True; menuItem.OnClick := MenuItemFontSizeClick; MenuItemSize.Insert(MenuItemSize.Count,menuItem); end; CheckFontSize(MenuItemSize); end; //动态创建的菜单项的单击事件 //设置字体大小,同时改变选中状态 procedure TForm1.MenuItemFontSizeClick(Sender: TObject); begin lbl1.Font.Size := (sender as TMenuItem).Tag; //寻找被按下菜单的Tag,然后赋值给字体大小 (sender as TMenuItem).Checked := true; //选中 end; //检测窗体内的一个label的字体大小 procedure TForm1.CheckFontSize(MenuItem:TmenuItem); var i:Integer; begin ShowMessage(IntToStr(lbl1.Font.Size)); for i := 0 to 7 do begin if lbl1.Font.Size = FontSize[i] then begin MenuItem.Items[i].Checked := true; Break; end; end; end; end.
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论