我的代码有问题。我正在尝试在我的表单上拖放图片,但是当我移动选定的图片框时,当我离开组框时它会丢失。它只是消失了。
public partial class Form1 : Form
{
int x_offset = 0; // any better to do this without having a global variable?
int y_offset = 0;
PictureBox dpb = new PictureBox();
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Maximized;
this.AllowDrop = true;
this.pictureBox1.MouseDown += pictureBox1_MouseDown;
this.pictureBox2.MouseDown += pictureBox2_MouseDown;
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
PictureBox me = (PictureBox)sender;
x_offset = e.X;
y_offset = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox me = (PictureBox)sender;
me.Left = e.X + me.Left - x_offset;
me.Top = e.Y + me.Top - y_offset;
}
}