2014-01-30 3 views
1

Есть ли эквивалент Firemonkey для Windows MessageBox(), то есть где заголовок/заголовок диалога можно установить? Я знаю, что могу сделать это сам, но предпочитаю использовать существующее решение, но я не могу его найти.Эквивалент Firemonkey для Windows 'MessageBox()?

+1

Я не думаю, что есть один. В итоге я написал класс оболочки, который использует MessageBox для Windows и NSAlert для OSX. У сборки 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, показывающий два текста в диалоговом окне. –

0

Посмотрите на this answer. Используя Open Source SynTaskDialog for Lazarus and FireMonkey, вы можете определить настраиваемое окно сообщения или замену ящика сообщения, где можно установить заголовок диалога.

Он работает на Firemonkey в Windows, но я до сих пор не тестировал его под OSX.

Смежные вопросы