2014-01-30 51 views
1

是否有與Windows的MessageBox()相同的Firemonkey,即對話框的標題/標題可以設置的位置?我知道我可以自己做一個,但更喜歡使用現有的解決方案,但我似乎無法找到它。等效於Windows的MessageBox()的Firemonkey?

+1

我不認爲有一個。我最終編寫了一個封裝類,它使用Windows上的MessageBox和OSX上的NSAlert。 MessageDlg中的構建也存在本地化問題。 –

+0

啊,還有一段代碼我會寫兩遍。我開始使用FMX思考我可以編寫一次所有代碼,但我發現自己越來越多地瞭解有關編碼Mac的知識:)。謝謝,我會查找NSAlert。 –

+0

我需要包含哪些頭文件才能使用NSAlert?我似乎無法找到它,並且幫助沒有多大幫助(如往常一樣)。 –

回答

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調用類似的MessageDlg呼叫FireMonkey:

TDAMessageBox.MessageDialog('Title', 'Message Text', TMsgDlgType.mtError, mbYesNoCancel, 0); 

結果看起來像這樣:

enter image description here

+0

謝謝。我會看看我是否可以在C++中完成這項工作。 –

+0

甚至沒有嘗試此代碼,因爲我注意到沒有辦法設置標題,這是我發佈的原因。此外,看到NSAlert(ShowMessage轉換爲它)它看起來像一個真正的警報框,而我需要一個共同的對話框來顯示一些信息。 –

+0

是的你是對的。我在示例中添加了「標題」。對於NSAlert,您可以添加兩個文本:Alert.setMessageText(NSSTR(Title));和Alert.setInformativeText(NSSTR(Msg));.它不是Window上的Window標題,它在對話框中顯示兩個文本。 –