• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

(32)uniGUI for Delphi下如何动态创建控件

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 (中行雷威2019.5.30)

(同一个世界,同一个梦想,交流学习C++Builder and Delphi XE10,传承c++builder and Delphi的魅力!欢迎各地朋友加入我的QQ群484979943、860634510、299497712,进群密码“BCB”,同时也请将该群号广为宣传,希望能够广集各方高手,共同进步。如需下载开发工具及源代码请加入我的QQ群。)

【阅读倡议】

1、有问题请留言;

2、没问题请点赞;

3、看连载请加群;

4、下源码请加群;

【开发工具】

1、C++Builder and Delphi 10.3.1

2、FMSoft_uniGUI_Complete_Professional_1.70.0.1501(正版)

本人主笔的国内第一本uniGUI教学案例代码已诞生,分为cbuilder和delphi两个版本,买代码送手册,需要的朋友可以加入我的QQ技术交流群484979943给我(群主)留言。资料简介:https://blog.csdn.net/dlboy2018/article/details/88923832

7.9如何动态创建控件

         本例子主要介绍如何动态创建一些常用uniGUI控件,并且为一些控件添加一些简单的事件,介绍一下如何为调用动态创建的控件的函数或事件。

7.9.1界面设计

         在MainForm上摆放两个UniContainerPanel和一个UniMemo控件,UniContainerPanel1控件的Align设为alLeft,UniMemo控件的Name设为UniMemoLog、Align设为alRight,将UniContainerPanel2的Align设为alClient。在UniContainerPanel1上面摆放八个UniButton控件,分别将其Name设为createButton、createEdit、createComboBox、createMemo、createPanel、createRadioGroup、createStringGrid、createTreeView,将他们的Align都设为alTop。在UniContainerPanel1上放一个UniCheckBox并将其Name设为cbDragFlag,将其Caption设为“允许拖拽”。如下图所示,左侧八个按钮分别用来动态创建不同的控件,勾选上“允许拖拽”后新创建的控件可以用鼠标拖拽,拖拽后将在右侧的UniMemoLog控件里显示控件的新坐标位置,新创建的控件将在UniContainerPanel2里面出现,鼠标点哪里新创建的控件将出现在哪里。

7.9.2功能实现

1、Main.pas引用动态创建控件的头文件

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics,

  Controls, Forms, uniGUITypes, uniGUIAbstractClasses,

  uniGUIClasses, uniGUIRegClasses, uniGUIForm, uniCheckBox,uniGUIBaseClasses,

  uniButton, uniMemo, uniPanel,uniEdit,uniComboBox,uniRadioGroup,uniStringGrid,uniTreeView;  //引用控件的头文件

 

2、Main.pas定义一些变量和函数

type

  TMainForm = class(TUniForm)

   …

  private

    { Private declarations }

    B1,E1,P1,C1,M1,R1,S1,T1: Integer;//定义各个控件的序号变量

    LastX, LastY : Integer;//定义鼠标点击最后位置变量

    procedure AnyEndDrag(Sender: TUniControl; Left, Top: Integer);//定义拖拽结束函数

    procedure ShowHello(Sender: TObject);//定义按钮点击函数

 

  public

    { Public declarations }

  end;

 

3、Main.pas主要功能

         点击UniContainerPanel2的任何位置,记录下鼠标点击坐标,点击左侧的控件创建按钮时将在坐标位置创建一个新控件,拖动控件时让右侧记录下控件的新位置。

 

1)记录鼠标的点击位置

procedure TMainForm.UniContainerPanel2MouseDown(Sender: TObject;

  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

//记录鼠标当前点击位置

begin

    LastX := X;

    LastY := Y;

end;

 

2)记录控件拖动后的新位置

         AnyEndDrag是一个公共函数,拖动任何一个新创建的控件,都将记录下控件的新位置,让新创建控件的OnEndDrag=AnyEndDrag即可。

procedure TMainForm.AnyEndDrag(Sender: TUniControl; Left, Top: Integer);

begin

  UniMemoLog.Lines.Add(Sender.Name+'''的X位置: '+IntToStr(Left)+' Y位置: '+IntToStr(Top));

  UniMemoLog.Lines.Add('--------------------------------------------------');

end;

 

3)控制新控件是否可以拖动

procedure TMainForm.cbDragFlagClick(Sender: TObject);

var

  I : Integer;

  uControl : TUniControl;

begin

if cbDragFlag.Checked then //可拖放

  begin

    for I := 0 to UniContainerPanel2.ControlCount - 1  do

      if UniContainerPanel2.Controls[I] is TUniControl then

      begin

        uControl := UniContainerPanel2.Controls[I] as TUniControl;

        uControl.Draggable.Enabled := True;

        uControl.Cursor := crDrag

      end;

  end

else                       //不可拖放

  begin

    for I := 0 to UniContainerPanel2.ControlCount - 1  do

      if UniContainerPanel2.Controls[I] is TUniControl then

      begin

        uControl := UniContainerPanel2.Controls[I] as TUniControl;

        uControl.Draggable.Enabled := False;

        uControl.Cursor := crDefault;

      end;

  end;

end;

 

4)动态创建UniButton按钮

ShowHello是一个自定义的功能函数,如果想让按钮的点击事件调用ShowHello函数,设置按钮的OnClick=ShowHello即可。

procedure TMainForm.ShowHello(Sender: TObject);

//动态创建的按钮的点击事件

begin

ShowMessageN('HelloWorld');

end;

 

procedure TMainForm.createButtonClick(Sender: TObject);

//动态创建UniButton控件

begin

  B1:=B1+1;

  with TUniButton.Create(Self) as TUniButton do

  begin

    Left := LastX;

    Top := LastY;

    Parent := UniContainerPanel2;

    Caption := 'Button'+IntToStr(B1);

    Name := Caption;

    Draggable.Enabled := True;

    Cursor := crDrag;

    OnEndDrag := AnyEndDrag;//调用拖放结束事件

    OnClick :=ShowHello;//调用按钮点击事件

  end;

end;

 

5)动态创建UniEdit控件

procedure TMainForm.createEditClick(Sender: TObject);

begin

  E1 := E1 +1;

  with TUniEdit.Create(Self) as TUniEdit do

  begin

    Left := LastX;

    Top := LastY;

    Parent := UniContainerPanel2;

    Text := 'Edit'+IntToStr(E1);

    Name := Text;

    Draggable.Enabled := True;

    Cursor := crDrag;

    OnEndDrag := AnyEndDrag;

 

    Width :=200;//控件总长度

    FieldLabel :='姓名';//输入框标签

    FieldLabelWidth:=30;//标签长度

    Text:='';//默认内容置为空

    EmptyText:='请输入姓名';//输入框为空时的提示信息

  end;

end;

 

6)动态创建UniComboBox控件

procedure TMainForm.createComboBoxClick(Sender: TObject);

begin

  C1 := C1+1;

  with TUniComboBox.Create(Self) as TUniComboBox do

  begin

    Left := LastX;

    Top := LastY;

    Parent := UniContainerPanel2;

    Text := 'ComboBox'+IntToStr(C1);

    Name := Text;

    Draggable.Enabled := True;

    Cursor := crDrag;

    OnEndDrag := AnyEndDrag;

 

    Items.Add('大连理工大学');//添加下拉选项

    Items.Add('北京理工大学');

    Items.Add('武汉理工大学');

    ItemIndex:=0;//制定默认显示第1项

  end;

end;

 

7)动态创建UniMemo控件

procedure TMainForm.createMemoClick(Sender: TObject);

var

  i:Integer;

begin

  M1 := M1+1;

  with TUniMemo.Create(Self) as TUniMemo do

  begin

    Left := LastX;

    Top := LastY;

    Parent := UniContainerPanel2;

    Lines.Add('Memo'+IntToStr(M1));

    Name := 'Memo'+IntToStr(M1);

    Draggable.Enabled := True;

    Cursor := crDrag;

    OnEndDrag := AnyEndDrag;

 

//    ScrollBars := ssBoth; //设定拖动条(有BUG)

    Font.Size:=12;

    Font.Color:=clRed;

    WordWrap:=false;//不自动换行

    for i:=0 to 20 do

      begin

        Lines.Add('我自来大连理工大学,我的学号是:'+IntToStr(i));

      end;

  end;

end;

 

8)动态创建UniPanel控件

procedure TMainForm.createPanelClick(Sender: TObject);

begin

  P1 := P1+1;

  with TUniPanel.Create(Self) as TUniPanel do

  begin

    Left := LastX;

    Top := LastY;

    Parent := UniContainerPanel2;

    TitleVisible := True;

    Title := 'Panel'+IntToStr(P1);

    Name := Title;

    Draggable.Enabled := True;

    Cursor := crDrag;

    OnEndDrag := AnyEndDrag;

 

    Collapsible:=true; //可收起

    Collapsed:=false;  //状态:未收起

    CollapseDirection:=cdTop;//收起方向

  end;

end;

 

9)动态创建UniRadioGroup控件

procedure TMainForm.createRadioGroupClick(Sender: TObject);

begin

  R1 := R1+1;

  with TUniRadioGroup.Create(Self) as TUniRadioGroup do

  begin

    Left := LastX;

    Top := LastY;

    Parent := UniContainerPanel2;

    Items.Add('香蕉');

    Items.Add('西瓜');

    Items.Add('苹果');

    ItemIndex := 0;

    Width := 150;

    Height := 80;

    Caption := 'RadioGroup'+IntToStr(R1);

    Name := Caption;

    Draggable.Enabled := True;

    Cursor := crDrag;

    OnEndDrag := AnyEndDrag;

  end;

end;

 

10)动态创建UniStringGrid控件

procedure TMainForm.createStringGridClick(Sender: TObject);

var

  i,j:Integer;

begin

  S1 := S1+1;

  with TUniStringGrid.Create(Self) as TUniStringGrid do

  begin

    Left := LastX;

    Top := LastY;

    Parent := UniContainerPanel2;

    HeaderTitle := 'StringGrid'+IntToStr(S1);

    Height := 150;

    Name := HeaderTitle;

    Draggable.Enabled := True;

    Cursor := crDrag;

    OnEndDrag := AnyEndDrag;

 

    ShowColumnTitles:=true;//显示列标题

    StripeRows:=true;//条带化

    RowCount:=5;

    ColCount:=3;

    for i:=0 to RowCount do

      begin

        for j:=0 to ColCount do

          begin

            Cells[j,i]:=IntToStr(i*j);

          end;

      end;

  end;

end;

 

11)动态创建UniTreeView控件

procedure TMainForm.createTreeViewClick(Sender: TObject);

var

  LDestNode: TUniTreeNode;

begin

  T1 := T1+1;

  with TUniTreeView.Create(Self) as TUniTreeView do

  begin

    Left := LastX;

    Top := LastY;

    Width :=200;

    Height :=250;

    Parent := UniContainerPanel2;

    Title := 'UniTreeView'+IntToStr(T1);

    Name := Title;

    LDestNode := Items.Add(nil,'中华人民共和国') ;

    Items.AddChild(LDestNode,'北京');

    Items.AddChild(LDestNode,'辽宁');

    Items.AddChild(LDestNode,'上海');

    Draggable.Enabled := True;

    Cursor := crDrag;

    OnEndDrag := AnyEndDrag;

  end;

end;

7.9.3运行效果

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab fprintf输出矩阵发布时间:2022-07-18
下一篇:
Matlab矩阵运算发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap