如何在Delphi中显示带有两个按钮(继续/关闭)的对话框

时间:2011-12-22 12:21:04

标签: delphi delphi-2010

我想创建一个警告对话框,询问用户注册期间输入的信息是否正确,并询问他是否想要继续或关闭该对话框并更正其信息。

4 个答案:

答案 0 :(得分:11)

var
  td: TTaskDialog;
  tb: TTaskDialogBaseButtonItem;
begin
  td := TTaskDialog.Create(nil);
  try
    td.Caption := 'Warning';
    td.Text := 'Continue or Close?';
    td.MainIcon := tdiWarning;
    td.CommonButtons := [];

    tb := td.Buttons.Add;
    tb.Caption := 'Continue';
    tb.ModalResult := 100;

    tb := td.Buttons.Add;
    tb.Caption := 'Close';
    tb.ModalResult := 101;

    td.Execute;

    if td.ModalResult = 100 then
      ShowMessage('Continue')
    else if td.ModalResult = 101 then
      ShowMessage('Close');

  finally
    td.Free;
  end;
end;

enter image description here

注意: This will only work on Windows Vista or later

答案 1 :(得分:8)

var
  AMsgDialog: TForm;
  abutton: TButton;
  bbutton: TButton;
begin

  AMsgDialog := CreateMessageDialog('This is a test message.', mtWarning,[]);
  abutton := TButton.Create(AMsgDialog);
  bbutton :=  TButton.Create(AMsgDialog);

  with AMsgDialog do

    try

      Caption := 'Dialog Title' ;
      Height := 140;
      AMsgDialog.Width := 260 ;

      with abutton do
      begin
        Parent := AMsgDialog;
        Caption := 'Continue';
        Top := 67;
        Left := 60;
        // OnClick :tnotyfievent ;
      end;

      with bbutton do
      begin
        Parent := AMsgDialog;
        Caption := 'Close';
        Top := 67;
        Left := 140;
        //OnClick :tnotyfievent ;
      end;

       ShowModal ;

    finally
      abutton.Free;
      bbutton.Free;
      Free;
    end;

enter image description here

答案 2 :(得分:7)

如果是delphi那么

if mrYes=MessageDlg('Continue?',mtwarning,[mbYes, mbNo],0) then 
  begin
        //do somthing
  end
else
exit; //go out

答案 3 :(得分:2)

基于this

procedure HookResourceString(rs: PResStringRec; newStr: PChar);
var
  oldprotect: DWORD;
begin
  VirtualProtect(rs, SizeOf(rs^), PAGE_EXECUTE_READWRITE, @oldProtect);
  rs^.Identifier := Integer(newStr);
  VirtualProtect(rs, SizeOf(rs^), oldProtect, @oldProtect);
end;

const
  SContinue = 'Continue';
  SClose = 'Close';

procedure TForm1.Button1Click(Sender: TObject);
begin
  HookResourceString(@SMsgDlgOK, SContinue);
  HookResourceString(@SMsgDlgCancel, SClose);
  if MessageDlg('My Message', mtConfirmation, [mbOK, mbCancel], 0) = mrOK then
  begin
    // OK...
  end;
end;