2

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

这个:

SetTimer(0,999,10000,@timerproc); 

在:

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

返回:

随机数!

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

非常感谢!

4

3 回答 3

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;
于 2012-12-27T07:15:43.047 回答
5

SetTimer 的工作方式会有所不同,具体取决于您是否将窗口句柄传递给它。

Timer_Indentifier := SetTimer(0, MyIdentifier, Time, @myproc);

在上述实例中,Timer_Identifier 不等于 MyIdentifier。

Timer_Indentifier := SetTimer(handle, MyIdentifier, Time, @myproc);

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

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

使用没有窗口句柄的特定 Timer 函数是不同的。简短的回答是,在您的场景中,使用 Windows 为您提供的值。

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

于 2012-12-27T06:47:08.537 回答
0

多媒体定时器解决了我的问题!

我可以将任何我想要的东西传递给他们dwUser:)

MMRESULT timeSetEvent(
  UINT uDelay,
  UINT uResolution,
  LPTIMECALLBACK lpTimeProc,
  DWORD_PTR dwUser,
  UINT fuEvent
);

来自 MSDN:dwUser -> 用户提供的回调数据。

  • 他们有一个TIME_ONESHOT选项,这正是我使用计时器的目的!
于 2012-12-27T07:11:28.310 回答