winform中的mousemove事件,click与mouseup,mousedown事件的关系,

首先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事件里进行比较,是否响应事件

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值