开发者

Countdown timer increase on interaction?

I have a form that I want to close after 5 seconds if no mouse interaction is done but if any mouse interaction is done I want it to close countdown + 5 seconds and each interaction would increase it by 5 seconds.

This is what I came up with so far:

int countdown = 5;
System.Timers.Timer 开发者_JAVA技巧timer;

Start timer

timer = new System.Timers.Timer(1000);
timer.AutoReset = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(ProcessTimerEvent);
timer.Start();

The event

private void ProcessTimerEvent(Object obj, System.Timers.ElapsedEventArgs e)
{
    --countdown;
    if (countdown == 0)
    {
        timer.Close();
        this.Invoke(new Action(() => { this.Close(); }));
    }
}

And just for testing I am using the form mouseclick event to increaes the countdown by 5 but will have to change it to a a different event because if you click on a label or any other control on the form it will not increase the timer.

private void NotifierTest_MouseClick(object sender, MouseEventArgs e)
{
    countdown += 5;
}

Questions

  • Am I implementing a countdown where the counter can be increased in a correct way ?

  • Should I change anything ?

  • How would you do this if any different from what I have done ?

  • How should I handle the mouse click capture ?

  • Using a Low Level Hook ?

  • Using mouse click position and verify if it was or not on my winform ?

Other option

A different option I am currently thinking is to capture if the mouse is within the form area or not and enable / disable the close countdown if it is not within the area but I am not sure on how to interact with the mouse for this hence the above questions about how would I interact with the mouse.


I think in essence what you are doing is fine, the real trick is going to be to handle the mouse events.

Here is a quick and dirty example of how you could do this just checking if the mouse is in the client area of the window. Basically on every timer expiry the code gets the mouse position on the screen and checks if that overlaps with the client area of the window. You should probably also check if the window is active etc. but this should be a reasonable starting point.

using System;
using System.Windows.Forms;
using System.Timers;
using System.Drawing;

namespace WinFormsAutoClose
{
  public partial class Form1 : Form
  {
    int _countDown = 5;
    System.Timers.Timer _timer;

    public Form1()
    {
      InitializeComponent();

      _timer = new System.Timers.Timer(1000);
      _timer.AutoReset = true;
      _timer.Elapsed += new ElapsedEventHandler(ProcessTimerEvent);
      _timer.Start();
    }

    private void ProcessTimerEvent(Object obj, System.Timers.ElapsedEventArgs e) 
    {
      Invoke(new Action(() => { ProcessTimerEventMarshaled(); }));
    }

    private void ProcessTimerEventMarshaled()
    {
      if (!IsMouseInWindow())
      {
        --_countDown;
        if (_countDown == 0)
        {
          _timer.Close();
          this.Close();
        }
      }
      else
      {
        _countDown = 5;
      }
    }

    private bool IsMouseInWindow()
    {
      Point clientPoint = PointToClient(Cursor.Position);
      return ClientRectangle.Contains(clientPoint);
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜