c# - 在MouseDoubleClick期间禁用事件

时间:2016-07-13 09:20:25

标签: c# events

是否可以在MouseDoubleClick事件期间禁用其他MouseEvent? 我在标签上注册了多个事件,但我希望MouseDoubleClick是优先的,并阻止所有其他MouseEvent(例如MouseDown或MuoseMove)。然后,在DoubleClickHandler完成后,我想重新激活它们。

4 个答案:

答案 0 :(得分:2)

您可以创建一个标记来存储您的不同状态,以便您的方法知道他们必须知道的内容。

一种简单的方法就是:

bool dblClickDone = false;

void DoubleClickHandler(...) 
{
    //...
    dblClickDone = true;
}

void MouseDownHandler(...) 
{
    if (dblClickDone) {
        //...
    }
}

你明白了。

答案 1 :(得分:1)

使用“ - =”取消注册事件,例如:

TextView

答案 2 :(得分:0)

MSDN提出了两种方法。您使用的那个将基于您的程序设计。我已经在下面的MSDN中复制了代码,以防页面出现故障。

第一种方法是回滚在单击'中所做的任何更改。如果双击事件被触发,则为event。

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);
        }
    }
}

第二种方法是创建一个在执行单击时触发的计时器。如果在计时器限制内未遵循双击,则假定用户想要执行单击并执行该操作,否则将执行双击操作。

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);
        }
    }
}

答案 3 :(得分:0)

我在双击和按下鼠标时遇到了类似的问题。我已经体验到,在合理的双击过程中,双击期间的鼠标向上触发两次。

对我来说,最有效的方法是将鼠标隐藏在可以触发的两个级别之后。布尔值和计时器的时间间隔短得使用户看不到,但长于双击中的两次单击之间的时间间隔。

布尔值canMouseUp默认为true。并应在双击期间设置为false,以在双击完成后忽略鼠标。

鼠标悬停检查canMouseUp,如果为false,则将其设置为true,除此之外没有其他设置。如果canMouseUp为true,则启动mouseUpTimer。

双击将mouseUpTimer停止,将canMouseUp设置为false并完成操作。

private void MyObject_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        try
        {

            if (!canMouseUp)
            {
                canMouseUp= true;
                e.Handled = true;
            }
            else
            {
                mouseUpTimer.Start();
                e.Handled = true;
            }
        }
        catch (Exception)
        {

            throw;
        }
    }

private void MyObject_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            try
            {
                mouseUpTimer.Stop();
                // Do what DoubleClick is supposed to do
                canMouseUp = false;
                e.Handled = true;
            }
            catch (Exception)
            {

                throw;
            }
        }
private void mouseUpTimer_Elapsed(object sender, ElapsedEventArgs e)
        {

            try
            {
                // Do what Mouse up is supposed to do.
            }
            catch (Exception)
            {

            }
        }