互动增加了倒数计时器吗? [英] Countdown timer increase on interaction?

查看:72
本文介绍了互动增加了倒数计时器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,如果没有完成鼠标交互,我想在5秒后关闭,但是如果完成任何鼠标交互,我希望它关闭倒数+ 5秒每次互动都会使它增加5秒。

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;

启动计时器

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

事件

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

为了测试,我使用的是mouseclick事件将倒计时增加5,但必须将其更改为其他事件,因为如果您单击标签或表格上的任何其他控件,它将不会增加计时器。

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 ?

      使用一个低级钩子?

      使用鼠标单击位置并验证
      是否在我的winform上?

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

      我目前正在考虑的另一种选择是捕获鼠标是否是否在表单区域内,如果不在该区域内,则启用/禁用关闭倒数,但是我不确定如何与鼠标进行交互,因此出现了有关如何与鼠标进行交互的上述问题。

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

      这篇关于互动增加了倒数计时器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆