delphi 自定义弹出窗口,减少form(dfm文件)的数量

定义一个函数PopUpForm,用来弹出窗口,减少form(dfm文件)的数量,和那么多uses。

弹出窗口后,把aPanel放在窗口上,提供两个按钮,确定取消。确定按钮的标签可更改。两个按钮分别响应键盘回车ESC

函数返回true,点了确定按钮;返回false,点了取消按钮。

参数:
aPanel: TControl;                  //一个存放各种组件的面板,或单一个VCL组件,如1个memo,用来显示内容
aParent: TWinControl;          //指定 PopUpForm 运行前, aPanel 的 Parent
aCaption,aOkBtnTitle: string;//设置弹出窗口的标题,和确定按钮的标签
aYouQuxiao: boolean=false //true,界面上有取消按钮,false,没有。

 function PopUpForm(aPanel: TControl; aParent: TWinControl; aCaption,
  aOkBtnTitle: string; aYouQuxiao: boolean=false): boolean;
 var
  ssForm: TForm;
 begin
  result:=false;
 
  ssForm := TForm.Create(Application);
  with ssForm do
  try
     Canvas.Font.Name:=Ziti;
     Canvas.Font.Size:=ZitiDaxiao;
     Position:=poOwnerFormCenter;
     BorderStyle:=forms.bsSingle;
     BorderIcons:=[biSystemMenu];
     ssForm.ClientHeight := aPanel.Height+29;
     ssForm.ClientWidth := aPanel.Width;
     aPanel.Left:=0;
     aPanel.Top:=0;
     aPanel.Parent:=ssForm;
     aPanel.Visible:=true;
     Caption:=aCaption;
     Font.Name:=Ziti;
     Font.Size:=ZitiDaxiao;
     with TButton.Create( ssForm) do
     begin
       Parent := ssForm;
       Caption := aOkBtnTitle;
       ModalResult := 1; //mrOK
       Default:=true;
       SetBounds(aPanel.Width-63, aPanel.Height+2, 58, 25);
     end;
     if aYouQuxiao then
     with TButton.Create(ssForm) do
     begin
       Parent := ssForm;
       Caption := '取消';
       ModalResult := 2;//mrCancel;
       Cancel:=true;
       SetBounds(aPanel.Width-126, aPanel.Height+2, 58, 25);
     end;
     if ShowModal = 1 then result:=true;
  finally
     aPanel.Visible:=false;
     aPanel.Parent:=aParent;
     ssForm.Free;
  end;
 end;
 
 
 比如可自定义一个MessageBox,上面有一个label显示消息内容,btnCnt=1,只有一个确定按钮,btnCnt=2,则可以提供确定,取消两个按钮接受选择。
 
 function MessageBox(pa:TWinControl; bt,nr:string;
  btnCnt:integer=1):boolean;
 var
  lb:TLabel;
 begin
  lb:=TLabel.Create(pa);
  with lb do
  begin
     left:=0;
     top:=0;
     AlignMent:=taCenter;
     AutoSize:=false;
     Width := 320;
     Height:=70;
     Caption:=#13#10+nr+#13#10;
     WordWrap:=true;
     Parent:=pa;
  end;
  result:=PopUpForm(lb,pa,bt,'确定',(btnCnt=2));
  lb.Free;
 end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值