问题:
   计时器把inc(Timesnum);的时候,Timesnum 常常复位到1。比如已经Timesnum 已经=89了,但是应该是=90的时候,变成了 1。我是用  label1.Caption := IntToStr(Timesnum);来显示的。请各位大侠出手。代码如下:
var
  Form1: TForm1;
  hHook: integer;
  Timesnum: integer;
const
  Timescount = 300*4;function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
begin
  Timesnum := 0;  Result := 0;
end;function StartHook: Boolean;
begin
  Result := False;
  if hHook = 0 then
  begin
    hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
    if hHook > 0 then
    begin
      Result := True;
    end;
  end;
end;procedure StopHook;
begin
  if hHOok > 0 then
  begin
    UnHookWindowsHookEx(hHook);
    hHook := 0;
  end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
  hHook := 0;
  StartHook();
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 stophook;
end;procedure TForm1.Timer1Timer(Sender: TObject);
begin
  inc(Timesnum);
  label1.Caption := IntToStr(Timesnum);
  if Timesnum > Timescount then
    ShowMessage('已经好久没动键盘和鼠标了!');
end;

解决方案 »

  1.   

    function HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
    begin
    //  Timesnum := 0;  //原因是这里,删掉  Result := 0;
    end;function StartHook: Boolean;
    begin
      Result := False;
      if hHook = 0 then
      begin
        Timesnum := 0;  // 在这里初始化
        hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
        if hHook > 0 then
        begin
          Result := True;
        end;
      end;
    end;
      

  2.   

    to maozefa(阿发伯),
    对不起,我没有说清楚,功能是这样的,如果有鼠标或者键盘动作,那么计数器Timesnum 就要复位。如果这台计算机假如有5分钟没有任何鼠标或者键盘动作,那么程序就 ShowMessage('已经好久没动键盘和鼠标了!');否则就把计数器Timesnum 复位为0,不然如果我有2分钟没有动作,那么现在只有3分钟没有动作就会提示 ShowMessage('已经好久没动键盘和鼠标了!');所以计数器Timesnum 需要复位。
    谢谢!
      

  3.   

    要复位可以这样写写在:HookProc(iCode: integer; wParam: wParam; lParam: lParam): LResult; stdcall;
    begin
      if 鼠标或者键盘动作 then 
        Timesnum := 0;   Result := 0;
    end;function StartHook: Boolean;
    begin
      Result := False;
      if hHook = 0 then
      begin
        Timesnum := 0;  // 在这里初始化
        hHook := SetWindowsHookEx(WH_JOURNALRECORD, HookProc, HInstance, 0);
        if hHook > 0 then
        begin
          Result := True;
        end;
      end;
    end;