是否有一个Firemonkey等效于Windows的MessageBox(),即可以设置对话框的标题/标题?我知道我可以自己制作一个,但更喜欢使用现有的解决方案,但我似乎无法找到它。
答案 0 :(得分:2)
好的,我的例子是我如何使用NSAlert:
unit DAMessageBox;
interface
uses
System.SysUtils,
System.IOUtils,
FMX.Dialogs,
System.UITypes,
{$IFDEF MSWINDOWS}
Winapi.ShellAPI, Winapi.Windows, Vcl.Forms;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
Macapi.CocoaTypes, Macapi.Foundation, Macapi.AppKit,
Posix.Stdlib;
{$ENDIF POSIX}
type
TDAMessageBox = class
class function MessageDialog(const Title: String; const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint) : integer;
end;
implementation
class function TDAMessageBox.MessageDialog(const Title: String; const Msg: string; DlgType: TMsgDlgType;
Buttons: TMsgDlgButtons; HelpCtx: Integer): integer;
var
{$IFDEF MSWINDOWS}
WinButtons : Cardinal;
WinType : Cardinal;
{$ENDIF WINDOWS}
{$IFDEF POSIX}
Alert: NSAlert;
Style: NSAlertStyle;
DlgRes : Integer;
{$ENDIF POSIX}
begin
{$IFDEF MSWINDOWS}
case DlgType of
TMsgDlgType.mtWarning: WinType:= MB_ICONWARNING;
TMsgDlgType.mtError: WinType:= MB_ICONSTOP;
TMsgDlgType.mtInformation: WinType:= MB_ICONINFORMATION;
TMsgDlgType.mtConfirmation: WinType:= MB_ICONQUESTION;
TMsgDlgType.mtCustom: WinType:= MB_ICONINFORMATION;
end;
if Buttons = mbOKCancel then begin
WinButtons:= MB_OKCANCEL;
end;
if Buttons = mbYesNo then begin
WinButtons:= MB_YESNO;
end;
if Buttons = mbYesNoCancel then begin
WinButtons:= MB_YESNOCANCEL;
end;
Result:= MessageBox(Application.Handle, PChar(Msg), PChar(Title), WinType or WinButtons);
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
Alert:= TNSAlert.Create;
//map the configurations:
//mtWarning, mtError, mtInformation, mtConfirmation
case DlgType of
TMsgDlgType.mtWarning: Style:= NSWarningAlertStyle;
TMsgDlgType.mtError: Style:= NSCriticalAlertStyle;
TMsgDlgType.mtInformation: Style:= NSInformationalAlertStyle;
TMsgDlgType.mtConfirmation: Style:= NSInformationalAlertStyle;
TMsgDlgType.mtCustom: Style:= NSInformationalAlertStyle;
end;
try
Alert.setMessageText(NSSTR(Title));
Alert.setInformativeText(NSSTR(Msg));
Alert.setAlertStyle(Style);
//add dialog buttons, note: there are only 3 buttons allowed:
//mbAbortIgnore, mbAbortRetryIgnore, *mbOKCancel,mbYesAllNoAllCancel, mbYesAllNoAllCancel, *mbYesNo, *mbYesNoCancel
//currently I only map the ones I need here
if Buttons = mbOKCancel then begin
//Writeln('mbOKCancel');
Alert.addButtonWithTitle(NSSTR('OK'));
Alert.addButtonWithTitle(NSSTR('Cancel'));
end;
if Buttons = mbYesNo then begin
//Writeln('mbYesNo');
Alert.addButtonWithTitle(NSSTR('Yes'));
Alert.addButtonWithTitle(NSSTR('No'));
end;
if Buttons = mbYesNoCancel then begin
//Writeln('mbYesNoCancel');
Alert.addButtonWithTitle(NSSTR('Yes'));
Alert.addButtonWithTitle(NSSTR('No'));
Alert.addButtonWithTitle(NSSTR('Cancel'));
end;
DlgRes := Alert.runModal;
//map the result to Delphi Consts
//NSAlertFirstButtonReturn = 1000,
//NSAlertSecondButtonReturn = 1001,
//NSAlertThirdButtonReturn = 1002
if Buttons = mbOKCancel then begin
if DlgRes = NSAlertFirstButtonReturn then Result := idYes;
if DlgRes = NSAlertSecondButtonReturn then Result := idNo;
end;
if (Buttons = mbYesNo) or (Buttons = mbYesNoCancel) then begin
if DlgRes = NSAlertFirstButtonReturn then Result := idYes;
if DlgRes = NSAlertSecondButtonReturn then Result := idNo;
if DlgRes = NSAlertThirdButtonReturn then Result := idCancel;
end;
finally
Alert.release;
end;
{$ENDIF POSIX}
end;
end.
MessagBox调用类似于FireMonkey中的MessageDlg调用:
TDAMessageBox.MessageDialog('Title', 'Message Text', TMsgDlgType.mtError, mbYesNoCancel, 0);
结果如下:
答案 1 :(得分:0)
看看this answer。使用开源SynTaskDialog for Lazarus and FireMonkey,您可以定义自定义消息框或消息框替换,其中可以设置对话框标题。
它适用于Windows中的Firemonkey,但直到现在我还没有在OSX下测试它。
答案 2 :(得分:0)
另一种适用于所有操作系统的方法,编写时间几乎相同,而且更加灵活: 创建一个新表单,命名为“fmDialog1”,输入您想要的标题和文本(可能还有一些图像),以及诸如确定、取消、是、否等按钮。 给每个按钮一个不同的值,非零,给它的 ModalResult 属性
然后在需要对话框的代码中,写(用C++)aa=fmDialog1->ShowModal();或(在 Pascal/Delphi 中)aa:=fmDialog1.ShowModal(); 并测试 aa 以查看用户选择了哪个按钮。
在 Android 应用中,您可能需要添加一个循环来克服 ShowModal 的异步特性。