[Delphi] Personalizzare i pulsanti del MessageDlg.
In Delphi è possibile personalizzare i pulsanti presenti sui messaggi della nostra applicazione, per fare ciò è necessario creare la funzione MioMessaggio.
function MioMessaggio(const Msg:string; DlgType:TMsgType; Buttons: TMsgDlgButtons; Caption: array of String): integer;
var
aMsgDlg:TForm;
i:integer;
dlgButton: TButton;
CaptionIndex: integer;
begin
aMsgDlg := CreateMessageDialog(Msg, DlgType, Buttons);
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(Captions) then
break;
dlgButton.Caption := Captions[CaptionIndex];
Inc(CaptionIndex);
end;
end;
Result := aMsgDlg.ShowModal;
end;
Per provare il funzionamento della su indicata funzione, basta inserire un pulsante sulla form ed assegnare all’evento onClick il seguente codice:
procedure TForm1.Button1Click(Sender: TObject);
begin
if MioMessaggio(’Quale pulsante clicchi?’, mtCustom, mbOkCancel, ['Si', 'No']) = mrOk then
ShowMessage(’Hai cliccato il pulsante SI’);
else
ShowMessage(’Hai cliccato il pulsante NO’);
end;