VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之C#分屏控件用法实例

本文实例中的自定义类PictureBox继承于UserControl,最终实现简单的分屏功能。分享给大家供大家参考之用。具体实现代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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#程序设计有所帮助。


相关教程