⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.cs

📁 CSharp写的一个翻牌游戏
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace fpGame
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class frmMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
        private MyButton[,] _btns=new MyButton[5,5]; 
		public frmMain()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.panel1 = new System.Windows.Forms.Panel();
			this.button1 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Location = new System.Drawing.Point(24, 16);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(432, 312);
			this.panel1.TabIndex = 0;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(24, 344);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(80, 24);
			this.button1.TabIndex = 1;
			this.button1.Text = "button1";
			// 
			// frmMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(480, 445);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.panel1);
			this.Name = "frmMain";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.frmMain_Load);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new frmMain());
		}

		private void frmMain_Load(object sender, System.EventArgs e)
		{
			for(int i=0;i<5;i++)
			{
				for(int j=0;j<5;j++)
				{
					MyButton mb=new MyButton();
					mb.PosX=i;
					mb.PosY=j;
					mb.Click+=new EventHandler(btn_Click);
					mb.BackColor=Color.White;
					_btns[i,j]=mb;
					mb.Width=this.panel1.Width/5;
					mb.Height=this.panel1.Height/5;
					mb.Left=this.panel1.Width/5*i;
					mb.Top=this.panel1.Height/5*j;
					this.panel1.Controls.Add(mb);
					
				
				}
			}
		}
		private void btn_Click(object sender, System.EventArgs e)
		{
			
			MyButton mb=(MyButton)sender;//获取当前被点击的按钮
			ReColor(mb.PosX,mb.PosY);
			ReColor(mb.PosX-1,mb.PosY);
			ReColor(mb.PosX+1,mb.PosY);
			ReColor(mb.PosX,mb.PosY-1);
			ReColor(mb.PosX,mb.PosY+1);
           
		}
		private void ReColor(int x,int y)//根据按钮的索引修改按钮的颜色,如果是黑色则改成白色,如果是白色则改成黑色
		{
			if((x>=0&&x<5)&&(y>=0&&y<5))
			{
				if(_btns[x,y].BackColor==Color.White)
					_btns[x,y].BackColor=Color.Black;
				else
					_btns[x,y].BackColor=Color.White;
				
			}
		}

		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -