unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TCloseMessageThread = class(TThread)
public
procedure Execute; override;
constructor Create();
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TCloseMessageThread }
constructor TCloseMessageThread.Create();
begin
FreeOnTerminate:= true;
inherited Create(False);
end;
procedure TCloseMessageThread.Execute;
var
hWndForground: HWND;
sCaption: array [0..254] of char;
begin
while not Terminated do
begin
hWndForground:= GetForegroundWindow();
if hWndForground <> 0 then
begin
GetWindowText(hWndForground, sCaption, Length(sCaption) + 1);
if SameText(sCaption, Application.Title) then //'Project1',主窗口标题不要和工程名相同。
PostMessage(hWndForground, WM_CLOSE, 0, 0);
end;
Sleep(20);
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
TCloseMessageThread.Create;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage('hello');
end;
end.