2011-03-24 63 views
4

我知道這個問題自從之前(例如Best way to show customized message dialogs),但我仍然沒有找到我想要的東西。按鈕的自定義標題的通用對話框

我開始是這樣的:

class function TAttracsForm.MessageDlg(const aMsg: string; aDlgType: TMsgDlgType; Buttons: TMsgDlgButtons; aCaptions: array of String; aDefault: TMsgDlgBtn): TModalResult; 
var 
    vDlg: TForm; 
    i: Integer; 
begin 
    if aButtons.Count = aCaptions.Count then 
    begin 
    vDlg := CreateMessageDialog(aMsg, aDlgType, Buttons); 
    try 
     for i := 0 aCaptions.Count - 1 do 
     TButton(vDlg.FindComponent(Buttons[i].Caption)).Caption := aCaptions[i]; 

     vDlg.Position := poDefaultPosOnly; 
     Result := vDlg.ShowModal; 
    finally 
     vDlg.Free; 
    end; 
    end; 
end; 

和呼叫會是什麼樣子:

if (MessageDlg('Really quit application ?', mtWarning, 
     [mbNo, mbCancel, mbYes], {'No save', 'Cancel', 'Save'}) = mrYes) then 

但當然,上面的代碼不進行編譯。我不知道如何在循環中獲取一個集合中的一個項目,以及如何在開始時獲得它的總數。

+5

也許你可以使用TTaskDialog或前Vist之一有能力的模擬。 – 2011-03-24 10:56:38

+0

+1我同意Vista任務對話框是可用的方式。 – 2011-03-24 11:08:19

+0

我的應用程序只在XP和Server 2003 R2上運行,所以我不能使用TTaskDialog。 – 2011-03-24 12:15:41

回答

8

您可以使用此代碼:

function MyMessageDlg(CONST Msg: string; DlgTypt: TmsgDlgType; button: TMsgDlgButtons; 
    Caption: ARRAY OF string; dlgcaption: string): Integer; 
var 
    aMsgdlg: TForm; 
    i: Integer; 
    Dlgbutton: Tbutton; 
    Captionindex: Integer; 
begin 
    aMsgdlg := createMessageDialog(Msg, DlgTypt, button); 
    aMsgdlg.Caption := dlgcaption; 
    aMsgdlg.BiDiMode := bdRightToLeft; 
    Captionindex := 0; 
    for i := 0 to aMsgdlg.componentcount - 1 Do 
    begin 
    if (aMsgdlg.components[i] is Tbutton) then 
    Begin 
     Dlgbutton := Tbutton(aMsgdlg.components[i]); 
     if Captionindex <= High(Caption) then 
     Dlgbutton.Caption := Caption[Captionindex]; 
     inc(Captionindex); 
    end; 
    end; 
    Result := aMsgdlg.Showmodal; 
end; 

例如:

MyMessageDlg('Hello World!', mtInformation, [mbYes, mbNo], 
     ['Yessss','Noooo'], 'New MessageDlg Box'): 
+1

工作完美但在實例中必須使用,而不是;用於劃分參數,您可以使用正常的MessageDlg結果進行檢查。因此,以這種方式使用它:如果MyMessageDlg('Hello World!',mtInformation,[mbYes,mbNo], ['Yessss','Noooo'],'New MessageDlg Box')= mrYes then Begin .... End – QMaster 2015-09-06 10:45:33

+0

這對'Firemonkey'不起作用。 – Machado 2016-01-18 13:44:11

+1

@Holmes對於Firemonkey的解決方案,請看[此答案](http://stackoverflow.com/a/35232732/2306907)。 – yonojoy 2016-02-05 22:17:43

5

怎麼是這樣的:

type 
    TButtonInfo = record 
    MsgDlgBtn: TMsgDlgBtn; 
    Caption: string; 
    end; 

function ButtonInfo(MsgDlgBtn: TMsgDlgBtn; const Caption: string): TButtonInfo; 
begin 
    Result.MsgDlgBtn := MsgDlgBtn; 
    Result.Caption := Caption; 
end; 

const 
    ModalResults: array[TMsgDlgBtn] of Integer = (
    mrYes, mrNo, mrOk, mrCancel, mrAbort, mrRetry, mrIgnore, mrAll, mrNoToAll, 
    mrYesToAll, 0, mrClose); 

function FindDialogButton(Form: TForm; MsgDlgBtn: TMsgDlgBtn): TButton; 
var 
    i: Integer; 
    Component: TComponent; 
begin 
    for i := 0 to Form.ComponentCount-1 do begin 
    Component := Form.Components[i]; 
    if Component is TButton then begin 
     if TButton(Component).ModalResult=ModalResults[MsgDlgBtn] then begin 
     Result := TButton(Component); 
     exit; 
     end; 
    end; 
    end; 
    Result := nil; 
end; 

function MessageDlg(
    const aMsg: string; 
    aDlgType: TMsgDlgType; 
    const Buttons: array of TButtonInfo; 
    aDefault: TMsgDlgBtn 
): TModalResult; 
var 
    i: Integer; 
    MsgDlgButtons: TMsgDlgButtons; 
    vDlg: TForm; 
begin 
    MsgDlgButtons := []; 
    for i := low(Buttons) to high(Buttons) do begin 
    Assert(not (Buttons[i].MsgDlgBtn in MsgDlgButtons));//assert uniqueness 
    Include(MsgDlgButtons, Buttons[i].MsgDlgBtn); 
    end; 
    vDlg := CreateMessageDialog(aMsg, aDlgType, MsgDlgButtons, aDefault); 
    try 
    for i := low(Buttons) to high(Buttons) do begin 
     FindDialogButton(vDlg, Buttons[i].MsgDlgBtn).Caption := Buttons[i].Caption; 
    end; 
    vDlg.Position := poDefaultPosOnly; 
    Result := vDlg.ShowModal; 
    finally 
    vDlg.Free; 
    end; 
end; 

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

enter image description here

+0

嗯......這不起作用... – 2011-03-24 13:14:24

+0

爲什麼不呢?是因爲我用加速鍵嗎?另外,「這不起作用」不是一個很好的錯誤報告..... ;-) – 2011-03-24 13:15:40

+0

我試過你的代碼,並得到http://privat.rejbrand.se/dlgbtn​​.png。 – 2011-03-24 13:17:11

相關問題