带有自定义按钮标题的通用对话框

发布于 2024-10-25 17:35:01 字数 1029 浏览 5 评论 0原文

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

我是这样开始的:

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;

调用看起来像:

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

但是上面的代码当然不能编译。我不知道如何在循环中获取一组中的一项以及如何在开始时获取它的总数。

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.

I started like this:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清浅ˋ旧时光 2024-11-01 17:35:01

您可以使用此代码:

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'):

you can use this code:

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;

For example:

MyMessageDlg('Hello World!', mtInformation, [mbYes, mbNo],
      ['Yessss','Noooo'], 'New MessageDlg Box'):
不一样的天空 2024-11-01 17:35:01

像这样的东西怎么样:

type
  TButtonInfo = record
    MsgDlgBtn: TMsgDlgBtn;
    Caption: string;
  end;

function ButtonInfo(MsgDlgBtn: TMsgDlgBtn; const Caption: string): TButtonInfo;
begin
  Result.MsgDlgBtn := MsgDlgBtn;
  Result.Caption := Caption;
end;

const
  ModalResults: array[TMsgDlgBtn] of Integer = (
    mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll,
    mrYesToAll, 0, mrClose);

function FindDialogButton(Form: TForm; MsgDlgBtn: TMsgDlgBtn): TButton;
var
  i: Integer;
  Component: TComponent;
begin
  for i := 0 to Form.ComponentCount-1 do begin
    Component := Form.Components[i];
    if Component is TButton then begin
      if TButton(Component).ModalResult=ModalResults[MsgDlgBtn] then begin
        Result := TButton(Component);
        exit;
      end;
    end;
  end;
  Result := nil;
end;

function MessageDlg(
  const aMsg: string;
  aDlgType: TMsgDlgType;
  const Buttons: array of TButtonInfo;
  aDefault: TMsgDlgBtn
): TModalResult;
var
  i: Integer;
  MsgDlgButtons: TMsgDlgButtons;
  vDlg: TForm;
begin
  MsgDlgButtons := [];
  for i := low(Buttons) to high(Buttons) do begin
    Assert(not (Buttons[i].MsgDlgBtn in MsgDlgButtons));//assert uniqueness
    Include(MsgDlgButtons, Buttons[i].MsgDlgBtn);
  end;
  vDlg := CreateMessageDialog(aMsg, aDlgType, MsgDlgButtons, aDefault);
  try
    for i := low(Buttons) to high(Buttons) do begin
      FindDialogButton(vDlg, Buttons[i].MsgDlgBtn).Caption := Buttons[i].Caption;
    end;
    vDlg.Position := poDefaultPosOnly;
    Result := vDlg.ShowModal;
  finally
    vDlg.Free;
  end;
end;

procedure Test;
begin
  MessageDlg(
    'Really quit application ?',
    mtWarning,
    [ButtonInfo(mbNo, 'Do&n''t save'), ButtonInfo(mbCancel, '&Cancel'), ButtonInfo(mbYes,'&Save')],
    mbYes
  );
end;

在此处输入图像描述

How about something like this:

type
  TButtonInfo = record
    MsgDlgBtn: TMsgDlgBtn;
    Caption: string;
  end;

function ButtonInfo(MsgDlgBtn: TMsgDlgBtn; const Caption: string): TButtonInfo;
begin
  Result.MsgDlgBtn := MsgDlgBtn;
  Result.Caption := Caption;
end;

const
  ModalResults: array[TMsgDlgBtn] of Integer = (
    mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll,
    mrYesToAll, 0, mrClose);

function FindDialogButton(Form: TForm; MsgDlgBtn: TMsgDlgBtn): TButton;
var
  i: Integer;
  Component: TComponent;
begin
  for i := 0 to Form.ComponentCount-1 do begin
    Component := Form.Components[i];
    if Component is TButton then begin
      if TButton(Component).ModalResult=ModalResults[MsgDlgBtn] then begin
        Result := TButton(Component);
        exit;
      end;
    end;
  end;
  Result := nil;
end;

function MessageDlg(
  const aMsg: string;
  aDlgType: TMsgDlgType;
  const Buttons: array of TButtonInfo;
  aDefault: TMsgDlgBtn
): TModalResult;
var
  i: Integer;
  MsgDlgButtons: TMsgDlgButtons;
  vDlg: TForm;
begin
  MsgDlgButtons := [];
  for i := low(Buttons) to high(Buttons) do begin
    Assert(not (Buttons[i].MsgDlgBtn in MsgDlgButtons));//assert uniqueness
    Include(MsgDlgButtons, Buttons[i].MsgDlgBtn);
  end;
  vDlg := CreateMessageDialog(aMsg, aDlgType, MsgDlgButtons, aDefault);
  try
    for i := low(Buttons) to high(Buttons) do begin
      FindDialogButton(vDlg, Buttons[i].MsgDlgBtn).Caption := Buttons[i].Caption;
    end;
    vDlg.Position := poDefaultPosOnly;
    Result := vDlg.ShowModal;
  finally
    vDlg.Free;
  end;
end;

procedure Test;
begin
  MessageDlg(
    'Really quit application ?',
    mtWarning,
    [ButtonInfo(mbNo, 'Do&n''t save'), ButtonInfo(mbCancel, '&Cancel'), ButtonInfo(mbYes,'&Save')],
    mbYes
  );
end;

enter image description here

蒗幽 2024-11-01 17:35:01

我写了这段代码:(我来自克罗地亚,所以文本是克罗地亚语)

function MojDijalog(const Msg, Capt: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons;
  DefaultButton: TMsgDlgBtn): TModalResult;
var
  dlg : TForm;
begin
  dlg := CreateMessageDialog(Msg, DlgType, Buttons, DefaultButton) ;
  with dlg do begin
     Caption := Capt;
     FormStyle := fsStayOnTop;
     ( FindComponent( 'OK' ) as TButton ).Caption := 'U redu' ;
     ( FindComponent( 'Cancel' ) as TButton ).Caption := 'Odustani' ;
     ( FindComponent( 'Yes' ) as TButton ).Caption := 'Da' ;
     ( FindComponent( 'No' ) as TButton ).Caption := 'Ne' ;
      ( FindComponent( 'Help' ) as TButton ).Caption := 'Pomoć' ;
     ( FindComponent( 'Close' ) as TButton ).Caption := 'Zatvori' ;
     ( FindComponent( 'Ignore' ) as TButton ).Caption := 'Zanemari' ;
     ( FindComponent( 'Retry' ) as TButton ).Caption := 'Pokušaj ponovo' ;
     ( FindComponent( 'Abort' ) as TButton ).Caption := 'Prekini' ;
     ( FindComponent( 'All' ) as TButton ).Caption := 'Sve' ;
  end;
  Result := dlg.ShowModal;
end;

使用示例:

if MojDijalog('Obrisati zapis ?','Upit za brisanje',mtConfirmation,mbYesNo,mbNo) = mrNo then
    begin
         Abort;
    end;

I write this code: (I am from Croatia, so the texts are in Croatian)

function MojDijalog(const Msg, Capt: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons;
  DefaultButton: TMsgDlgBtn): TModalResult;
var
  dlg : TForm;
begin
  dlg := CreateMessageDialog(Msg, DlgType, Buttons, DefaultButton) ;
  with dlg do begin
     Caption := Capt;
     FormStyle := fsStayOnTop;
     ( FindComponent( 'OK' ) as TButton ).Caption := 'U redu' ;
     ( FindComponent( 'Cancel' ) as TButton ).Caption := 'Odustani' ;
     ( FindComponent( 'Yes' ) as TButton ).Caption := 'Da' ;
     ( FindComponent( 'No' ) as TButton ).Caption := 'Ne' ;
      ( FindComponent( 'Help' ) as TButton ).Caption := 'Pomoć' ;
     ( FindComponent( 'Close' ) as TButton ).Caption := 'Zatvori' ;
     ( FindComponent( 'Ignore' ) as TButton ).Caption := 'Zanemari' ;
     ( FindComponent( 'Retry' ) as TButton ).Caption := 'Pokušaj ponovo' ;
     ( FindComponent( 'Abort' ) as TButton ).Caption := 'Prekini' ;
     ( FindComponent( 'All' ) as TButton ).Caption := 'Sve' ;
  end;
  Result := dlg.ShowModal;
end;

Example of use :

if MojDijalog('Obrisati zapis ?','Upit za brisanje',mtConfirmation,mbYesNo,mbNo) = mrNo then
    begin
         Abort;
    end;
路弥 2024-11-01 17:35:01

为了跟进我在因尝试将一组按钮与字符串数组匹配而导致的关键错误的已接受答案中的评论,这里是完整的工作版本(因为我的评论几乎没有关于如何实现的线索)。

function MyMessageDlg(const dlgCaption, dlgMessage : string; dlgType : TmsgDlgType;
                      captions : array of string) : integer;
var
  aMsgdlg : TForm;
  i, captionIndex : integer;
  dlgButton : Tbutton;
  buttons : TMsgDlgButtons; //a set, so can't associate with captions array 
begin
  buttons := []; //load this set with mb Buttons, 1 per captions array element
  for i := 0 to High(captions) do //get all the enums up to the number of buttons
    if i <= Ord(High(TMsgDlgBtn)) then //check we haven't blown the set count
      buttons := buttons + [TMsgDlgBtn(i)]; //add the mb Buttons to our set
  aMsgdlg := createMessageDialog(dlgMessage, dlgType, buttons);
  try
    aMsgdlg.Caption := dlgCaption;
    captionIndex := 0;
    for i := 0 to aMsgdlg.ComponentCount-1 do //find our buttons to change labels
    begin
      if aMsgdlg.Components[i] is TButton then
      Begin
        dlgButton := aMsgdlg.Components[i] as TButton;
        dlgButton.caption := captions[captionIndex];
        dlgButton.ModalResult := captionIndex + 1; //override the mrOK etc with our index, +1 as mrNone is 0
        inc(captionIndex);
      end;
    end;
    result := aMsgdlg.ShowModal - 1; //back to zero based index
  finally
    aMsgdlg.free;
  end;
end;

我将变量重命名为更明显,重新排序参数以匹配典型的对话框调用并且更符合逻辑。

用法: -

MyMessageDlg('Confirm', 'Yes, No or Maybe?', mtConfirmation, ['Yes','No','Maybe']);

其中返回值为 0 表示1 表示 & 2 代表也许

干杯:)

To follow up my comment in the Accepted Answer of the critical bug due to attempting to match buttons in a set to an array of strings, here is the complete working version (as my comment had few clues on how to implement).

function MyMessageDlg(const dlgCaption, dlgMessage : string; dlgType : TmsgDlgType;
                      captions : array of string) : integer;
var
  aMsgdlg : TForm;
  i, captionIndex : integer;
  dlgButton : Tbutton;
  buttons : TMsgDlgButtons; //a set, so can't associate with captions array 
begin
  buttons := []; //load this set with mb Buttons, 1 per captions array element
  for i := 0 to High(captions) do //get all the enums up to the number of buttons
    if i <= Ord(High(TMsgDlgBtn)) then //check we haven't blown the set count
      buttons := buttons + [TMsgDlgBtn(i)]; //add the mb Buttons to our set
  aMsgdlg := createMessageDialog(dlgMessage, dlgType, buttons);
  try
    aMsgdlg.Caption := dlgCaption;
    captionIndex := 0;
    for i := 0 to aMsgdlg.ComponentCount-1 do //find our buttons to change labels
    begin
      if aMsgdlg.Components[i] is TButton then
      Begin
        dlgButton := aMsgdlg.Components[i] as TButton;
        dlgButton.caption := captions[captionIndex];
        dlgButton.ModalResult := captionIndex + 1; //override the mrOK etc with our index, +1 as mrNone is 0
        inc(captionIndex);
      end;
    end;
    result := aMsgdlg.ShowModal - 1; //back to zero based index
  finally
    aMsgdlg.free;
  end;
end;

I renamed variables to be more obvious, reordered the parameters to match the typical dialog call and to be more logical.

Usage: -

MyMessageDlg('Confirm', 'Yes, No or Maybe?', mtConfirmation, ['Yes','No','Maybe']);

Where the return value will be 0 for Yes, 1 for No & 2 for Maybe.

Cheers :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文