如何区分单击和双击事件

当我们做Winform项目的时候,有可能会遇到这样的需求,当一个控件上既绑定了单击事件又绑定了双击事件,这样就会遇到一个问题,我们的双击事件没有触发之前,往往先触发了单击事件,影响到了用户的操作。如果让系统区分用户是进行了单击还是双击事件,就显得非常必要了。

我们看一下微软上面是不是有解决办法。

混滚点击操作

 

using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MouseRollBackSingleClick
{
    public class Form1 : Form
    {
        private DoubleClickButton button1;
        private FormBorderStyle initialStyle;
       
        public Form1()
        {
            initialStyle = this.FormBorderStyle;
            this.ClientSize = new System.Drawing.Size(292, 266);
            button1 = new DoubleClickButton();
            button1.Location = new Point (40,40);
            button1.Click += new EventHandler(button1_Click);
            button1.AutoSize = true;
            this.AllowDrop = true;
            button1.Text = "Click or Double Click";
            button1.DoubleClick += new EventHandler(button1_DoubleClick);
            this.Controls.Add(button1);
           
        }

        
        // Handle the double click event.
        void button1_DoubleClick(object sender, EventArgs e)
        {
            // Change the border style back to the initial style.
            this.FormBorderStyle = initialStyle;
            MessageBox.Show("Rolled back single click change.");
        }

        // Handle the click event.
        void button1_Click(object sender, EventArgs e)
        {
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        
    }
    public class DoubleClickButton : Button
    {
        public DoubleClickButton() : base()
        {
            // Set the style so a double click event occurs.
            SetStyle(ControlStyles.StandardClick | 
                ControlStyles.StandardDoubleClick, true);
        }
    }
}

 

区分MouseDown事件中的点击次数

using System;
using System.Drawing;
using System.Windows.Forms;

namespace SingleVersusDoubleClick
{
    class Form1 : Form
    {
        private Rectangle hitTestRectangle = new Rectangle();
        private Rectangle doubleClickRectangle = new Rectangle();
        private TextBox textBox1 = new TextBox();
        private Timer doubleClickTimer = new Timer();
        private ProgressBar doubleClickBar = new ProgressBar();
        private Label label1 = new Label();
        private Label label2 = new Label();
        private bool isFirstClick = true;
        private bool isDoubleClick = false;
        private int milliseconds = 0;

        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }

        public Form1()
        {
            label1.Location = new Point(30, 5);
            label1.Size = new Size(100, 15);
            label1.Text = "Hit test rectangle:";

            label2.Location = new Point(30, 70);
            label2.Size = new Size(100, 15);
            label2.Text = "Double click timer:";

            hitTestRectangle.Location = new Point(30, 20);
            hitTestRectangle.Size = new Size(100, 40);

            doubleClickTimer.Interval = 100;
            doubleClickTimer.Tick +=
                new EventHandler(doubleClickTimer_Tick);

            doubleClickBar.Location = new Point(30, 85);
            doubleClickBar.Minimum = 0;
            doubleClickBar.Maximum = SystemInformation.DoubleClickTime;

            textBox1.Location = new Point(30, 120);
            textBox1.Size = new Size(200, 100);
            textBox1.AutoSize = false;
            textBox1.Multiline = true;

            this.Paint += new PaintEventHandler(Form1_Paint);
            this.MouseDown += new MouseEventHandler(Form1_MouseDown);
            this.Controls.AddRange(new Control[] { doubleClickBar, textBox1, 
                label1, label2 });
        }

        // Detect a valid single click or double click.
        void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            // Verify that the mouse click is in the main hit
            // test rectangle.
            if (!hitTestRectangle.Contains(e.Location))
            {
                return;
            }

            // This is the first mouse click.
            if (isFirstClick)
            {
                isFirstClick = false;

                // Determine the location and size of the double click 
                // rectangle area to draw around the cursor point.
                doubleClickRectangle = new Rectangle(
                    e.X - (SystemInformation.DoubleClickSize.Width / 2),
                    e.Y - (SystemInformation.DoubleClickSize.Height / 2),
                    SystemInformation.DoubleClickSize.Width,
                    SystemInformation.DoubleClickSize.Height);
                Invalidate();

                // Start the double click timer.
                doubleClickTimer.Start();
            }

            // This is the second mouse click.
            else
            {
                // Verify that the mouse click is within the double click
                // rectangle and is within the system-defined double 
                // click period.
                if (doubleClickRectangle.Contains(e.Location) &&
                    milliseconds < SystemInformation.DoubleClickTime)
                {
                    isDoubleClick = true;
                }
            }
        }

        void doubleClickTimer_Tick(object sender, EventArgs e)
        {
            milliseconds += 100;
            doubleClickBar.Increment(100);

            // The timer has reached the double click time limit.
            if (milliseconds >= SystemInformation.DoubleClickTime)
            {
                doubleClickTimer.Stop();

                if (isDoubleClick)
                {
                    textBox1.AppendText("Perform double click action");
                    textBox1.AppendText(Environment.NewLine);
                }
                else
                {
                    textBox1.AppendText("Perform single click action");
                    textBox1.AppendText(Environment.NewLine);
                }

                // Allow the MouseDown event handler to process clicks again.
                isFirstClick = true;
                isDoubleClick = false;
                milliseconds = 0;
                doubleClickBar.Value = 0;
            }
        }

        // Paint the hit test and double click rectangles.
        void Form1_Paint(object sender, PaintEventArgs e)
        {
            // Draw the border of the main hit test rectangle.
            e.Graphics.DrawRectangle(Pens.Black, hitTestRectangle);

            // Fill in the double click rectangle.
            e.Graphics.FillRectangle(Brushes.Blue, doubleClickRectangle);
        }
    }
}

 

 

 

这些例子要求:对System,System.Drawing和System.Windows.Forms程序集的引用。

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鱼>

一分也是爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值