Author: Tomas Rutkauskas How can I close a MessageBox() Answer: You can use a thread to achieve that: 1 unit MsgThread; 2 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Classes, Controls, Forms, StdCtrls, ExtCtrls; 7 8 type 9 TMboxThread = class(TThread) 10 private 11 { private declarations } 12 protected 13 procedure Execute; override; 14 public 15 constructor Create; 16 end; 17 18 type 19 TFrmMsgThread = class(TForm) 20 BtnClose: TButton; 21 Edit1: TEdit; 22 Edit2: TEdit; 23 Timer1: TTimer; 24 procedure BtnCloseClick(Sender: TObject); 25 procedure Timer1Timer(Sender: TObject); 26 private 27 FFirst: boolean; 28 FMboxThread: TMBoxThread; 29 FWinHandle: HWnd; 30 public 31 { public declarations } 32 end; 33 34 var 35 FrmMsgThread: TFrmMsgThread; 36 37 implementation 38 39 {$R *.DFM} 40 41 { TMboxThread } 42 43 constructor TMboxThread.Create; 44 begin 45 FreeOnTerminate := True; 46 inherited Create(False); 47 end; 48 49 procedure TMboxThread.Execute; 50 begin 51 { Place thread code here } 52 MessageBox(Application.Handle, 'Text', 'Caption', 53 MB_APPLMODAL + MB_SETFOREGROUND); 54 end; 55 56 { TForm1 } 57 58 procedure TFrmMsgThread.BtnCloseClick(Sender: TObject); 59 begin 60 FMBoxThread := TMBoxThread.Create; 61 FFirst := true; 62 Timer1.Enabled := true; 63 end; 64 65 procedure TFrmMsgThread.Timer1Timer(Sender: TObject); 66 begin 67 Timer1.Enabled := false; 68 if FFirst then 69 begin 70 FWinHandle := GetForegroundWindow; 71 FFirst := false; 72 Timer1.Enabled := true; 73 end 74 else 75 SendMessage(FWinHandle, WM_CLOSE, 0, 0); 76 end; 77 78 end.