1

这里是缝。我单击一个按钮并创建一个新的 PictureBox 没问题。当我单击并拖动时,我将图片移动到它的新位置。现在,当我再次单击该按钮时,我创建了同一个 PictureBox 的新实例,当我尝试移动旧的时,我最终移动了新创建的框。我认为这是因为它们都具有相同的名称:

PictureBox pic = new PictureBox();

如何通过单击在两个图片框之间切换?

*更新* 感谢 Nilotpal 的回答,我设法解决了上述问题。唯一的问题是图片框现在似乎在晃动,或者在另一个实例和我正在拖动的那个实例之间来回切换位置。无论哪种方式,我真的不确定如何解决这个问题。有任何想法吗?

*更新* 我拥有的代码:

 private void code128ToolStripMenuItem_Click(object sender, EventArgs e)
    {


        bNum++;
        Barcode barcode = new Barcode();


        pic = new PictureBox();
        pic.Name = "bCode" + bNum;
        pic.SizeMode = PictureBoxSizeMode.AutoSize;
        pic.Image = barcode.createBarcode(BarcodeLib.TYPE.CODE128, 300, 100, "123456789");

        pic.Show();
        labelHolder.Controls.Add(pic);
        pic.BringToFront();
        pic.MouseDown += pic_MouseDown;
        pic.MouseMove +=pic_MouseMove;
        pic.MouseUp += pic_MouseUp;
    }



    PictureBox thisPB;
     private void pic_MouseDown(object sender, MouseEventArgs e)
    {


        mouseDown = true;

        oldX = e.X;
        oldY = e.Y;


    }


    private void pic_MouseMove(object sender, MouseEventArgs e)
    {
        if(mouseDown)
        {



                thisPB = (PictureBox)sender;
                thisPB.Location = new Point(pic.Location.X - (oldX - e.X), pic.Location.Y - (oldY - e.Y));


                this.Refresh();


        }




    }


    private void pic_MouseUp(object sender, MouseEventArgs e)
    {

        mouseDown = false;

    }
4

3 回答 3

2
    private void button1_Click(object sender, EventArgs e)
    {
        PictureBox pb = new PictureBox();
        pb.Top = 200;
        pb.Left = 200;
        pb.BackColor = Color.Gray;
        pb.MouseMove += new MouseEventHandler(pb_MouseMove);
        this.Controls.Add(pb);
    }

    void pb_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            PictureBox thisPB = (PictureBox)sender;
            thisPB.Left = e.X;
            thisPB.Top = e.Y;
        }
    }

移动会不稳定,您可以根据需要进行更改。

于 2012-11-01T12:59:37.293 回答
2

修复!

旧代码:

Barcode barcode = new Barcode();
        pic = new PictureBox();
        pic.Name = "bCode" + bNum;
        pic.SizeMode = PictureBoxSizeMode.AutoSize;
        pic.Image = barcode.createBarcode(BarcodeLib.TYPE.CODE128, 300, 100, "123456789");

        pic.Show();
        labelHolder.Controls.Add(pic);
        pic.BringToFront();
        pic.MouseDown += pic_MouseDown;
        pic.MouseMove +=pic_MouseMove;
        pic.MouseUp += pic_MouseUp;
    }

    PictureBox thisPB;
    private void pic_MouseDown(object sender, MouseEventArgs e)
    {

        mouseDown = true;

        oldX = e.X;
        oldY = e.Y; }


    private void pic_MouseMove(object sender, MouseEventArgs e)
    {
        if(mouseDown)
        {
thisPB.Location = new Point(pic.Location.X - (oldX - e.X), pic.Location.Y - (oldY - e.Y));
                this.Refresh();

        }
    }


    private void pic_MouseUp(object sender, MouseEventArgs e)
    {
        mouseDown = false;
    }

工作代码:

Barcode barcode = new Barcode();

        pic = new PictureBox();
        pic.Name = "bCode" + bNum;
        pic.SizeMode = PictureBoxSizeMode.AutoSize;
        pic.Image = barcode.createBarcode(BarcodeLib.TYPE.CODE128, 300, 100, "123456789");

        pic.Show();
        labelHolder.Controls.Add(pic);
        pic.BringToFront();
        pic.MouseDown += pic_MouseDown;
        pic.MouseMove +=pic_MouseMove;
        pic.MouseUp += pic_MouseUp;
    }


    PictureBox thisPB;
    private void pic_MouseDown(object sender, MouseEventArgs e)
    {             
        mouseDown = true;

        oldX = e.X;
        oldY = e.Y;           
    }


    private void pic_MouseMove(object sender, MouseEventArgs e)
    {
        if(mouseDown)
        {             
                thisPB = (PictureBox)sender;
                thisPB.Location = new Point(thisPB.Location.X - (oldX - e.X), thisPB.Location.Y - (oldY - e.Y));

                this.Refresh();                
        }            
    }


    private void pic_MouseUp(object sender, MouseEventArgs e)
    {
        mouseDown = false;           
    }
于 2012-11-01T13:26:15.110 回答
1

您可能会发现这很有帮助。

static class ExtensionMethods
{
    public static Point Add(this Point original, Point value)
    {
        return new Point(original.X + value.X, original.Y + value.Y);
    }

    public static Point Subtract(this Point original, Point value)
    {
        return new Point(original.X - value.X, original.Y - value.Y);
    }
}
于 2015-09-02T09:04:51.390 回答