带有按钮的自定义字幕的通用对话框 [英] Generic dialog with custom captions for buttons

查看:227
本文介绍了带有按钮的自定义字幕的通用对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题从此之前已经过去了(例如:显示自定义消息对话框的最佳方式),但我仍然找不到我想要的。

I know this issue have been up since before (ex. Best way to show customized message dialogs), but I still don't find what I want.

我开始像这样:

class function TAttracsForm.MessageDlg(const aMsg: string; aDlgType: TMsgDlgType; Buttons: TMsgDlgButtons; aCaptions: array of String; aDefault: TMsgDlgBtn): TModalResult;
var
  vDlg: TForm;
  i: Integer; 
begin
  if aButtons.Count = aCaptions.Count then
  begin
    vDlg := CreateMessageDialog(aMsg, aDlgType, Buttons);
    try
      for i := 0 aCaptions.Count - 1 do
        TButton(vDlg.FindComponent(Buttons[i].Caption)).Caption := aCaptions[i]; 

      vDlg.Position := poDefaultPosOnly;
      Result := vDlg.ShowModal;
    finally
      vDlg.Free;
    end;
  end;
end;

而这个电话会如下所示:

And the call would look like:

if (MessageDlg('Really quit application ?', mtWarning, 
       [mbNo, mbCancel, mbYes], {'No save', 'Cancel', 'Save'}) = mrYes) then

但上面的代码当然不编译。我不知道如何在循环中获取一个项目,并且如何在开始时得到它的总计数。

But the above code of course don't compile. I don't know how to get one item of an set in the loop and how to get the total count of it in the beginning.

Edit1:
很好,谢谢代码David!但是它仍然不能从调用函数中编译。

Great, thanks for the code David! But it still don't compile from the calling function.

procedure TTAInstructionForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose :=  MessageCustomDlg('Do you want to save changes ?', mtWarning,
                 [ButtonInfo(mbNo, 'Do&n''t save'),
                  ButtonInfo(mbCancel, '&Cancel'),
                  ButtonInfo(mbYes,'&Save')], mbYes) <> mrCancel;

end;

它表示E2003未声明标识符:ButtonInfo。
我需要创建一个记录数组并将其作为参数传递吗?

It says E2003 Undeclared identifier: 'ButtonInfo'. Do I need to create an array of records and pass that as parameter ?

Edit2:
如果看行:

If look at the line:

TButton(vDlg.FindComponent(Buttons[i].Caption)).Caption := Buttons[i].Caption; 

它不会工作。它搜索Buttons [i] .Caption并用Buttons替换[i] .Caption。

It won't work. It search for Buttons[i].Caption and replace it with Buttons[i].Caption.

推荐答案

可以使用此代码:

function MyMessageDlg(CONST Msg: string; DlgTypt: TmsgDlgType; button: TMsgDlgButtons;
  Caption: ARRAY OF string; dlgcaption: string): Integer;
var
  aMsgdlg: TForm;
  i: Integer;
  Dlgbutton: Tbutton;
  Captionindex: Integer;
begin
  aMsgdlg := createMessageDialog(Msg, DlgTypt, button);
  aMsgdlg.Caption := dlgcaption;
  aMsgdlg.BiDiMode := bdRightToLeft;
  Captionindex := 0;
  for i := 0 to aMsgdlg.componentcount - 1 Do
  begin
    if (aMsgdlg.components[i] is Tbutton) then
    Begin
      Dlgbutton := Tbutton(aMsgdlg.components[i]);
      if Captionindex <= High(Caption) then
        Dlgbutton.Caption := Caption[Captionindex];
      inc(Captionindex);
    end;
  end;
  Result := aMsgdlg.Showmodal;
end;

例如:

MyMessageDlg('Hello World!', mtInformation, [mbYes, mbNo],
      ['Yessss','Noooo'], 'New MessageDlg Box'):

这篇关于带有按钮的自定义字幕的通用对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆