【问题标题】:Mouse Cursor position changes鼠标光标位置变化
【发布时间】:2010-11-10 08:39:31
【问题描述】:

嗨,我有一个 Windows 窗体应用程序,我想移动鼠标然后拖放将起作用,但我尝试使用 mousemove 鼠标事件来执行此操作,但似乎拖放非常敏感。所以我要问的是是否可以检测鼠标光标是否从当前光标移动至少一定距离,然后执行拖放代码。

【问题讨论】:

    标签: c# winforms cursor-position


    【解决方案1】:

    我了解您有一个拖放代码,您只想在鼠标移动一定距离时才执行该代码。如果是这样:

    一旦用户执行了初始操作(鼠标按下要拖动的项目?),您就可以挂钩鼠标移动事件。然后比较鼠标移动事件上的鼠标坐标,一旦坐标差高于你设置的任意值,就触发“拖放代码”。

    private int difference = 10;
    private int xPosition;
    private int yPosition;
    
    private void item_MouseDown(object sender, MouseEventArgs e) 
    {
        this.MouseMove += new MouseEventHandler(Form_MouseMove);
        xPosition = e.X;
        yPosition = e.Y;
    }
    
    private void Form_MouseMove(object sender, MouseEventArgs e) 
    {
        if (e.X < xPosition - difference
            || e.X > xPosition + difference
            || e.Y < yPosition - difference
            || e.Y > yPosition + difference) 
        {
            //Execute "dragdrop" code
            this.MouseMove -= Form_MouseMove;
        }
    }
    

    这将在光标移出虚拟 10x10 正方形时执行拖放操作。

    【讨论】:

    • 我将尝试实现您分享的代码 sn-p 感谢您回答我的问题,我认为它会起作用
    【解决方案2】:

    我真的不明白你的问题和你想要达到的效果。 顺便说一句,如果我的解释是正确的,那么只有当“拖动距离”大于某个量时,您才会尝试做某事。

    下面这段代码没有使用拖放事件,而是使用了 mouseup、mousedown 和 mousemove 事件。

    它会在按下左键时跟踪鼠标移动的距离。 当距离大于固定值时,它会改变鼠标光标(在拖动动作期间)

    当释放鼠标按钮时,如果经过的距离大于最小偏移量,我们执行我们的假放下动作。

    创建一个新的 windows 窗体项目并将 Form1 自动生成的类替换为下面的类

    希望这会有所帮助。

    public partial class Form1 : Form
    {
        private bool isDragging;            //We use this to keep track that we are FakeDragging
        private Point startPosition;        //The start position of our "Fake" dragging action
        private double dragDistance = 0;    //The distance(absolute) from the drag action starting point    
        private const int MINIMUM_DRAG_DISTANCE = 100;  //minimum FakeDrag distance.
    
        private Label label1 = new Label();
    
    
        public Form1()
        {
            #region Support form generation code
                InitializeComponent();
                this.label1 = new System.Windows.Forms.Label();
                this.label1.AutoSize = true;
                this.label1.Location = new System.Drawing.Point(54, 242);
                this.label1.Name = "Distance:";
                this.label1.Size = new System.Drawing.Size(35, 13);
    
                this.Controls.Add(label1);
    
                this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
                this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
                this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
            #endregion
        }
    
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            //if the mouse button is pressed then 
            //there's the chanche a dragAction
            //is being performed and we take note
            //of the position of the click
            //(we will use this later on the mouseMove event
            //to calculate the distance mouse has traveled
            if (e.Button.Equals(MouseButtons.Left))
                startPosition = e.Location;
        }
    
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            ///at the mouse up event we can execute code that does something
            if (isDragging && dragDistance > MINIMUM_DRAG_DISTANCE)
            {
                MessageBox.Show("we have fakedragged!\nDo something useful here");
                //Do your Drag & Drop code here.
            }
    
            ///but WE MUST signal our system that the Fake Drag has ended
            ///and reset our support variables.
            this.Cursor = Cursors.Default;
            isDragging = false;
            dragDistance = 0;
        }
    
    
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            ///when the mouse moves we control if the mouse button is still pressed.
            ///if it is so, we surely are FakeDragging and we set our boolean to true
            ///(it will be useful in the mouseUP event).
            ///Then we calculate the absolute distance the mouse has traveld since the
            ///user has pressed the left mouseButton.
    
    
            Point currentPosition = e.Location;
            if (e.Button.Equals(MouseButtons.Left))
            {
                isDragging = true;
                dragDistance =
                    Math.Abs(
                        Math.Sqrt(
                            (
                                Math.Pow(
                                    (currentPosition.X - startPosition.X), 2)
                            +
                                Math.Pow(
                                    (currentPosition.Y - startPosition.Y), 2)
                            )));
            }
    
            //we set the label text displaying the distance we just calculated
            label1.Text = 
                String.Format(
                    "Distance: {0}", 
                    dragDistance.ToString());
    
            Application.DoEvents();
    
    
            ///At last, if the distance il greater than our offset, we change the 
            ///mouse cursor(this is not required in a real case scenario)
            if (dragDistance > MINIMUM_DRAG_DISTANCE)
                this.Cursor = Cursors.Hand;
            else
                this.Cursor = Cursors.Default;
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 2011-06-26
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多