using System;
using System.Diagnostics;
using System.Windows.Forms;
public class Form1 : Form
{
private Stopwatch stopwatch;
private bool isImageClicked = false;
public Form1()
{
// 初始化 Stopwatch 对象
stopwatch = new Stopwatch();
// 为按钮的 Click 事件添加事件处理程序
image.MouseLeftButtonDown+= ico_OnMouseLeftButtonDown;
}
private void ico_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// 如果按钮在 500 毫秒内被点击过
if (isImageClicked && stopwatch.ElapsedMilliseconds < 500)
{
// 执行双击事件的处理逻辑
MessageBox.Show("按钮被双击了!");
}
else
{
// 重置 Stopwatch 并标记按钮已被点击
stopwatch.Restart();
isImageClicked = true;
}
}
}
给image等不能直接绑定双击事件的元素绑定双击事件
本文介绍了一个使用C#编写的WindowsForms应用程序,通过Stopwatch类检测鼠标左键单击事件是否在500毫秒内连续发生,实现按钮的双击检测功能。
摘要由CSDN通过智能技术生成