program timer ;uses  windows,messages,sysutils;
var
v_timer:integer;
i:integer;
dc:Hwnd;
msg:Tmsg;
Handle,handle2:WORD;procedure TimerProc(hwnd:HWND;uMsg,idEvent:UINT;dwTime:DWORD); stdcall; //此过程就是没有被执行过
begin
dc:=getdc(0);
TextOut(dc,10,10,pchar(inttostr(i)),length(inttostr(i)));
messagebox(0,pchar(inttostr(i)),pchar(inttostr(i)),mb_ok);
i:=i+1;
end;
begin
Handle2 :=GetCurrentThreadID;
messagebox(0,'ok',pchar('the Handle is '+inttostr(Handle2)),0);
v_timer:=5000;
SetTimer(0,10,5000,@TimerProc);while(getmessage(msg,0,0,0)) do
begin
   case msg.message of
   wm_quit:exit;
   wm_close:exit;
   wm_timer:begin
       messagebox(0,PCHAR(inttostr(msg.wParam)+'AND'+inttostr(msg.lParam)),'a',mb_ok);
   end;
   end;
end;end./////////////////wm_timer消息有响应,可就是过程没有自动被调用,谁知道是怎么回是,在消息里如何判断出定时器ID以确定是哪个定时器发出的消息

解决方案 »

  1.   

    程序没任何问题,在我机上TimerProc执行了(win2k+d6)
      

  2.   

    看错了.没有执行是因为你的消息没处理:
    program timer ;uses  windows,messages,sysutils;
    var
    v_timer:integer;
    i:integer;
    dc:Hwnd;
    msg:Tmsg;
    Handle,handle2:WORD;procedure TimerProc(hwnd:HWND;uMsg,idEvent:UINT;dwTime:DWORD); stdcall; //此过程就是没有被执行过
    begin
    dc:=getdc(0);
    TextOut(dc,10,10,pchar(inttostr(i)),length(inttostr(i)));
    messagebox(0,pchar(inttostr(i)),pchar(inttostr(i)),mb_ok);
    i:=i+1;
    end;
    begin
    Handle2 :=GetCurrentThreadID;
    messagebox(0,'ok',pchar('the Handle is '+inttostr(Handle2)),0);
    v_timer:=5000;
    SetTimer(0,10,5000,@TimerProc);while(getmessage(msg,0,0,0)) do
    begin
       case msg.message of
       wm_quit:exit;
       wm_close:exit;
       wm_timer:begin
           messagebox(0,PCHAR(inttostr(msg.wParam)+'AND'+inttostr(msg.lParam)),'a',mb_ok);
       end;
       end;   dispatchmessage(msg);  //这里end;end.另外,SetTimer(0,10,5000,@TimerProc);这个10没用,应该为NULL,看一下SetTimer的HELP可知
    还有,SetTimer返回值就是Timer的id,可用它来判是那个Timer发的消息:
    WM_TIMER的wParam就是这个id,也就是TimerProc中的idEvent
      

  3.   

    我自己实现了一个精度比较高的定时器,已经成功了
    program timer ;uses
      windows,
      messages,
      sysutils,
      MyTimer in 'MyTimer.pas';var
    v_timer:integer;
    i:longint;
    dc:Hwnd;
    msg:Tmsg;
    Handle,handle2:WORD;
    SubThreadID:Thandle;procedure TimerProc(hwnd:HWND;uMsg,idEvent:UINT;dwTime:DWORD); stdcall;
    begin
     v_timer:=5000;
     while (true)   do
     begin
       sleep(v_timer);
       PostThreadMessage(Handle2,wm_user+wm_timer,0,0);
     end;
    end;
    begin
    i:=0;
    Handle2 :=GetCurrentThreadID;
    messagebox(0,'ok',pchar('the Handle is '+inttostr(Handle2)),0);
    //SetTimer(0,10,5000,@TimerProc);
    CreateThread(nil,0,@TimerProc,nil,0,SubThreadID);
    dc:=getdc(0);
    while(getmessage(msg,0,0,0)) do
    begin
       case msg.message of
       wm_quit:exit;
       wm_close:exit;
       wm_timer:begin
           messagebox(0,PCHAR(inttostr(msg.wParam)+'AND'+inttostr(msg.lParam)),'a',mb_ok);
       end;
       wm_user+wm_timer:begin       TextOut(dc,10,10,pchar(inttostr(i)),length(inttostr(i)));
           i:=i+1;   end;
       end;
    end;
     CloseHandle(dc);
    end.
      

  4.   

    我只能说我的定时器能够精确到10ms 已经算是WINDOWS上最高的了
      

  5.   

    更高精度的定时器可用
    1.QueryPerformanceCounter
    2. RDTSC(CPU指令数)
    3.直接对8255芯片进行编程