MessageDlg - personalizando botoes |
Top Previous Next |
|
// Função utilizada para criar mensagens personalizadas. // Ex.: mlMensagem(['Ok'],'Esta é uma janela de testes'); // mlMensagem(['Não copiar','Copiar'],'Copiar arquivo?', 'Atenção!',2); // Retorna // 1 - 1º botão. 2 - 2º botao ... 0 - ALT+F4 ou X // O ESC não fecha porque eu não uso mrCancel - sem ele NEM a MessageDlg não fecha. // Todos os parametros, exceto os botões são default. function Mensagem(Botoes : Array of String; Mensagem : String = ''; BtnPadrao : Integer = 1; Titulo : String = 'Confirmação'; TipoDlg : TMsgDlgType = mtConfirmation; x : Integer = 0; y : Integer = 0 ): Integer; var Bt : TMsgDlgButtons; Btxt : Array[3..6] of ShortString; // Nao foi usado de 1..4 pois o "X" ou ALT+F4 = 2 I, C : Integer; begin For I := 3 to 6 do Btxt[I] := ''; // Zera var interna For I := Low(Botoes) to High(Botoes) do Btxt[I + 3] := Botoes[I]; // Adiciona captions na var Bt := []; For I := 3 to 5 do if Btxt[I] <> '' then Bt := Bt + [TMsgDlgBtn(I + 1)]; // Coloca na BT os botões do tipo mb... if BTxt[6] <> '' then Bt := Bt + [mbAll];
with CreateMessageDialog(Mensagem, TipoDlg, Bt) do // Cria MessageDlg try if x > 0 Then Top := x; if y > 0 Then left := y; Caption := Titulo; // Define o caption For I := 0 to ComponentCount - 1 do // Corre todos os seus componentes. if Components[I] is TButton then // Se for botão... begin // Caso o modal result dele é o mesmo do que foi criado, muda o caption C := TButton(Components[I]).ModalResult; if C = mrAll then C := 6; TButton(Components[I]).Caption := BTxt[ C ]; // Seta o botão padrão if (BtnPadrao + 2) = TButton(Components[I]).ModalResult then ActiveControl := TButton(Components[I]); end;
Result := ShowModal; // Caso pressionado ESC ou X ou ALT+F4 então devolve "0" senão devolve 1 para 1ª botão, 2 para 2ª... if Result = 2 then Result := 0 else if Result = mrAll then Result := 4 // Se foi mrAll = 6 fica sendo 4 else Result := Result - 2; finally Free; end; end; |