【问题标题】:MessageDlg with custom button captions in Delphi FireMonkey在 Delphi FireMonkey 中带有自定义按钮标题的 MessageDlg
【发布时间】:2016-05-11 20:21:30
【问题描述】:

使用 VCL,您可以使用 CreateMessageDialog to generate a message dialog with custom button captions

使用 FMX CreateMessageDialog 似乎不再存在(从 XE3 开始)。

除了从头开始重建消息对话框之外,还有其他方法可以使用 FireMonkey 自定义按钮标题吗?

我希望能够将函数称为described here

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

【问题讨论】:

  • 我认为这是个坏主意,但您可以在 FMX.Consts 中进行编辑并更改这些标签

标签: delphi firemonkey delphi-xe4


【解决方案1】:

我找到了SynTaskDialog for Lazarus and FireMonkey,一个SynTaskDialog 到FireMonkey 的端口。 SynTaskDialog 在较新的 Windows 版本上本机使用 Windows TaskDialog API,并在其他平台上模拟它。

有了这个开源库,我可以定义:

/// 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;

有了这个你可以调用:

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;

【讨论】:

  • 似乎在 Android 上未经测试。来自docs:“FireMonkey 支持 [...] 以实现真正的跨平台和跨编译器使用 [...]。TaskDialog 显示将在 Windows Vista+ 上原生显示,并在所有其他平台上进行模拟 - Windows、Linux 和 OSX经过测试。”
【解决方案2】:

简而言之,没有。您无法访问实际的对话框,就像您在 VCL 中所做的那样。就像DadisX在评论中说的那样,你只能改变资源字符串的值,不能改变对话框本身。

不过,话虽如此,FMX 使用平台抽象层来处理实际对话,您可以稍作调整。在每个支持的平台上,FMX 都有一个实现 FMX 的IFMXDialogService 接口的类,以提供适合平台的对话框。您可以编写自己的类来实现IFMXDialogService 并覆盖其MessageDialog() 方法(以及其他方法),以使用您自己的自定义对话框做任何您想做的事情。然后您可以使用TPlatformServices.RemovePlatformService() 取消注册IFMXDialogService 的默认类,并使用TPlatformServices.AddPlatformService() 注册您的类。

有关详细信息,请参阅 Embarcadero 的文档:

FireMonkey Platform Services

【讨论】:

    猜你喜欢
    • 2023-01-25
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    • 2012-12-10
    相关资源
    最近更新 更多