C#中如何让鼠标按下拖拉时,自己的一个图片跟着动。急着知道,有高手帮下忙哦。
谢谢这位大侠,我要实现的是在pictureBox外点击一个按钮,将按钮的Iamage拖到pictureBox的指定位置显示,有没有比较详细的解决方法,谢谢各位了...
谢谢这位大侠,我要实现的是在pictureBox外点击一个按钮,将按钮的Iamage拖到pictureBox的指定位置显示,有没有比较详细的解决方法,谢谢各位了
展开
1个回答
展开全部
拖到按钮代码一样,在放掉的时候根据位置控制一下
这是一种方法,不知道能不能用DragDrop事件来实现,我没试出来
Point btpt;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
btpt = Cursor.Position;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int px = Cursor.Position.X - btpt.X;
int py = Cursor.Position.Y - btpt.Y;
button1.Location = new Point(button1.Location.X + px, button1.Location.Y + py);
btpt = Cursor.Position;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
//如果按钮在PictureBox内则将按钮背景图片给PictureBox
if (button1.Location.X > pictureBox1.Location.X && button1.Location.X < pictureBox1.Location.X+pictureBox1.Width)
if (button1.Location.Y > pictureBox1.Location.Y && button1.Location.Y < pictureBox1.Location.Y + pictureBox1.Height)
pictureBox1.Image = button1.Image;
}
这是一种方法,不知道能不能用DragDrop事件来实现,我没试出来
Point btpt;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
btpt = Cursor.Position;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int px = Cursor.Position.X - btpt.X;
int py = Cursor.Position.Y - btpt.Y;
button1.Location = new Point(button1.Location.X + px, button1.Location.Y + py);
btpt = Cursor.Position;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
//如果按钮在PictureBox内则将按钮背景图片给PictureBox
if (button1.Location.X > pictureBox1.Location.X && button1.Location.X < pictureBox1.Location.X+pictureBox1.Width)
if (button1.Location.Y > pictureBox1.Location.Y && button1.Location.Y < pictureBox1.Location.Y + pictureBox1.Height)
pictureBox1.Image = button1.Image;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询