/// <summary> /// 调整画面布局 /// </summary> /// <param name="videoNum"></param> /// <returns></returns> private bool InitializeVideo(int videoNum) { //计算行列 int rowcol; if (videoNum <= 0 || !int.TryParse(Math.Sqrt(videoNum).ToString(), out rowcol)) { return false; } //计算宽高 int WidthHeight = (int)(MainPanel.Width / rowcol); //重新设置表格 //MainPanel为TableLayoutPanel控件 MainPanel.Controls.Clear(); MainPanel.RowCount = MainPanel.ColumnCount = rowcol; MainPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single; MainPanel.Refresh(); for (int i = 0; i < MainPanel.ColumnStyles.Count; i++) { MainPanel.ColumnStyles[i].SizeType = SizeType.Absolute; MainPanel.ColumnStyles[i].Width = WidthHeight; } for (int i = 0; i < MainPanel.RowStyles.Count; i++) { MainPanel.RowStyles[i].SizeType = SizeType.Absolute; MainPanel.RowStyles[i].Height = WidthHeight; } //添加控件 for (int i = 0; i < videoNum; i++) { PictureBox pVideo = new PictureBox(); pVideo.Padding = pVideo.Margin = new Padding(0); pVideo.Name = "pVideo" + i.ToString(); pVideo.Width = pVideo.Height = WidthHeight; pVideo.Dock = DockStyle.Fill; pVideo.BackgroundImage = Resources.bg; pVideo.BackgroundImageLayout = ImageLayout.Stretch; pVideo.Click += new EventHandler(pVideo_Click); MainPanel.Controls.Add(pVideo, i % rowcol, i / rowcol); } return true; } /// <summary> /// 点击图像事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void pVideo_Click(object sender, EventArgs e) { PictureBox pVideo = (PictureBox)sender; //MessageBox.Show(pVideo.Name); if (MainPanel.GetColumnSpan(pVideo) == 1) { //隐藏其它控件 foreach (Control ctr in MainPanel.Controls) { if (ctr.Name != pVideo.Name) ctr.Visible = false; } pos = MainPanel.GetPositionFromControl(pVideo); MainPanel.SetCellPosition(pVideo, new TableLayoutPanelCellPosition(0, 0)); MainPanel.SetRowSpan(pVideo, MainPanel.RowCount); MainPanel.SetColumnSpan(pVideo, MainPanel.ColumnCount); } else { //显示所有控件 foreach (Control ctr in MainPanel.Controls) { ctr.Visible = true; } MainPanel.SetCellPosition(pVideo, pos); MainPanel.SetRowSpan(pVideo, 1); MainPanel.SetColumnSpan(pVideo, 1); } }