如何在C#中在运行时在winForms上拖动图片框? [英] How to drag a picture box on winForms at runtime in C#?

查看:90
本文介绍了如何在C#中在运行时在winForms上拖动图片框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想创建一个表单,用户可以用鼠标将控件(pictureBox)从一侧拖动到另一侧...

我搜索但是找不到解决方案...

我的问题是如何在窗体上获取鼠标位置(X& Y)(鼠标时)在控制中)???



在此先感谢...



问候:

Jayanta ..

Hi,
I want to create a form on which user can drag the control(pictureBox)from one side to another side with the mouse...
I searched but I couldn't find the solution...
My problem is how can I get the mouse position(X & Y)on the form(when the mouse is on the control)???

Thanks in Advance...

Regards:
Jayanta..

推荐答案

然后你真的没有那么努力。我真的很讨厌自我推销,但在这里:创建自己的运行时可移动的Windows窗体控件 [ ^ ]
Then you really didn't search that hard. I really hate self-promotion, but here: Create your Own Runtime Movable Windows Forms Controls[^]


#region在运行时移动图片

private void pictureBox1_MouseDown(object sender,MouseEventArgs e)

{

isDragging = true;



currentX = eX;

currentY = eY;

}



private void pictureBox1_MouseMove(object sender,MouseEventArgs e)

{

if(isDragging)

{

pictureBox1.Top = pictureBox1.Top +(eY - currentY);

pictureBox1.Left = pictureBox1 .Left +( eX - currentX);

}

}



private void pictureBox1_MouseUp(object sender,MouseEventArgs e)

{

isDragging = false;

}

#endregion
#region move picture at runtime
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;

currentX = e.X;
currentY = e.Y;
}

private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
pictureBox1.Top = pictureBox1.Top + (e.Y - currentY);
pictureBox1.Left = pictureBox1.Left + (e.X - currentX);
}
}

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
#endregion


这篇关于如何在C#中在运行时在winForms上拖动图片框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆