Delphi 适用于Windows'的Firemonkey等效物;MessageBox()?
Windows的MessageBox()是否有Firemonkey等价物,即可以在其中设置对话框的标题/标题?我知道我可以自己做一个,但更喜欢使用现有的解决方案,但我似乎找不到它。好的,下面是我如何使用NSAlert的示例:Delphi 适用于Windows'的Firemonkey等效物;MessageBox()?,delphi,firemonkey,Delphi,Firemonkey,Windows的MessageBox()是否有Firemonkey等价物,即可以在其中设置对话框的标题/标题?我知道我可以自己做一个,但更喜欢使用现有的解决方案,但我似乎找不到它。好的,下面是我如何使用NSAlert的示例: unit DAMessageBox; interface uses System.SysUtils, System.IOUtils, FMX.Dialogs, System.UITypes, {$IFDEF MSWINDOWS} Winapi.She
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);
结果如下:
看一看。使用开放源代码,您可以定义自定义消息框或消息框替换,在其中可以设置对话框标题
它在Windows中的Firemonkey上确实可以工作,但我直到现在还没有在OSX下测试过它。另一种方法,可以在所有操作系统上工作,几乎需要相同的时间来编写,而且更加灵活: 创建一个新表单,比如名称为“fmDialog1”,放置您想要的标题和文本(可能还有一些图像),以及确定、取消、是、否等按钮。 为每个按钮的ModalResult属性指定一个不同的非零值 然后在需要对话框的代码中,编写(C++)aa=fmDialog1->ShowModal();或者(在Pascal/Delphi中)aa:=fmDialog1.ShowModal(); 并测试aa以查看用户选择了哪个按钮
在Android应用程序中,您可能需要添加一个循环来克服ShowModal的异步特性。我认为没有。最后我编写了一个包装器类,它在Windows上使用MessageBox,在OSX上使用NSAlert。内置的MessageDlg也有本地化的问题。啊,还有一段代码我将要写两次。我开始使用FMX时以为我可以一次性编写所有代码,但我发现自己越来越了解Mac的编码:)。谢谢,我将查找NSAlert。要使用NSAlert,我需要包含哪些头文件?我似乎找不到它,而且帮助也没有多大帮助(像往常一样)。我甚至没有尝试过这段代码,因为我注意到没有办法设置标题,这就是我发表文章的原因。另外,看过NSAlert(ShowMessage翻译成它)后,它看起来像一个真正的警报框,而我需要一个公共对话框来显示一些信息。是的,你是对的。我在示例中添加了“标题”。对于NSAlert,您可以添加两个文本:Alert.setMessageText(NSSTR(Title));和Alert.setInformativeText(NSSTR(Msg));。它并不像窗口上那样是一个真正的窗口标题,它在对话框中显示两个文本。