怎么让一个form在两分钟内没有鼠标和键盘点击就自动关闭,怎么做?

解决方案 »

  1.   

    var
      Form1: TForm1;
      Count:byte = 120;
    procedure TForm1.Timer1Timer(Sender: TObject);
    begin
    Dec(count,1);
    if count = 0 then
    close;
    end;
      

  2.   

    对楼上的进行修正:
    1.窗体中增加定时器
    2.在窗体中定义变量FCount,不应该是全局变量
    3.窗体的KeyPreview设置位True
    看看下面的代码:
    type
      TForm1 = class(TForm)
        Timer1: TTimer;
        procedure Timer1Timer(Sender: TObject);
        procedure ApplicationEvents1Message(var Msg: tagMSG;
          var Handled: Boolean);
        procedure FormKeyDown(Sender: TObject; var Key: Word;
          Shift: TShiftState);
        procedure FormCreate(Sender: TObject);
      private
        FCount: Integer;
      protected
        procedure WndProc(var Msg: TMessage); override;
      public
        { Public declarations }
      end;implementation{$R *.dfm}{ TForm1 }procedure TForm1.WndProc(var Msg: TMessage);
    begin
      inherited;
      if (Msg.Msg >= WM_MOUSEFIRST) and (Msg.Msg <= WM_MOUSELAST) then
      begin
        FCount := 100;
      end;
    end;procedure TForm1.Timer1Timer(Sender: TObject);
    begin
      Dec(FCount);
      if FCount < 0 then
        Close;
    end;procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    begin
      FCount := 100;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      FCount := 100;
      KeyPreview := True;
    end;
      

  3.   

    添加一个timer,然后在其中添加两分钟自动关闭事件
    再在相关的mousemove,mouseup,mousewheelup,keyup添加激活timer的语句timer.Enable := True;
    然后在mousedown,mousewheeldown和keyup事件中添加使timer失效的语句:timer.Enable := False;