MessageDlg with custom button captions in Delphi FireMonkey

a 夏天 提交于 2019-11-29 12:56:26

In short, no. You don't have access to the actual dialog, like you do in VCL. Like DadisX said in comment, you can only change the resource string values, but not touch the dialog itself.

However, with that said, FMX uses a platform abstraction layer to handle the actual dialog, and that you can tweak a bit. On each supported platform, FMX has a class that implement's FMX's IFMXDialogService interface to provide platform-appropriate dialogs. You can write your own class that implements IFMXDialogService and overrides its MessageDialog() method (amongst others) to do whatever you want with your own custom dialogs. Then you can unregister the default class for IFMXDialogService using TPlatformServices.RemovePlatformService() and register your class using TPlatformServices.AddPlatformService().

Refer to Embarcadero's documentation for more details:

FireMonkey Platform Services

yonojoy

I found SynTaskDialog for Lazarus and FireMonkey, a port of SynTaskDialog to FireMonkey. SynTaskDialog uses the Windows TaskDialog API natively on newer Windows versions and emulates it on other platforms.

With this Open Source library I can define:

/// returns 100 if first button is pressed, 101 if second button is pressed, ...
function MessageDlgCustom(
  const MsgHeader: string; const MsgText: string; const DlgType: TMsgDlgType; 
  const Buttons: array of string; const DefaultButton: Integer = 0): TModalResult;
var
    Task: TTaskDialog;
    I: Integer;
    DlgIcon: TTaskDialogIcon;   
    Title: string;
begin
    case DlgType of
        TMsgDlgType.mtWarning:
            begin
                DlgIcon := tiWarning;
                Title := 'Warning';
            end;
        TMsgDlgType.mtError:
            begin
                DlgIcon := tiError;
                Title := 'Error';
            end;
        TMsgDlgType.mtInformation:
            begin
                DlgIcon := tiInformation;
                Title := 'Information';
            end;
        TMsgDlgType.mtConfirmation:
            begin
                DlgIcon := tiQuestion;
                Title := 'Confirm';
            end;
        else begin
            DlgIcon := tiBlank;
            Title := '';
        end;
    end;
    Task.Title := Title;
    Task.Inst := MsgHeader;
    Task.Content := MsgText;
    for I := Low(Buttons) to High(Buttons) do
    begin
        if I <> Low(Buttons) then
          Task.Buttons := Task.Buttons + #13#10;
        Task.Buttons := Task.Buttons + Buttons[I];
    end;

    //see docu: custom buttons will be identified with an ID number starting at 100
    Result := Task.Execute([], DefaultButton, [], DlgIcon) - BUTTON_START;
end;

With this you can call:

case MessageDlgCustom('Quit application', 'Really quit application?', mtWarning,
  ['Save', 'Don''t save', 'Cancel']) of
    100: Quit(SAVE_YES);
    101: Quit(SAVE_NO);
    102: Abort;
end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!