2011-03-24 81 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