C# 获取按钮对象在窗体上的鼠标位置
虽然我发布了一些与此相关的东西(这让我损失了很多反对票),但我决定继续我最初的尝试(取得了一些好的结果),但我遇到了一个问题,我不知道如何解决它 我的代码创建了一组n个可单击的按钮,按下按钮时应移动这些按钮。我已经设法做到了,但当我点击它并移动它们时,它们会以一种奇怪的方式“跳跃”,而不是重新定位我想要的方式,然后它们可以自由移动 这是button类的代码:C# 获取按钮对象在窗体上的鼠标位置,c#,button,C#,Button,虽然我发布了一些与此相关的东西(这让我损失了很多反对票),但我决定继续我最初的尝试(取得了一些好的结果),但我遇到了一个问题,我不知道如何解决它 我的代码创建了一组n个可单击的按钮,按下按钮时应移动这些按钮。我已经设法做到了,但当我点击它并移动它们时,它们会以一种奇怪的方式“跳跃”,而不是重新定位我想要的方式,然后它们可以自由移动 这是button类的代码: namespace moverButtons { class buttonCito:Button {
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);
}
}
}
}
名称空间移动按钮
{
公共部分类Form1:Form
{
点位置鼠标窗体;
公共表格1()
{
初始化组件();
this.MouseMove+=新的MouseEventHandler(map\u MouseMove);
}
private void map\u MouseMove(object sender,MouseEventArgs e)//获取鼠标在窗体上的位置
{//我想把这个位置给button类,有办法吗?
positionMouseForm=e.位置;
}
私有void Form1\u加载(对象发送方、事件参数e)
{列表按钮=新列表();
随机rnd=新随机();
int x,y;
对于(int i=0;i<5;i++)
{
x=下一个rnd(1300);
y=下一个rnd(1300);
按钮到新按钮=新按钮到(50,50,新点(x,y));
按钮。添加(新按钮);
this.Controls.Add(newButton);
}
}
}
}
如果我能以某种方式将鼠标在窗体上的位置指定给按钮,我可以轻松地修复它。解决此问题的一种方法是使用delta(两个对象之间的差异) 当按下按钮时,您可以将lastMousePosition最初设置为鼠标位置(在释放按钮之前不会再次设置)
注意,像这样反复调用构造函数的性能不是很好。您可能想考虑重用仅一次实例化的点对象。当然,您只需要在所有东西都正常工作后才需要担心:)您可能需要查看上的帮助中心。没有人想翻越代码的大山。代码很短,我用了很多“使用”以防万一,还有空格。现在还不太清楚发布的代码有什么问题(特别是考虑到代码太多)。你说它“跳跃”,然后“可以自由移动”。那到底是什么意思?你能举个例子吗?顺便说一句,谢谢你发布代码,太多总比没有好:)代码可能很短,但格式很重要。我们每天阅读大量的问题,如果仅仅阅读问题就需要10分钟,很多人会跳过它。好吧,你看,我点击它们来移动它们,它们的“原点”成为光标的尖端(我不想要),然后我可以移动它们,但随着内部光标位置的改变,例如,我想,如果我点击按钮,按钮内光标的位置保持不变,然后我移动按钮,按钮内光标的位置保持不变(对不起,如果它听起来很凌乱,我不是英语母语人士)。天哪,它起作用了,你真是个天才!!,这种技术叫什么?我记得我在不久前读过的代码中看到了它,现在我想起来了,但我不知道它是干什么用的。谢谢你,我不知道它有什么特别的名字,但是在游戏编程(以及一般的动态用户界面编程)中,使用delta来移动东西是很常见的。当我这么做的时候,我只是说我在“使用一个delta”。delta,顺便说一句,是一个希腊字母,在各种字段中用于表示对象状态的变化或差异。
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);
}