通过flowLayoutPanel控件可以实现自动排版。
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 demo { public partial class FormMain : Form { public FormMain() { InitializeComponent(); } /// <summary> /// 动态添加图像控件 /// </summary> private void AddPicControl() { PictureBox pic = new PictureBox(); // 设置图像相关属性及事件 pic.Name = string.Format("pic{0}", flowLayoutPanel1.Controls.Count);//Name属性 pic.Width = 100; //Size-Width pic.Height = 100; //Size-Height pic.BorderStyle = BorderStyle.FixedSingle; // 边框样式:单线边 pic.SizeMode = PictureBoxSizeMode.Zoom; // 设置图像SizeMode=Zoom pic.Cursor = Cursors.Hand; // 鼠标悬浮样式:小手 // 向容器中添加一个图像控件 flowLayoutPanel1.Controls.Add(pic); } /// <summary> /// 根据数组动态添加图像控件 /// </summary> private void MakePicControl() { int count = 0; PictureBox[] pic; count = 10; pic = new PictureBox[count]; for (int i = 0; i < count; i++) { pic[i] = new System.Windows.Forms.PictureBox(); pic[i].Width = 100; //Size-Width pic[i].Height = 100; //Size-Height pic[i].BorderStyle = BorderStyle.FixedSingle; //pic[i].Location = new Point(50 + i * 110, 100); pic[i].SizeMode = PictureBoxSizeMode.Zoom; pic[i].Image = Image.FromFile(@"img\" + (i + 1) + ".png"); flowLayoutPanel1.Controls.Add(pic[i]); } } /// <summary> /// 测试 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTest_Click(object sender, EventArgs e) { // flowLayoutPanel添加滚动条 flowLayoutPanel1.AutoScroll = true; AddPicControl(); } /// <summary> /// 批量添加 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMore_Click(object sender, EventArgs e) { // flowLayoutPanel添加滚动条 flowLayoutPanel1.AutoScroll = true; MakePicControl(); } } }