最近由于工作需要,需要设置一个多分屏窗口以便于多分屏播放视频!
思考了一下,大致思路如下:用TableLayoutPanel来划分多个区域,在每个区域中都放入一个PictureBox,用PictureBox来接收摄像机的数据流;
每次选择不同的分屏时,根据数量n*n来设置TableLayoutPanel的行列数量,并向其中添加PictureBox。
多分屏效果图如下:
对于多分屏,通常需要全屏功能,多分为显示屏全屏和程序窗体全屏,其对应的效果如下:
代码如下:
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;
using System.Windows;
namespace MultiCamera
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
split_screen(1);
//记录原来屏幕大小和宽高
Point_Old = new System.Drawing.Point(this.Location.X,this.Location.Y);
Width_Old = this.Width;
Height_old = this.Height;
}
private int curScreenNum = 0;
private bool isFullScreen = false;
private int row = 0;
private int col = 0;
private PictureBox[] pb = new PictureBox[25];
private Point Point_Old = new System.Drawing.Point();
private int Width_Old;
private int Height_old;
private void split_screen(int num)
{
tlp_screen.ColumnStyles.Clear();
tlp_screen.RowStyles.Clear(); //清除行列属性
int i;
for (i = 0; i < curScreenNum; i++)
{
tlp_screen.Controls.Remove(pb[i]);
}
tlp_screen.Refresh();
int Sqrt_num = (int)Math.Sqrt(num);
tlp_screen.ColumnCount = Sqrt_num;
tlp_screen.RowCount = Sqrt_num;
int pb_width = (tlp_screen.Width-6*Sqrt_num) / Sqrt_num; //无法修改picturebox的Margin为0(默认为3),所以可设置空格6*列/行
int pb_height = (tlp_screen.Height-6*Sqrt_num) / Sqrt_num;
for (i = 0; i < num; i++)
{
row = i / Sqrt_num;
col = i % Sqrt_num;
pb[i] = new PictureBox();
pb[i].Tag = i;
pb[i].Click += new System.EventHandler(this.PicClick);
//pb[i].Padding = new Padding(1, 1, 1, 1); //为何不管用?
tlp_screen.Controls.Add(pb[i],col,row);
pb[i].BackColor = Color.FromArgb(50, 20 * (row + col), 40 * row, 10 * col);
pb[i].Location = new System.Drawing.Point(row * pb_width, col * pb_height);
pb[i].Size = new System.Drawing.Size(pb_width,pb_height);
//Console.WriteLine("pb[{0}] w:{1} h:{2} x:{3},y:{4}", i,pb[i].Width, pb[i].Height, pb[i].Location.X, pb[i].Location.Y);
}
curScreenNum = num;
}
private void PicClick(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
pic.BackColor = Color.Blue ;
string PicTag = pic.Tag.ToString();
//MessageBox.Show("I'm PictrueBox["+PicTag+"]");
FullScreen(pic);
switch (PicTag)
{
case "0":
break;
case "1":
break;
case "2":
break;
case "3":
break;
case "4":
break;
case "5":
break;
case "6":
break;
case "7":
break;
case "8":
break;
case "9":
break;
case "10":
break;
case "11":
break;
case "12":
break;
case "13":
break;
case "14":
break;
case "15":
break;
case "16":
break;
case "17":
break;
case "18":
break;
case "19":
break;
case "20":
break;
case "21":
break;
case "22":
break;
case "23":
break;
case "24":
break;
default:
break;
}
}
private void FullScreen(PictureBox pic)
{
//窗体内部全屏
/*
if (!isFullScreen)
{ //Full Screen
isFullScreen = true;
tlp_screen.Controls.Remove(pic);
pic.Parent = this;
pic.BringToFront();
pic.Dock = DockStyle.Fill;
tlp_screen.Hide();
}
else
{//Cancel Full Screen
isFullScreen = false;
tlp_screen.Controls.Add(pic);
pic.Parent = tlp_screen;
pic.Dock = DockStyle.Fill;
tlp_screen.Show();
}
*/
/*窗体外全屏*/
Rectangle rect = new Rectangle();
rect = Screen.GetBounds(this);//获取屏幕大小
if (!isFullScreen)
{ //Full Screen
isFullScreen = true;
this.FormBorderStyle = FormBorderStyle.None; //取消窗体边框
this.Width = rect.Width;
this.Height = rect.Height;
this.Location = new System.Drawing.Point(0, 0);
tlp_screen.Controls.Remove(pic);
pic.Parent = this;
pic.BringToFront();
pic.Dock = DockStyle.Fill;
tlp_screen.Hide();
}
else
{//Cancel Full Screen
isFullScreen = false;
this.FormBorderStyle = FormBorderStyle.Sizable;
this.Location = Point_Old;
this.Height = Height_old;
this.Width = Width_Old;
tlp_screen.Controls.Add(pic);
pic.Parent = tlp_screen;
pic.Dock = DockStyle.Fill;
tlp_screen.Show();
}
}
private void btn_fp_1_Click(object sender, EventArgs e)
{
split_screen(1);
}
private void btn_fp_4_Click(object sender, EventArgs e)
{
split_screen(4);
}
private void btn_fp_9_Click(object sender, EventArgs e)
{
split_screen(9);
}
private void btn_fp_16_Click(object sender, EventArgs e)
{
split_screen(16);
}
private void btn_fp_25_Click(object sender, EventArgs e)
{
split_screen(25);
}
}
}