Countdown timer increase on interaction?

痞子三分冷 提交于 2020-01-13 19:38:40

问题


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 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.


回答1:


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);
    }
  }
}


来源:https://stackoverflow.com/questions/5778686/countdown-timer-increase-on-interaction

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!