C# winfrom动态生成panel面板,移动有蒙版和按钮显隐

public static int jj=4;
public static string[,] Chess = new string[jj, 3]; //声明一个二维数组.
//1.面板名
//2.button名
//3.显隐

 private void Form1_Load(object sender, EventArgs e)
        {
            
            int y = 0;
            int num = 0;
            int numy = 0;
            int ys = 1;
            for (int i = 1; i < jj+1; i++)
            {
                int s = i % 2;
                int v = i / 2;
                int x = 2;

                if (i % 2 == 0)//y走两次
                {
                    x = x + (251 + 10) * 1;
                    num++;
                }
                else
                {
                    y = (80* num);
                    numy++;
                }
                Panel panel = new Panel();

                panel.Size = new Size(251, 80);
                panel.Location = new Point(x, ys+(y+10*numy));
                panel.BorderStyle = BorderStyle.FixedSingle;
                panel.MouseEnter += panel_MouseEnter;
                panel.Name = "p" + i;
                panel1.Controls.Add(panel);

                PictureBox pictureBox = new PictureBox();
                pictureBox.Size = new Size(45, 45);
                pictureBox.Location = new Point(13, 17);
                pictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox.Image = Resources.屏幕截图_1_;
               // pictureBox.MouseEnter += pic_MouseEnter;
                pictureBox.Name = "pic_p"+i;
                panel.Controls.Add(pictureBox);

                Button button = new Button();
                button.Size = new Size(75, 23);
                button.Location = new Point(114, 31);
                button.FlatStyle = FlatStyle.Flat;
                button.FlatAppearance.BorderSize = 0;
                button.BackColor = Color.DarkBlue;
                button.Text = "停止";
                button.Name = "button_p" + i;
              //  button.MouseEnter += button_MouseEnter;
                button.Visible = false;
                panel.Controls.Add(button);


                PictureBox pictureBox2 = new PictureBox();
                pictureBox2.Size = new Size(20, 45);
                pictureBox2.Location = new Point(203, 17);
                pictureBox2.Image = Resources.屏幕截图_9_;
                pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
               // pictureBox2.MouseEnter += pic2_MouseEnter;
                pictureBox2.Name = "picx_p" + i;
                panel.Controls.Add(pictureBox2);

                Label lab = new Label();
                lab.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
                lab.Text = "Save";
                //lab.Size = new Size(100, 30);
                lab.Location = new Point(67, 36);
               // lab.MouseEnter += lab_MouseEnter;
                lab.Name = "lab_P" + i;
                panel.Controls.Add(lab);
                


            }
            jkl();
        }
//添加二维数组 
public void jkl()
        {
            int i = 0;
            int j = 0;
            foreach (Control control in this.panel1.Controls)
            {
                if (control is Panel)
                {
                    
                    foreach (Control controls in control.Controls)
                    {
                        if (controls is Button)
                        {

                            Chess[i, j] = control.Name;
                            Chess[i, j + 1] = controls.Name;
                            string s = "0";
                            if (controls.Visible)
                            {
                                s = "1";

                            }
                            Chess[i, j + 2] = s;


                        }
                    }
                    i++;
                }
            }
        }

小面板中

  private void panel_MouseEnter(object sender, EventArgs e)
        {
            Panel panel2 = (Panel)sender;

            for (int i = 0; i < jj; i++)
            {
                Button button = panel1.Controls.Find(Chess[i, 1], true)[0] as Button;
                Panel panel = panel1.Controls.Find(Chess[i, 0], true)[0] as Panel;
               
                if (Chess[i, 0].ToString() == panel2.Name.ToString())
                {
                   if(Chess[i, 2].ToString()=="0")
                        {
                       
                        button.Visible = true;
                        panel.BackColor = Color.AntiqueWhite;
                    }
                }
            }
          
        }

最外部的面板

 private void panel1_MouseEnter(object sender, EventArgs e)
        {
            for (int i = 0; i < jj; i++)
            {
                Button button = panel1.Controls.Find(Chess[i, 1], true)[0] as Button;
                Panel panel = panel1.Controls.Find(Chess[i, 0], true)[0] as Panel;

                panel.BackColor = Color.Transparent;
                button.Visible = false;
            }
        }

效果图

原文地址:https://www.cnblogs.com/xuezhu/p/13546780.html