SetTimer生成一个随机的IDEvent

ELC*_*ouz 2 windows delphi api timer

当我尝试使用Windows SetTimer函数时,它会为计时器生成一个IDEvent,即使我已经指定了一个!

这个:

SetTimer(0,999,10000,@timerproc); 
Run Code Online (Sandbox Code Playgroud)

在:

procedure timerproc(hwnd: HWND; uMsg: UINT; idEvent: UINT_PTR;dwTime: DWORD); stdcall;
begin
KillTimer(0, idEvent);
showmessage(inttostr(idevent));
end;
Run Code Online (Sandbox Code Playgroud)

返回:

随机数!

是否可以自己管理我的计时器而不是Windows为我选择?

非常感谢你!

NGL*_*GLN 8

如果要在单个例程中处理多个计时器事件,则按特定窗口而不是特定例程处理它:

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    FTimerWindow: HWND;
    procedure TimerProc(var Msg: TMessage);
  end;

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  FTimerWindow := Classes.AllocateHWnd(TimerProc);
  SetTimer(FTimerWindow, 999, 10000, nil);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Classes.DeallocateHWnd(FTimerWindow);
end;

procedure TForm1.TimerProc(var Msg: TMessage);
begin
  if Msg.Msg = WM_TIMER then
    with TWMTimer(Msg) do
      case TimerID of
        999:
          //
      else:
        //
      end;
end;
Run Code Online (Sandbox Code Playgroud)


Aud*_*oGL 5

SetTimer将以不同的方式工作,具体取决于您是否向其传递窗口句柄.

Timer_Indentifier := SetTimer(0, MyIdentifier, Time, @myproc);
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,Timer_Identifier不等于MyIdentifier.

Timer_Indentifier := SetTimer(handle, MyIdentifier, Time, @myproc);
Run Code Online (Sandbox Code Playgroud)

在第二个示例中,Timer_Identifier = MyIdentifier.

这是因为在第二个示例中,您的Windows循环将需要使用"MyIdentifier"来找出哪个计时器正在向其发送"WM_Timer"消息.

使用特定的定时器功能,没有窗口句柄,是不同的.简短的回答是,在您的方案中,使用Windows为您提供的值.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms644906%28v=vs.85%29.aspx

  • 你整理好了.FWIW,你从idEvent得到的随机值等于从SetTimer返回的值. (2认同)