下面是我的一个运行后监视系统运行特定程序的一个无窗体的完整源程序。
使用api函数来设置timer,而且能在运行时响应消息。
不知对你是否有用?
program sng;
uses
Windows,Messages,SysUtils,Forms,Registry;
var
quit_time:SYSTEMTIME;
function RegisterServiceProcess(dwProcessID,dwType:Integer):Integer;stdcall;external 'KERNEL32.DLL';
procedure ontimer;
var
hwndapplication1,hwndapplication2,hwndapplication3:HWND;
begin
hwndapplication1:=findwindow(nil,'Age of Empires Expansion');
hwndapplication2:=findwindow(nil,'RaidenII');
hwndapplication3:=findwindow(nil,'Age of Empires');
if hwndapplication1 <> 0 then
begin
SendMessage(hwndapplication1, WM_CLOSE, 0, 0);
end;
if hwndapplication3 <> 0 then
begin
SendMessage(hwndapplication3, WM_CLOSE, 0, 0);
end;
if hwndapplication2 <> 0 then
begin
SendMessage(hwndapplication2, WM_CLOSE, 0, 0);
PostMessage(HWND_BROADCAST, WM_KEYDOWN,13, 0);
end;
end;
function timerProc(hwnd:HWND;uMsg:UINT;idEvent:UINT;dwTime
WORD)
WORD;
procedure init_app;
begin
//设置计时器
TimerProc(application.handle,wm_timer,1,gettickcount);
settimer(application.handle,1,10000,@ontimer);
//使程序不出现在windows关闭对话框中
RegisterServiceProcess(GetCurrentProcessID,1);
end;
procedure Close_app;
var
reg_game:Tregistry;
begin
//至注册表内添加启动选项
reg_game:=Tregistry.Create ;
reg_game.RootKey:=HKEY_LOCAL_MACHINE;
try
reg_game.OpenKey('SOFTWARE/Microsoft/Windows/CurrentVersion/Run',True);
if reg_game.KeyExists('游戏至尊')=false then
//extractfiledir(application)----可获得程序启动路径
//extractfilepath()---类似
//extractfilename()----可获得程序的文件名
reg_game.WriteString('游戏至尊',extractfilepath(application.exename)+ExtractFileName(application.ExeName ));
except
application.MessageBox('I cannot registry myself','error',4);
End;
reg_game.CloseKey ;
reg_game.Free;
//使程序出现在windows关闭对话框中
RegisterServiceProcess(GetCurrentProcessID,0);
//释放计时器
killtimer(application.handle,1);
end;
//主程序
begin
init_app;
close_app;
getsystemtime(quit_time);
while quit_time.wYear =1999 do
begin
getsystemtime(quit_time);
ontimer;
end;
close_app;
end;
end.