delphi函数定时

2018-10-31

delphi中不使用timer控件,直接用settimer函数实现定时运行程序方法:
函数:
SetTimer(hWnd,ID_TIMER,1000,NULL) ;
KillTimer(hWnd,ID_TIMER) ;

参数依次为:
1.调用函数的handle,windows会定时发送WM_TIMER消息给此handle
2.timer的标识,可设置为0
3.定时器的值,单位是毫秒
4.回调函数
一般这样写:
settimer(self.handle,0,1000,@timerfun);
举例:以下为每2秒打开hao123网站
program Project2;
uses
windows,shellapi;
//===========================
procedure setrun;//自定义过程
begin
ShellExecute(0,'open','http://www.hao123.com',nil,nil,sw_ShowNormal);
end;
//===========================
var
msg: Tmsg;
begin
SetTimer(0, 0, 2000, @setrun); //定时间器,2秒钟运行一次
while GetMessage(msg, 0, 0, 0) do DispatchMessage(msg);
KillTimer(0, 0);
end.
{GetMessage函数可取得与指定窗口联系的消息
在这里DispatchMessage函数通常调度从GetMessage取得的消息}

阅读30