unit Unit1;
interface
uses
Forms, ExtCtrls, Classes, Controls, StdCtrls, Windows, Messages;
type
TMboxThread =
class(TThread)
protected
procedure Execute;
override;
public
constructor Create;
end;
type
TForm1 =
class(TForm)
Button1: TButton;
Timer1: TTimer;
procedure BtnCloseClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
FFirst: boolean;
FMboxThread: TMBoxThread;
FWinHandle: HWnd;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
constructor TMboxThread.Create;
begin
FreeOnTerminate := True;
inherited Create(False);
end;
procedure TMboxThread.Execute;
begin
{ Place thread code here }
MessageBox(Application.Handle, '
Text', '
Caption',
MB_APPLMODAL + MB_SETFOREGROUND);
end;
{ TForm1 }
procedure TForm1.BtnCloseClick(Sender: TObject);
begin
FMBoxThread := TMBoxThread.Create;
FFirst := true;
Timer1.Enabled := true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := false;
if FFirst
then
begin
FWinHandle := GetForegroundWindow;
FFirst := false;
Timer1.Enabled := true;
end
else
SendMessage(FWinHandle, WM_CLOSE, 0, 0);
end;
end.