AI智能
改变未来

C#窗体(二)—鼠标长时间按下事件


鼠标长时间按下事件

创建VS窗体应用程序具体代码如下

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace MouseLong{public partial class Form1 : Form{Timer timer = new Timer();//计时器int timeout = 0;//超时时间MouseEventArgs mouseDown;//鼠标长时间按下时间参数public Form1(){InitializeComponent();timer.Interval = 1000;//计时器时间间隔timer.Tick += new EventHandler(timer_Tick);//计时器时间发生函数}void timer_Tick(object sender, EventArgs e){if (++timeout == 3){OnMouseLongDown(this.mouseDown);//调用鼠标长时间按下事件}}//鼠标按下事件protected override void OnMouseDown(MouseEventArgs e){this.mouseDown = e;//鼠标按下事件参数timeout = 0;//超时时间设置为0timer.Start();//启动计时器}//鼠标抬起事件protected override void OnMouseUp(MouseEventArgs e){timer.Stop();//停止计时器}protected virtual void OnMouseLongDown(MouseEventArgs e) {MessageBox.Show(\"鼠标被长时间按下\");}private void Form1_Load(object sender, EventArgs e){}}}

当鼠标按下后触发了鼠标按下事件函数OnMouseDown,在该函数中启动计时器。当鼠标抬起时触发了鼠标抬起事件函数OnMouseUp,在该函数中停止计时器。在计时器事件函数中进行计时,每隔1秒钟将timeout加1,如果鼠标按下达到 3 秒钟,则在计时器事件函数中触发鼠标长时间按下事件函数OnMouseLongDown。

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C#窗体(二)—鼠标长时间按下事件