c# 按住 yes 5 秒后关闭系统

发布于 2024-10-01 09:52:10 字数 178 浏览 3 评论 0原文

我想知道是否有人知道如何使用对话框来创建按住按钮事件。场景如下:

用户想要关闭系统,但由于确认至关重要,因此用户必须按住按钮 5 秒钟才能执行操作。

我试图在“是”“否”的情况下做到这一点,即。

要确认关闭,请按住“是”5 秒钟。

有人在能够提供一些帮助/见解之前做过这件事吗?

I was wondering if anyone knows how to use a dialog box to create a hold down button event. Here is the scenerio:

a user would like to shutdown their system, but because it is critical that they confirm, that user must hold the button for 5 seconds before the action can be done.

I am trying to do it in a yes no scenario ie.

To confirm shutdown please hold "Yes" for 5 seconds.

Anyone done this before able to offer a little help/insight?

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

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

发布评论

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

评论(9

来世叙缘 2024-10-08 09:52:11

当然,处理 mousedown 事件和 mouseup 事件。在 mousedown 上启动一个计时器,看看它在 mouseup 上运行了多长时间。完毕!

Sure, handle BOTH the mousedown event and the mouseup event. Start a timer on the mousedown and see how long it has run on the mouseup. Done!

剧终人散尽 2024-10-08 09:52:11

您可以通过多种方式做到这一点。我首先想到的是分离一个等待 5 秒的线程,如果用户的鼠标重新出现,该线程就会中止。

    Thread shutdown;
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        shutdown = new Thread(()=>ShutDown());
        shutdown.Start();
    }

    private void ShutDown()
    {
        Thread.Sleep(5000);
        Console.Write("5 seconds has elapsed");
        // Do something.
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        if (shutdown != null)
        {
            shutdown.Abort();
            shutdown = null;
        }
    }

开销低,您无需为如此简单的事情添加额外的支持控件。

You could do this any number of ways. The first that comes to my mind would be to spin off a thread that waits 5 seconds and is simply aborted if the user's mouse comes back up.

    Thread shutdown;
    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        shutdown = new Thread(()=>ShutDown());
        shutdown.Start();
    }

    private void ShutDown()
    {
        Thread.Sleep(5000);
        Console.Write("5 seconds has elapsed");
        // Do something.
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        if (shutdown != null)
        {
            shutdown.Abort();
            shutdown = null;
        }
    }

Low overhead and you're not adding additional supporting controls for something this simple.

黑凤梨 2024-10-08 09:52:11

当您可以只使用 getAsyncKeyState() 时为什么还要麻烦呢?告诉他们按住“y”5 秒钟。您可以在此处找到参考: http://www.pinvoke.net/default.aspx/ user32.getasynckeystate

或者您可以按自己的方式操作,在 MouseDown 上启动计时器,然后在 MouseUp 上结束计时器,然后查看它是否大于或小于 5 秒。 http://msdn .microsoft.com/en-us/library/system.windows.forms.control.mousedown%28VS.71%29.aspx

Why bother when you can just use getAsyncKeyState()? Tell them to hold down 'y' for 5 seconds. You can find a reference here: http://www.pinvoke.net/default.aspx/user32.getasynckeystate

Or you can do it your way and start a timer on MouseDown, then on MouseUp, end the timer and then see if it's more or less than 5 seconds. http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousedown%28VS.71%29.aspx

提赋 2024-10-08 09:52:11

您可以使用 Form.MouseDown 事件检测用户是否按下了鼠标按钮。在事件处理程序中,检查光标是否位于按钮上方(事件在光标的坐标中传递)。然后,您可以启用一个计时器,该计时器将在 5 秒内计时,并在计时器计时时执行关闭操作。

You can use the Form.MouseDown events do detect that the user has pressed the mouse button. In the event handler, check to see if cursor is over the button or not (the event is passed in the coordinates of the cursor). You can then enable a timer which will tick in 5 seconds, and perform the shutdown when the timer ticks.

滥情空心 2024-10-08 09:52:11

当用户第一次单击“是”时,启动一个计时器,反复检查鼠标位置是否在按钮内部。 5 秒后,继续关闭。如果用户将鼠标移出按钮,则停止计时器。

When the user first clicks YES, start a timer that repeatedly checks if the mouse location is inside of the button. After 5 seconds has elapsed, proceed with the shutdown. If the user moves the mouse out of the button, stop the timer.

呆橘 2024-10-08 09:52:11
private DateTime mouseDownTime;

private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
    mouseDownTime = DateTime.Now;
}

private void Button_MouseUp(object sender, MouseButtonEventArgs e)
{
    if (mouseDownTime.AddSeconds(5) < DateTime.Now)
        MessageBox.Show("You held it for 5 seconds!");
}
private DateTime mouseDownTime;

private void Button_MouseDown(object sender, MouseButtonEventArgs e)
{
    mouseDownTime = DateTime.Now;
}

private void Button_MouseUp(object sender, MouseButtonEventArgs e)
{
    if (mouseDownTime.AddSeconds(5) < DateTime.Now)
        MessageBox.Show("You held it for 5 seconds!");
}
肤浅与狂妄 2024-10-08 09:52:11

您可以在 MouseDown 事件上设置一个计时器,如果在计时器事件触发之前鼠标捕获更改(检查 MouseCaptureChanged 事件)为 false,则取消计时器。

You can set up a timer on the MouseDown event, and if the mouse capture changes (check the MouseCaptureChanged event) to false before the timer event fires, cancel the timer.

花间憩 2024-10-08 09:52:10

尝试使用按钮的 Mouse_Down & Mouse_Up 事件和计时器(假设您使用的是 WinForms)。

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (this.timer1.Enabled == false)
        {
            this.timer1.Interval = 5000;
            this.timer1.Enabled = true;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.timer1.Enabled = false;
        MessageBox.Show("Shutdown!");
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        timer1.Enabled = false;
    }

Try using a button's Mouse_Down & Mouse_Up event, and a timer (this assumes you're using WinForms).

    private void button1_MouseDown(object sender, MouseEventArgs e)
    {
        if (this.timer1.Enabled == false)
        {
            this.timer1.Interval = 5000;
            this.timer1.Enabled = true;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        this.timer1.Enabled = false;
        MessageBox.Show("Shutdown!");
    }

    private void button1_MouseUp(object sender, MouseEventArgs e)
    {
        timer1.Enabled = false;
    }
微暖i 2024-10-08 09:52:10

您可以捕获“mousedown”上的按钮按下情况,并启动 5 秒计时器。一旦计时器完成,就会启动关闭。如果发生“鼠标松开”事件,它可能会停止并重置计时器。

You could capture the button press on 'mousedown', and start a 5-second timer. Once the timer completes, shutdown is initiated. If a 'mouseup' event happens, it could stop and reset the timer.

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