C split screen control usage instance

  • 2020-09-16 07:45:25
  • OfStack

The custom class PictureBox in the example in this article inherits from UserControl and ends up with simple split screen functionality. To share with you for your reference. The specific implementation code is as follows:


public partial class PictureControl : UserControl
{
    public event MouseEventHandler PicControl_DoubleClick;
    private int picNum;
    /// <summary>
    ///  Number of screens 
    /// </summary>
    public int PicNum
    {
      get { return picNum; }
      set
      {
        if (value == 4 || value == 6 || value == 9 || value == 12 || value == 16 || value == 20 || value == 25)// only 

 is 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>
    ///  Layout by number 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>
    ///  layout pictureBox
    /// </summary>
    /// <param name="x"> A few lines </param>
    /// <param name="y"> A few columns </param>
    /// <param name="size"> Size of the current control </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;                     // Set auto scaling 
          pic.BackColor = Color.White;                          // Set the background color 
          pic.Location = new Point((size.Width / y) * j, (size.Height / x) * i);     // Set up the Location
          pic.BorderStyle = BorderStyle.FixedSingle;                   // Set the border 
          pic.MouseDoubleClick += new MouseEventHandler(pic_MouseDoubleClick);      // Subscribe to a double-click event for the control 
          pic.Size = new Size(size.Width / y, size.Height / x);              // Set the control size    
          pic.Tag = num;                                 // Set the control number to screen number 
          this.Controls.Add(pic);                             // add 
          num++;
        }
      }
    }
    void pic_MouseDoubleClick(object sender, MouseEventArgs e)
    {
      if (this.PicControl_DoubleClick != null)
      {
        this.PicControl_DoubleClick(sender, e);// The double-click event of a control that will be added dynamically   To the outside of the control. 
      }
    }
    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>
    ///  Set the image according to the screen sequence number 
    /// </summary>
    /// <param name="index"> The screen no. </param>
    /// <param name="img"> image </param>
    public void SetImageByIndex(int index, Image img)
    {
      GetPicByIndex(index).Image = img;
    }
}

I hope the examples in this article have been helpful in C# programming.


Related articles: