c# 按住 yes 5 秒后关闭系统
我想知道是否有人知道如何使用对话框来创建按住按钮事件。场景如下:
用户想要关闭系统,但由于确认至关重要,因此用户必须按住按钮 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当然,处理 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!
您可以通过多种方式做到这一点。我首先想到的是分离一个等待 5 秒的线程,如果用户的鼠标重新出现,该线程就会中止。
开销低,您无需为如此简单的事情添加额外的支持控件。
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.
Low overhead and you're not adding additional supporting controls for something this simple.
当您可以只使用 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
您可以使用 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.
当用户第一次单击“是”时,启动一个计时器,反复检查鼠标位置是否在按钮内部。 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.
您可以在 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.
尝试使用按钮的 Mouse_Down & Mouse_Up 事件和计时器(假设您使用的是 WinForms)。
Try using a button's Mouse_Down & Mouse_Up event, and a timer (this assumes you're using WinForms).
您可以捕获“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.