如何延迟鼠标输入矩形的动作c#

发布于 2024-09-24 18:51:33 字数 101 浏览 3 评论 0原文

我想在鼠标指针进入 winform 图形矩形并在其中停留一段时间后将操作延迟几秒钟。有什么方法可以做到这一点?

感谢

c#、.net 2.0、winform

I would like to delay an action by several seconds after a mouse pointer has entered and remained for period of time in a winform graphics rectangle. What would be a way to do this?

Thanks

c#, .net 2.0, winform

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-残月青衣踏尘吟 2024-10-01 18:51:33
private Timer timer;
    private void rect_MouseEnter(object sender, EventArgs e)
    {
        timer = new Timer();
        timer.Interval = 3000;
        timer.Start();
        timer.Tick += new EventHandler(t_Tick);

    }

    private void rect_MouseLeave(object sender, EventArgs e)
    {
        timer.Dispose();
    }

    void t_Tick(object sender, EventArgs e)
    {
        timer.Dispose();
        MessageBox.Show(@"It has been over for 3 seconds");
    }
private Timer timer;
    private void rect_MouseEnter(object sender, EventArgs e)
    {
        timer = new Timer();
        timer.Interval = 3000;
        timer.Start();
        timer.Tick += new EventHandler(t_Tick);

    }

    private void rect_MouseLeave(object sender, EventArgs e)
    {
        timer.Dispose();
    }

    void t_Tick(object sender, EventArgs e)
    {
        timer.Dispose();
        MessageBox.Show(@"It has been over for 3 seconds");
    }
对岸观火 2024-10-01 18:51:33

诸如此类的事情:

    static void MouseEnteredYourRectangleEvent(object sender, MouseEventArgs e)
    {
        Timer delayTimer = new Timer();
        delayTimer.Interval = 2000; // 2000msec = 2 seconds
        delayTimer.Tick += new ElapsedEventHandler(delayTimer_Elapsed);
    }

    static void delayTimer_Elapsed(object sender, EventArgs e)
    {
        if(MouseInRectangle())
            DoSomething();

        ((Timer)sender).Dispose();
    }

可能可以更有效地完成,但应该可以工作:D

设置 MouseInRectangle -> 的两种方法一种是让它获取当前的鼠标坐标和控件的位置,并查看它是否在其中,另一种方法是在 control.mouse_leave 上将变量设置为 false。

Something such as:

    static void MouseEnteredYourRectangleEvent(object sender, MouseEventArgs e)
    {
        Timer delayTimer = new Timer();
        delayTimer.Interval = 2000; // 2000msec = 2 seconds
        delayTimer.Tick += new ElapsedEventHandler(delayTimer_Elapsed);
    }

    static void delayTimer_Elapsed(object sender, EventArgs e)
    {
        if(MouseInRectangle())
            DoSomething();

        ((Timer)sender).Dispose();
    }

Probably could be done more efficiently, but should work :D

Two ways to set up MouseInRectangle -> one is to make it get the current mouse coordinates and the position of the control and see if it's in it, another way would be a variable which you would set to false on control.mouse_leave.

居里长安 2024-10-01 18:51:33

尝试使用Timer 控件(System.Windows.Forms.Timer)。

Try using the Timer control (System.Windows.Forms.Timer).

甜味超标? 2024-10-01 18:51:33

请注意 System.Windows.Forms.Timer 并不精确,您不能依赖它会精确地按照给定的时间间隔执行操作。
最好使用 System.Times.timer 并使用 Invoke 操作返回 GUI 线程。

Please notice System.Windows.Forms.Timer is not Exact and you cannot rely it will act on exactly the interval given.
it would be prefeable to use System.Times.timer and use the Invoke action to return to the GUI thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文