程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#分屏控件用法實例

C#分屏控件用法實例

編輯:C#入門知識

C#分屏控件用法實例。本站提示廣大學習愛好者:(C#分屏控件用法實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#分屏控件用法實例正文


本文實例中的自界說類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;
    }
}

願望本文所述實例對年夜家C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved