在我的代码中,每次按下 button1 时,面板中都会生成一个名为 NOT 的图片框实例。当图像被单击并按住时,可以四处拖动。我的问题是每次按下按钮 1 时,我都希望创建另一个具有相同属性的图片框,这样理论上我可以整天按下按钮 1 并根据需要拖动尽可能多的非图片框对象。到目前为止,一旦按下按钮,只会创建一个 NOT 实例,而无法生成另一个实例。所以基本上如何在每次按下 button1 时创建 NOT 的新唯一实例。
public Form1()
{
InitializeComponent();
Drag();
}
private void button1_Click(object sender, EventArgs e)
{
spawnGate("not");
}
PictureBox NOT = new PictureBox();
private Point startPoint = new Point();
public void Drag()
{
NOT.MouseDown += (ss, ee) =>
{
if (ee.Button == System.Windows.Forms.MouseButtons.Left)
{
startPoint = Control.MousePosition;
}
};
NOT.MouseMove += (ss, ee) =>
{
if (ee.Button == System.Windows.Forms.MouseButtons.Left)
{
Point temp = Control.MousePosition;
Point res = new Point(startPoint.X - temp.X, startPoint.Y - temp.Y);
NOT.Location = new Point(NOT.Location.X - res.X, NOT.Location.Y - res.Y);
startPoint = temp;
}
};
}
public void spawnGate(string type)
{
switch (type)
{
case "not":
NOT.Width = 100;
NOT.Height = 50;
NOT.Image = Properties.Resources.Not_gate;
NOT.SizeMode = PictureBoxSizeMode.Zoom;
workspace.Controls.Add(NOT);
break;
}
}
}
摇曳的蔷薇
杨__羊羊
相关分类