C#分屏控件用法實例
阿新 • • 發佈:2018-01-02
for border com ace see ex18 pub 背景顏色 num
本文實例中的自定義類PictureBox繼承於UserControl,最終實現簡單的分屏功能。分享給大家供大家參考之用。具體實現代碼如下:
public
partial
class
PictureControl : UserControl
{
public
event
MouseEventHandler PicControl_DoubleClick;
private
int
picNum;
/// <summary>
/// 屏幕個數
/// </summary>
public
int
PicNum
{
get
{
return
picNum; }
set
{
if
(value == 4 || value == 6 || value == 9 || value == 12 || value == 16 || value == 20 || value == 25)
//只能
是4、6、9、12、16、20、25
{
picNum = value;
this
.SetPictureBox(
this
.picNum);
}
else
{
this
.PicNum = 12;
this
.SetPictureBox(
this
.picNum);
}
}
}
public
PictureControl()
{
this
.picNum = 4;
InitializeComponent();
this
.SetPictureBox(
this
.picNum);
}
/// <summary>
/// 根據個數布局PictureBox
/// </summary>
/// <param name="num"></param>
private
void
SetPictureBox(
int
num)
{
this
.Controls.Clear();
Size size =
this
.Size;
switch
(num)
{
case
4:
this
.SetPictureBox(2, 2, size);
break
;
case
6:
this
.SetPictureBox(2, 3, size);
break
;
case
9:
this
.SetPictureBox(3, 3, size);
break
;
case
12:
this
.SetPictureBox(3, 4, size);
break
;
case
16:
this
.SetPictureBox(4, 4, size);
break
;
case
20:
this
.SetPictureBox(4, 5, size);
break
;
case
25:
this
.SetPictureBox(5, 5, size);
break
;
}
}
/// <summary>
/// 布局pictureBox
/// </summary>
/// <param name="x">幾行</param>
/// <param name="y">幾列</param>
/// <param name="size">當前控件的大小</param>
private
void
SetPictureBox(
int
x,
int
y,Size size)
{
int
num = 0;
for
(
int
i = 0; i < x; i++)
{
for
(
int
j = 0; j < y; j++)
{
PictureBox pic =
new
PictureBox();
pic.SizeMode = PictureBoxSizeMode.Zoom;
//設置自動縮放
pic.BackColor = Color.White;
//設置背景顏色
pic.Location =
new
Point((size.Width / y) * j, (size.Height / x) * i);
//設置Location
pic.BorderStyle = BorderStyle.FixedSingle;
//設置邊框
pic.MouseDoubleClick +=
new
MouseEventHandler(pic_MouseDoubleClick);
//訂閱控件雙擊事件
pic.Size =
new
Size(size.Width / y, size.Height / x);
//設置控件大小
pic.Tag = num;
//設定控件編號即屏幕序號
this
.Controls.Add(pic);
//添加
num++;
}
}
}
void
pic_MouseDoubleClick(
object
sender, MouseEventArgs e)
{
if
(
this
.PicControl_DoubleClick !=
null
)
{
this
.PicControl_DoubleClick(sender, e);
//將動態添加的控件的雙擊事件 傳向控件體外。
}
}
private
void
PictureControl_SizeChanged(
object
sender, EventArgs e)
{
this
.SetPictureBox(
this
.picNum);
}
private
PictureBox GetPicByIndex(
int
index)
{
foreach
(Control c
in
this
.Controls)
{
if
(Convert.ToInt32(c.Tag) == index)
{
return
(PictureBox)c;
}
}
PictureBox p =
new
PictureBox();
p.Tag = -1;
return
p;
}
/// <summary>
/// 根據屏幕序號設置圖像
/// </summary>
/// <param name="index">屏幕號</param>
/// <param name="img">圖像</param>
public
void
SetImageByIndex(
int
index, Image img)
{
GetPicByIndex(index).Image = img;
}
}
public
partial
class
PictureControl : UserControl
{
public
event
MouseEventHandler PicControl_DoubleClick;
private
int
picNum;
/// <summary>
/// 屏幕個數
/// </summary>
public
int
PicNum
{
get
{
return
picNum; }
set
{
if
(value == 4 || value == 6 || value == 9 || value == 12 || value == 16 || value == 20 || value == 25)
//只能
是4、6、9、12、16、20、25
{
picNum = value;
this
.SetPictureBox(
this
.picNum);
}
else
{
this
.PicNum = 12;
this
.SetPictureBox(
this
.picNum);
}
}
}
public
PictureControl()
{
this
.picNum = 4;
InitializeComponent();
this
.SetPictureBox(
this
.picNum);
}
/// <summary>
/// 根據個數布局PictureBox
/// </summary>
/// <param name="num"></param>
private
void
SetPictureBox(
int
num)
{
this
.Controls.Clear();
Size size =
this
.Size;
switch
(num)
{
case
4:
this
.SetPictureBox(2, 2, size);
break
;
case
6:
this
.SetPictureBox(2, 3, size);
break
;
case
9:
this
.SetPictureBox(3, 3, size);
break
;
case
12:
this
.SetPictureBox(3, 4, size);
break
;
case
16:
this
.SetPictureBox(4, 4, size);
break
;
case
20:
this
.SetPictureBox(4, 5, size);
break
;
case
25:
this
.SetPictureBox(5, 5, size);
break
;
}
}
/// <summary>
/// 布局pictureBox
/// </summary>
/// <param name="x">幾行</param>
/// <param name="y">幾列</param>
/// <param name="size">當前控件的大小</param>
private
void
SetPictureBox(
int
x,
int
y,Size size)
{
int
num = 0;
for
(
int
i = 0; i < x; i++)
{
for
(
int
j = 0; j < y; j++)
{
PictureBox pic =
new
PictureBox();
pic.SizeMode = PictureBoxSizeMode.Zoom;
//設置自動縮放
pic.BackColor = Color.White;
//設置背景顏色
pic.Location =
new
Point((size.Width / y) * j, (size.Height / x) * i);
//設置Location
pic.BorderStyle = BorderStyle.FixedSingle;
//設置邊框
pic.MouseDoubleClick +=
new
MouseEventHandler(pic_MouseDoubleClick);
//訂閱控件雙擊事件
pic.Size =
new
Size(size.Width / y, size.Height / x);
//設置控件大小
pic.Tag = num;
//設定控件編號即屏幕序號
this
.Controls.Add(pic);
//添加
num++;
}
}
}
void
pic_MouseDoubleClick(
object
sender, MouseEventArgs e)
{
if
(
this
.PicControl_DoubleClick !=
null
)
{
this
.PicControl_DoubleClick(sender, e);
//將動態添加的控件的雙擊事件 傳向控件體外。
}
}
private
void
PictureControl_SizeChanged(
object
sender, EventArgs e)
{
this
.SetPictureBox(
this
.picNum);
}
private
PictureBox GetPicByIndex(
int
index)
{
foreach
(Control c
in
this
.Controls)
{
if
(Convert.ToInt32(c.Tag) == index)
{
return
(PictureBox)c;
}
}
PictureBox p =
new
PictureBox();
p.Tag = -1;
return
p;
}
/// <summary>
/// 根據屏幕序號設置圖像
/// </summary>
/// <param name="index">屏幕號</param>
/// <param name="img">圖像</param>
public
void
SetImageByIndex(
int
index, Image img)
{
GetPicByIndex(index).Image = img;
}
}
C#分屏控件用法實例