现在随机生成一些PictrueBox 控件,如何把这些pictureBox合理的拼在一个Panel控件里,每个PictrueBox控件不可有重叠的部门,只能是边与边相切。pictrueBox的宽与高可以互换(此为模拟旋转90度)。还有要注意的是PictrueBox必须被Panel包含。
非常感谢您的解答,有答案的话可以回贴或发送到.向无私的好同志致敬
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
RandPictrueBox();
} public void RandPictrueBox()
{
panel1.Controls.Clear(); Random rad = new Random();
List<PictureBox> lst = new List<PictureBox>();
int areas = 0;
while (areas < panel1.Width * panel1.Height)
{
PictureBox picBox = new PictureBox();
picBox.MouseDown += new MouseEventHandler(picBox_MouseDown);
picBox.MouseMove += new MouseEventHandler(picBox_MouseMove);
picBox.MouseUp += new MouseEventHandler(picBox_MouseUp);
picBox.BackColor = Color.FromArgb(rad.Next(0, 255), rad.Next(0, 255), rad.Next(0, 255));
picBox.BorderStyle = BorderStyle.Fixed3D;
picBox.Location = new Point(0, 0);
panel1.Controls.Add(picBox);
picBox.Width = rad.Next(panel1.Width / 7, panel1.Width/3);
picBox.Height = rad.Next(panel1.Height / 5, panel1.Height/3);
areas += picBox.Width * picBox.Height; lst.Add(picBox);
} lst.Sort(delegate(PictureBox pic1, PictureBox pic2) {
int a1 = pic1.Width * pic1.Height;
int a2 = pic2.Width * pic2.Height;
return a2.CompareTo(a1);
}); DoSpellBoard();
this.Text = areas.ToString() + "/" + (this.panel1.Width*this.panel1.Height);
}
public void DoSpellBoard()
{ throw new Exception("请高手赐教");
}
void picBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
allowMove = false;
sPoint = Point.Empty;
}
} void picBox_MouseMove(object sender, MouseEventArgs e)
{
if (allowMove && e.Button == MouseButtons.Left)
{
PictureBox picBox = sender as PictureBox;
picBox.Location = new Point(picBox.Location.X + e.X - sPoint.X, picBox.Location.Y + e.Y - sPoint.Y);
}
} bool allowMove = false;
Point sPoint = Point.Empty;
void picBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
allowMove = true;
sPoint = e.Location;
}
}
}
非常感谢您的解答,有答案的话可以回贴或发送到.向无私的好同志致敬
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
RandPictrueBox();
} public void RandPictrueBox()
{
panel1.Controls.Clear(); Random rad = new Random();
List<PictureBox> lst = new List<PictureBox>();
int areas = 0;
while (areas < panel1.Width * panel1.Height)
{
PictureBox picBox = new PictureBox();
picBox.MouseDown += new MouseEventHandler(picBox_MouseDown);
picBox.MouseMove += new MouseEventHandler(picBox_MouseMove);
picBox.MouseUp += new MouseEventHandler(picBox_MouseUp);
picBox.BackColor = Color.FromArgb(rad.Next(0, 255), rad.Next(0, 255), rad.Next(0, 255));
picBox.BorderStyle = BorderStyle.Fixed3D;
picBox.Location = new Point(0, 0);
panel1.Controls.Add(picBox);
picBox.Width = rad.Next(panel1.Width / 7, panel1.Width/3);
picBox.Height = rad.Next(panel1.Height / 5, panel1.Height/3);
areas += picBox.Width * picBox.Height; lst.Add(picBox);
} lst.Sort(delegate(PictureBox pic1, PictureBox pic2) {
int a1 = pic1.Width * pic1.Height;
int a2 = pic2.Width * pic2.Height;
return a2.CompareTo(a1);
}); DoSpellBoard();
this.Text = areas.ToString() + "/" + (this.panel1.Width*this.panel1.Height);
}
public void DoSpellBoard()
{ throw new Exception("请高手赐教");
}
void picBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
allowMove = false;
sPoint = Point.Empty;
}
} void picBox_MouseMove(object sender, MouseEventArgs e)
{
if (allowMove && e.Button == MouseButtons.Left)
{
PictureBox picBox = sender as PictureBox;
picBox.Location = new Point(picBox.Location.X + e.X - sPoint.X, picBox.Location.Y + e.Y - sPoint.Y);
}
} bool allowMove = false;
Point sPoint = Point.Empty;
void picBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
allowMove = true;
sPoint = e.Location;
}
}
}
先去看
【转帖】我最讨厌的10种帖子!!!再来发帖