首先click是mousedown 和mouseup的结合,只有两个事件都发生了才会产生click事件,我在做button移动的时候,遇到了移动事件mousemove过后就会发生click事件,但是我只是想移动button,只发生mousemove事件
代码如下:
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 area
{
public partial class Form1 : Form
{
private Point beforeP;//开始位置
private Point afterP;//结束位置
private DateTime downTime;
private DateTime upTime;
public Form1()
{
InitializeComponent();
}
private void calcu_Click(object sender, EventArgs e)
{
this.upTime = DateTime.Now;
//interval为鼠标按下,松开的间隔
TimeSpan interval = this.upTime - this.downTime;
double sec = interval.Seconds;//将时间间隔以秒为单位转化
//this.textBox1.Text = sec.ToString();
if (sec <= 0.8)//如果间隔小于0.8秒才执行click事件
{
double radius = Convert.ToDouble(textBox1.Text);
double pi = 3.14;
double result1 = radius * radius * pi;
this.label3.Text = result1.ToString();
}
}
private void calcu_MouseDown(object sender, MouseEventArgs e)
{
beforeP = e.Location;//记录开始的位置
this.downTime = DateTime.Now;//记录按下鼠标的开始时间
}
private void calcu_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)//鼠标左键按下
{
afterP.X = calcu.Left + (e.X - beforeP.X);
afterP.Y = calcu.Top + (e.Y - beforeP.Y);
calcu.Location = afterP;//控件最后的位置是控件的起始位置+鼠标的位移
}
}
}
}
主要就是利用比较mouseup和mousedown之间发生的时间间隔,在click事件里进行比较,是否响应事件