将图片框声明为单个对象并更改其位置

时间:2019-05-23 15:01:23

标签: c# .net

我希望能够声明多个具有不同位置,背景色和标签的单独图片框。我想使用标记来引用代码中的图片框,以分别更改和/或删除它们。 我该怎么办?

我试图创建一个单独的类,该类用于创建与公共静态相同的图片框,但是遇到trbRed.Value值的一些问题,因为它们在该类中不存在。我不确定该解决方案是否可以解决我的问题,而且我也不知道如何以任何一种方式创建类。

此程序使用拖放操作,因此使用鼠标位置 创建具有不同背景色的图片框。

picBlock在类的前面声明为 PictureBox picBlock = new PictureBox();

private void myPanel_MouseMove(object sender, MouseEventArgs e)
        {
            int mouseX = e.X / 25 + 1;
            int mouseY = (myPanel.Size.Height - e.Y) / 25 + 1;

            if (myPanel.BackgroundImage != null)
            {
                lblMousePos.Show();
                lblMousePos.Text = "pos (" + mouseX + ", " + mouseY + ")";
            }
        }

public void myPanel_DragDrop(object sender, DragEventArgs e)
{
    picBlock = new PictureBox
    {
        BackColor = Color.FromArgb(255, trbRed.Value, 
        trbGreen.Value, trbBlue.Value),

        Image = Image.FromFile("my picture here.png"),

        Size = new Size(25, 24),

        Location = new Point((mouseX - 1) * 25, 
        this.myPanel.Height - mouseY * 24),

        SizeMode = PictureBoxSizeMode.Zoom,

        Tag = mouseX.ToString() + ", " + mouseY.ToString()
    };

    listX_pos.Add(mouseX);
    listY_pos.Add(mouseY);

    picBlock.MouseMove += picBlock_mousemove;
    picBlock.MouseDown += picBlock_mousedown;

    this.myPanel.Controls.Add(picBlock);
}

public void picBlock_mousedown (object sender, MouseEventArgs e)
        {
            this.myPanel.Controls.Remove(picBlock);
            for (int i = 0; i < listX_pos.Count; i++)
            {
                if (listX_pos[i] == ((picBlock.Location.X / 25) + 1) && listY_pos[i] == (this.myPanel.Location.Y - picBlock.Location.Y / 25) - 11)
                {
                    listX_pos.RemoveAt(i);
                    listY_pos.RemoveAt(i);
                }
            }
//pcbPicture is a picturebox with the same content of "my picture here.png"
            picBlock.DoDragDrop(pcbPicture.Image, DragDropEffects.Move);
        }

public void picBlock_mousemove(object sender, MouseEventArgs e)
        {
            if (myPanel.BackgroundImage != null)
            {
//a label to show the position of the mouse
                lblMousePos.Show();
                Point point = myPanel.PointToClient(MousePosition);
                int mouseX = point.X / 25 + 1;
                int mouseY = (myPanel.Size.Height - point.Y) / 25 + 1;
                lblMousePos.Text = "pos (" + mouseX + ", " + mouseY + ")";
            }
        }

创建图片框没有问题,但是只能从picBlock_mousemovepicBlock_mousedown空隙更改最近创建的图片框的位置。
我希望能够更改任何已创建的图片框以及它们自己的控件(mousemove和mousedown)的位置。

1 个答案:

答案 0 :(得分:1)

正如@rene所说,您的问题是在picBlock鼠标处理程序中,您引用的picBlock仅包含最后创建的图像。

解决方法就在其中。 .NET中的每个事件都有两个属性,按照约定:object senderSystem.EventArgs e

e更加不稳定;这是一个由System.EventArgs(或本身)驱动的类,其中包含有关事件的一些必需信息:按下的鼠标按钮/键盘键,鼠标X和Y,等等-任何相关信息。

sender的可用性较低。它包含触发事件的对象。

为什么?我们不能直接访问它,例如通过成员变量访问它吗?

答案是您的(和类似的)情况:我们将单个方法附加到某些对象的事件,然后我们想知道在哪个元素上工作。例如,一个简单的示例,每次您单击按钮时,都会弹出一个消息框,其内容如下:

private void Form_Load(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        var btn = new Button
        {
            Text = "Button #" + i, Top = i * 50, Left = 0
        };
        btn.Click += btn_Click;
        this.Controls.Add(btn);
    }
}

private void btn_Clicked(object sender, EventArgs e)
{
    MessageBox.Show(((Button)sender).Text); // Access the clicked button by `sender`
}

回到您的问题,像使用它一样(您不需要成员变量picBlock):

private void myPanel_MouseMove(object sender, MouseEventArgs e)
{
        if (myPanel.BackgroundImage != null)
        {
            lblMousePos.Show();
            lblMousePos.Text = "pos (" + (e.X / 25 + 1) + ", " + ((myPanel.Size.Height - e.Y) / 25 + 1) + ")";
        }
}

public void myPanel_DragDrop(object sender, DragEventArgs e)
{
    var picBlock = new PictureBox
    {
        BackColor = Color.FromArgb(255, trbRed.Value, 
        trbGreen.Value, trbBlue.Value),

        Image = Image.FromFile("my picture here.png"),

        Size = new Size(25, 24),

        Location = new Point((mouseX - 1) * 25, 
        this.myPanel.Height - mouseY * 24),

        SizeMode = PictureBoxSizeMode.Zoom,

        //Tag = musX.ToString() + ", " + musY.ToString()
        // You don't need the Tag property
    };

    picBlock.MouseMove += picBlock_mousemove;
    picBlock.MouseDown += picBlock_mousedown;

    this.myPanel.Controls.Add(picBlock);
}

public void picBlock_mousedown (object sender, MouseEventArgs e)
{
        var picBlock = (PictureBlock)sender;
        this.panelSpel.Controls.Remove(picBlock);
        for (int i = 0; i < listX_pos.Count; i++)
        {
            if (listX_pos[i] == ((picBlock.Location.X / 25) + 1) && listY_pos[i] == (this.myPanel.Location.Y - picBlock.Location.Y / 25) - 11)
            {
                listX_pos.RemoveAt(i);
                listY_pos.RemoveAt(i);
            }
        }
        //pcbPicture is a picturebox with the same content of "my picture here.png"
        picBlock.DoDragDrop(pcbPicture.Image, DragDropEffects.Move);
}

public void picBlock_mousemove(object sender, MouseEventArgs e)
{
        var picBlock = (PictureBlock)sender;
        if (myPanel.BackgroundImage != null)
        {
            //a label to show the position of the mouse
            lblMousePos.Show();
            Point point = myPanel.PointToClient(MousePosition);
            int mouseX = point.X / 25 + 1;
            int mouseY = (myPanel.Size.Height - point.Y) / 25 + 1;
            lblMousePos.Text = "pos (" + musX + ", " + musY + ")";
        }
}