c# - 获取按钮对象的鼠标在表单上的位置

标签 c# button

虽然我发布了一些与此相关的内容(这让我付出了很多反对票),但我决定继续我自己最初的尝试(取得了一些不错的结果),但我遇到了一个问题,我不能看看如何解决。

我的代码创建了一组n个可点击按钮,按下按钮时应该移动这些按钮。我已经设法做到了,但是当我单击它并移动它们时,它们以一种奇怪的方式“跳跃”,而不是按照我想要的方式重新定位,然后它们可以自由移动。

这是按钮类的代码:

namespace moverButtons 
{
    class buttonCito:Button
    {

        Point posActual;
        bool mousePressed;

       // public MouseEventHandler MouseMove;
        public buttonCito(int altUra, int anchUra, Point position)
        {
            this.Height = altUra;
            this.Width = anchUra;
            this.Location = position;

        }

        public buttonCito()
        {
            // TO DO: Complete member initialization
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {

            mousePressed = (e.Button == MouseButtons.Left) ? true : false;

            if (e.Location != null && mousePressed)
            {
                    moverButton(e.X, e.Y);

            }        

            //Añadir rutina para mover con el mouse 
            //Add routine to move with the mouse
        }
        public void moverButton(int x,int y)
        {

            this.Location = new Point(x + posActual.X, y + posActual.Y);
            posActual = this.Location;
        }

    }
}

这是以下形式的代码:

namespace moverbuttons

{   
    public partial class Form1 : Form
    {
        Point positionMouseForm;

        public Form1()
        {
            InitializeComponent();

            this.MouseMove += new MouseEventHandler(map_MouseMove);

        }

        private void map_MouseMove(object sender, MouseEventArgs e)//obtains the position of the mouse on the form
        {   //I want to give this position to the button class, is there  a way?
            positionMouseForm = e.Location;
        }

        private void Form1_Load(object sender, EventArgs e)
        {   List<buttonCito> buttons = new List<buttonCito>();
            Random rnd = new Random();
            int x,y;

            for (int i = 0; i < 5; i++)
            {
                x = rnd.Next(1, 300);
                y = rnd.Next(1, 300);
                buttonCito newButton = new buttonCito(50,50,new Point(x,y));


                buttons.Add(newButton);
                this.Controls.Add(newButton);
            }
        }
    }
}

如果可以的话,以某种方式将鼠标在表单上的位置提供给按钮,我可以轻松修复它。

最佳答案

解决此问题的一种方法是使用增量(两个对象之间的差异)。

Point lastMousePosition;

private void MoveButton(int currentX, int currentY)
{
     int deltaX = currentX - lastMousePosition.X;
     int deltaY = currentY - lastMousePosition.Y;

     this.Location = new Point(Location.X + deltaX, Location.Y + deltaY);
     lastMousePosition = new Point(currentX, currentY);
}

您可以将lastMousePosition 最初设置为按下按钮时的鼠标位置(并且在释放按钮之前不会再次设置)。

请注意,像这样重复调用构造函数的性能不是很好。您可能需要考虑重用仅实例化一次的 Point 对象。当然,只有当一切正常工作后,你才应该担心这个问题:)

关于c# - 获取按钮对象的鼠标在表单上的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23138575/

相关文章:

javascript - Jquery 按钮 'click()' 功能无法添加新的 html 文本

android - 填充标题文本框后,ListView 页脚元素不可单击

带有 Activity 按钮的Android Listview,可更改行中的文本

java - JButton问题

python - 更改标签文本时出现问题

c# - 在数据库中存储 Dictionary<string, string>

c# - 如何使用 Distinct 从 Linq 查询中选择特定对象?

c# - "multi-threadedly"在 async/await 模式下实际上执行了什么代码?

C# text.Replace 保留大小写

c# - 想要以与保存在 C# windowsForm 中相同的格式读取从数据库检索的文件