我希望能够移动在运行时创建的矩形。因为它们不是控件,所以我不确定如何让它们移动。用户输入他们想要的矩形(热点)的数量,然后当他们点击button2时,他们被绘制到panel2上。
我的代码应该允许我在面板之间移动其他控件(按钮等),但我只能在创建控件的面板上拖放。
如果您能解决这两个问题,我将不胜感激
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
Rectangle[] hotspotArray = new Rectangle[0];
List<Rectangle> hotspotList = new List<Rectangle>();
private Rectangle rec;
private Point MouseDownLocation;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = true;
panel1.AllowDrop = true;
panel2.AllowDrop = true;
panel1.DragEnter += new DragEventHandler(panel1_DragEnter);
panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
panel1.DragOver += new DragEventHandler(panel1_DragOver);
panel2.DragEnter += new DragEventHandler(panel1_DragEnter);
panel2.DragDrop += new DragEventHandler(panel1_DragDrop);
panel2.DragOver += new DragEventHandler(panel1_DragOver);
numericUpDown1.ValueChanged += new EventHandler(numericUpDown1_ValueChanged);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
OpenFileDialog myFile = new OpenFileDialog();
myFile.Filter = "Image Files(*.img, *.bmp) |*.img; *.bmp;";
if (myFile.ShowDialog() == DialogResult.OK)
{
panel2.BackgroundImage = Image.FromFile(myFile.FileName);
}
}
catch (Exception error)
{
MessageBox.Show("Error loading the selected file. Original error: " + error.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (numericUpDown1.Value != 0)
{
int num = (int)numericUpDown1.Value;
createNewRectangles(num);
}
}
private void Control_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.All);
}
void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = sender as Control;
c = (Button)e.Data.GetData(typeof(Button));
if (c != null)
{
c.Location = new Point(e.X, e.Y);
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
public void createNewRectangles(int numRecs)
{
Point recLoc = new Point(10, 10);
Pen p = new Pen(Color.Red);
for (int loop = 0; loop < numericUpDown1.Value; loop++)
{
rec = new Rectangle(100, 100, 100, 100);
rec.Location = recLoc;
hotspotList.Add(rec);
recLoc.X = recLoc.X + 10;
recLoc.Y = recLoc.Y + 10;
}
hotspotArray = hotspotList.ToArray();
panel2.CreateGraphics().DrawRectangles(p, hotspotArray);
Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
rec.Location = new Point((e.X - MouseDownLocation.X) + rec.Left, (e.Y - MouseDownLocation.Y) + rec.Top);
MouseDownLocation = e.Location;
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
rec.Location = MouseDownLocation;
}
}
}