/// <summary> /// 生成雷阵的界面,即生成LeiButton控件(继承于Button控件) /// </summary> private void Leizheng() { for (int i = 0; i < lie; i++) { for (int j = 0; j < hang; j++) { button[j, i] = new LeiButton(); button[j, i].Location = new Point(3 + i * 30, 6 + j * 30); button[j, i].X = j; button[j, i].Y = i; button[j, i].Youlei = 0; button[j, i].Font = new System.Drawing.Font("宋体", button[j, i].Font.Size, button[j, i].Font.Style); groupBox1.Controls.Add(button[j, i]); button[j, i].MouseUp += new MouseEventHandler(bt_MouseUp); // .Location = new Point(30+i * 30, 30+j * 30); // bt.X = j; bt.Y = i; bt.Youlei = 0; // button[j, i] = bt; // //bt.Text = i.ToString() + "" + j.ToString(); //bt.MouseUp+=new MouseEventHandler(bt_MouseUp); // this.Controls.Add(bt); } } }
/// <summary> /// 定义鼠标单击事件,单击按钮时触发该事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void bt_MouseUp(object sender, MouseEventArgs e) { if (!over) { int x, y; //获取被点击的Button按钮 LeiButton b = (LeiButton)sender; x = b.X; //x代表button数组的第一个索引 y = b.Y; //y表示Button数组的第二个索引 //判断按下的鼠标键是哪个 switch (e.Button) { //按下鼠标左键 case MouseButtons.Left: //判断该方格是否被翻开,Tag=0表示方格未被翻开 if (Convert.ToInt16(button[x, y].Tag) == 0) { if (button[x, y].Youlei == 0) { button[x, y].Enabled = false; button[x, y].Text = Getdilei(x, y).ToString(); Saolei(x, y); if (Win()) { Showlei(); timer1.Enabled = false; SoundPlayer sound = new SoundPlayer(shengli); sound.Play(); MessageBox.Show("恭喜你扫雷成功!游戏结束!", "扫雷完成"); over = true; } } else { button[x, y].BackgroundImage = Image.FromFile(shibai); SoundPlayer sound = new SoundPlayer(shengyin); sound.Play(); timer1.Enabled = false; b.Enabled = false; b.BackgroundImage = Image.FromFile(shibai); xianshi(); //button1.Image = Image.FromFile(siqu); //button1.ImageAlign = ContentAlignment.MiddleRight; MessageBox.Show("回家练练运气再来!", "游戏失败"); over = true; } } break; case MouseButtons.Right: if (Convert.ToInt16(button[x, y].Tag) == 1) { button[x, y].Tag = 2; button[x, y].BackgroundImage = Image.FromFile(wenhao); } else if (Convert.ToInt16(button[x, y].Tag) == 2) { button[x, y].Tag = 0; restlie++; button[x, y].BackgroundImage = null; } else { button[x, y].Tag = 1; button[x, y].BackgroundImage = Image.FromFile(flag); restlie--; } lei.Text = " " + restlie.ToString() + "颗"; if (Win()) { SoundPlayer sound = new SoundPlayer(shengli); sound.Play(); MessageBox.Show("恭喜你!你太有才了,扫雷成功", "扫雷完成"); timer1.Enabled = false; over = true; } break; } } else { return; } }