Delphi动态添加控件

复制代码
{动态添加导航}
var
Panl:Tpanel;
MainPage,Subpage:TPageControl;
TabSheet1: TTabSheet;
ToolBar2: TToolBar;
Splitter:TSplitter;
begin
{创建导航树开始}
 Panl:=Tpanel.Create(self);
 with Panl  do
 begin
    Parent := mainForm;
    Left:= 0 ;
    Top:= 30  ;
    Width:= 185 ;
    Height:= 381 ;
    Align:= alLeft;
    Caption:= ‘Panel1‘;
    TabOrder:= 2;
  end;
  {创建导航树结束}

        MainPage:=TPageControl.Create(self);
        with MainPage do
        begin
        Parent := Panl;
            Left := 0;
            Top := 0;
            Width := 235;
            Height := 410;
            ActivePage := TabSheet1;
            Align := alClient;
            BiDiMode := bdLeftToRight;
            Font.Charset := DEFAULT_CHARSET;
            Font.Color := clWindowText;
            Font.Height := -13;
            Font.Name := ‘MS Sans Serif‘;
            Font.Style := [];
            MultiLine := True;
            ParentBiDiMode := False;
            ParentFont := False;
            TabOrder := 0;
            TabPosition := tpLeft;
         end;
         TabSheet1:=TTabSheet.Create(self);
         TabSheet1.Parent := MainPage;
         TabSheet1.Caption := ‘树形导航‘;

         ToolBar2:=TToolBar.Create(self);

        with ToolBar2 do
        begin
        Parent :=TabSheet1;
                Left := 0;
                Top := 0;
                Width := 207;
                Height := 30;
                ButtonHeight := 28;
                ButtonWidth := 29;
                Caption := ‘ToolBar2‘;
                EdgeInner := esNone;
                EdgeOuter := esNone;
                Flat := True;
                Images := ImageList1;
                TabOrder := 0;
                TabStop := True;
                Wrapable := False;
        end;

  {添加分割条开始}
  Splitter:=TSplitter.Create(self);
  with Splitter  do
  begin
    Parent := mainForm;
    Left := 185;
    Top := 30;
    Height:= 381;
  end;
  {添加分割条结束}
end;
复制代码

delphi 动态创建菜单

复制代码
delphi 动态创建菜单

procedure TForm1.Button1Click(Sender: TObject);
var
MainMenu:TMainMenu;
MenuItem:TMenuItem;
begin
//创建主菜单对象,并设置主窗口的菜单为MainMenu对象
MainMenu:=TMainMenu.Create(Self);
Self.Menu:=MainMenu;

//创建MainMenu主菜单的第一级菜单对象 MainMenu.Items.Add
MenuItem:=TMenuItem.Create(MainMenu);
MenuItem.Caption:=‘一级菜单‘;
MainMenu.Items.Add(MenuItem);

//创建MainMenu主菜单的第二级子菜单对象 MainMenu.Items[0].Add
MenuItem:=TMenuItem.Create(MainMenu);
MenuItem.Caption:=‘二级菜单‘;
MainMenu.Items[0].Add(MenuItem);

//创建MainMenu主菜单的第三级子菜单对象 MainMenu.Items[0].Items[0].Add
MenuItem:=TMenuItem.Create(MainMenu);
MenuItem.Caption:=‘三级菜单‘;
MainMenu.Items[0].Items[0].Add(MenuItem);

// 用Items[0]...这种形式,可以创建 四级、五级直到N级菜单都可以

MenuItem:=TMenuItem.Create(MainMenu);
MenuItem.Caption:=‘四级菜单‘;
MainMenu.Items[0].Items[0].Items[0].Add(MenuItem);

end;
复制代码

 

posted on   癫狂编程  阅读(558)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
历史上的今天:
2017-11-07 学习 TTreeView [1] - TTreeNodes、TTreeNode 与 Items、Items.Count、Items.Clear
2017-11-07 学习 TTreeView [3] - Add、AddChild、AddFirst、AddChildFirst、Parent
2017-11-07 学习官方示例 - TForm.BorderIcons
2017-11-07 删除或添加最大化、最小化按钮 - 回复 "Tommy the CAT" 的问题

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
好的代码像粥一样,都是用时间熬出来的
点击右上角即可分享
微信分享提示