var AddCollctMenus: Array Of TMenuItem; //动态菜单
procedure TForm1.Button5Click(Sender: TObject); Var i, AddCollctMenuCount: Integer; Begin AddCollctMenuCount := Length(AddCollctMenus) - 1; For i := 0 To AddCollctMenuCount Do Begin FreeAndNil(AddCollctMenus[i]); End; With SQLQuery1 Do Begin CLOSE; SQL.Clear; SQL.Add('select id,username,password from userinfo'); OPEN; SetLength(AddCollctMenus, SQLQuery1.RecordCount); First; i := 0; While Not Eof Do Begin AddCollctMenus[i] := TMenuItem.Create(Self); With AddCollctMenus[i] Do Begin OnClick := MenuItem2.OnClick; //AutoHotkeys := maAutomatic; Tag:= SQLQuery1.FieldByName('id').AsInteger; // Hint := SQLQuery1.FieldByName('password').AsString; // 有三个变量可以传递 到 菜单。 Caption := SQLQuery1.FieldByName('username').AsString; // End; CollPopMenu.Add(AddCollctMenus[i]); Inc(i); NEXT; End; End; End;
procedure TForm1.MenuItem2Click(Sender: TObject); begin // 有三个变量可以传递 到 菜单 showmessage((Sender As TMenuItem).Hint); showmessage((Sender As TMenuItem).Caption); showmessage((Sender As TMenuItem).Tag.ToString); end;