定义一个函数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;