If you are on Windows and don't care about the return value of the MessageDlg then you can call this procedure - specify the caption of the MessageDlg in the parameter:Code: Pascal [Select][+][-]uses Windows; // Finds the window having the specified caption and destroys it.procedure KillWindow(ATitle: String);var H: HWND;begin H := FindWindow(nil, PChar(ATitle)); if H <> 0 then DestroyWindow(H);end;
Auto-close dialogs might be useful for some user, this is one way to implement it(see attachment): Code: Pascal [Select][+][-]unit uMsgDlg; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Dialogs, Controls, ExtCtrls; type TMsgDlgEx = class private FMsgFrm: TForm; FParent: TForm; FTimer: TTimer; FDefModRes: TModalResult; procedure DoOnTimer(Sender: TObject); public constructor Create(const AMsg: string; ADlgType: TMsgDlgType; AButtons: TMsgDlgButtons; AParent: TForm; ADefModRes: TModalResult; AInterval: Cardinal); destructor Destroy; override; function ShowDialog: TModalResult; end; implementation constructor TMsgDlgEx.Create(const AMsg: string; ADlgType: TMsgDlgType; AButtons: TMsgDlgButtons; AParent: TForm; ADefModRes: TModalResult; AInterval: Cardinal);begin FMsgFrm := CreateMessageDialog(AMsg, ADlgType, AButtons); FDefModRes := ADefModRes; FParent := AParent; FTimer := TTimer.Create(nil); FTimer.Enabled := False; FTimer.Interval := AInterval; FTimer.OnTimer := @DoOnTimer;end; destructor TMsgDlgEx.Destroy;begin FTimer.Enabled := False; FTimer.Free; FMsgFrm.Free; inherited Destroy;end; function TMsgDlgEx.ShowDialog: TModalResult;begin FMsgFrm.FormStyle := fsSystemStayOnTop; if FParent <> nil then begin FMsgFrm.Position := poDefaultSizeOnly; FMsgFrm.Left := FParent.Left + (FParent.Width - FMsgFrm.Width) div 2; FMsgFrm.Top := FParent.Top + (FParent.Height - FMsgFrm.Height) div 2; end else FMsgFrm.Position := poWorkAreaCenter; FTimer.Enabled := True; Result := FMsgFrm.ShowModal;end; procedure TMsgDlgEx.DoOnTimer(Sender: TObject);begin FTimer.Enabled := False; FMsgFrm.ModalResult := FDefModRes;end;Usage:Code: Pascal [Select][+][-]uses uMsgDlg; procedure TForm1.Button1Click(Sender: TObject);var MsgDlgEx: TMsgDlgEx; Str: String;begin Str := 'Do you wish to save the changes(Dialog will autoclose in 5 seconds, default result is mrYes)?'; MsgDlgEx := TMsgDlgEx.Create(Str, mtInformation, [mbYes, mbNo], Self, mrYes, 5000); try if MsgDlgEx.ShowDialog = mrYes then ShowMessage('Saving changes') finally MsgDlgEx.Free; end;end;