📄 frmmain.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace FiveStones
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem menuItem10;
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.MenuItem mnuClose;
private System.Windows.Forms.MenuItem mnuNew;
private System.Windows.Forms.MenuItem mnuComputerFirst;
private System.Windows.Forms.MenuItem mnuPersonFirst;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Label lblBlackPlayer;
private System.Windows.Forms.Label lblWhitePlayer;
private System.Windows.Forms.ListBox lstHistory;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.Label lblXY;
private System.Windows.Forms.MenuItem mnuAbout;
private System.Windows.Forms.MenuItem mnuHelp;
//定义棋盘类
private Chessboard chessboard;
public frmMain()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
//构造棋盘类
System.Drawing.Graphics g = this.CreateGraphics();
chessboard = new Chessboard(g);
}
/// <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()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain));
this.mainMenu = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.mnuNew = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.mnuPersonFirst = new System.Windows.Forms.MenuItem();
this.mnuComputerFirst = new System.Windows.Forms.MenuItem();
this.menuItem10 = new System.Windows.Forms.MenuItem();
this.mnuClose = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.mnuHelp = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.mnuAbout = new System.Windows.Forms.MenuItem();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.lstHistory = new System.Windows.Forms.ListBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.lblWhitePlayer = new System.Windows.Forms.Label();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.lblBlackPlayer = new System.Windows.Forms.Label();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.lblXY = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.groupBox3.SuspendLayout();
this.SuspendLayout();
//
// mainMenu
//
this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem2});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuNew,
this.menuItem7,
this.mnuPersonFirst,
this.mnuComputerFirst,
this.menuItem10,
this.mnuClose});
this.menuItem1.Text = "游戏(&G)";
//
// mnuNew
//
this.mnuNew.Index = 0;
this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.F2;
this.mnuNew.Text = "开始";
this.mnuNew.Click += new System.EventHandler(this.mnuNew_Click);
//
// menuItem7
//
this.menuItem7.Index = 1;
this.menuItem7.Text = "-";
//
// mnuPersonFirst
//
this.mnuPersonFirst.Checked = true;
this.mnuPersonFirst.Index = 2;
this.mnuPersonFirst.Text = "玩家先";
this.mnuPersonFirst.Click += new System.EventHandler(this.mnuPersonFirst_Click);
//
// mnuComputerFirst
//
this.mnuComputerFirst.Index = 3;
this.mnuComputerFirst.Text = "电脑先";
this.mnuComputerFirst.Click += new System.EventHandler(this.mnuComputerFirst_Click);
//
// menuItem10
//
this.menuItem10.Index = 4;
this.menuItem10.Text = "-";
//
// mnuClose
//
this.mnuClose.Index = 5;
this.mnuClose.Text = "关闭(&X)";
this.mnuClose.Click += new System.EventHandler(this.mnuClose_Click);
//
// menuItem2
//
this.menuItem2.Index = 1;
this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuHelp,
this.menuItem4,
this.mnuAbout});
this.menuItem2.Text = "帮助(&H)";
//
// mnuHelp
//
this.mnuHelp.Index = 0;
this.mnuHelp.Shortcut = System.Windows.Forms.Shortcut.F1;
this.mnuHelp.Text = "帮助";
this.mnuHelp.Click += new System.EventHandler(this.mnuHelp_Click);
//
// menuItem4
//
this.menuItem4.Index = 1;
this.menuItem4.Text = "-";
//
// mnuAbout
//
this.mnuAbout.Index = 2;
this.mnuAbout.Text = "关于(&A)";
this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.lstHistory);
this.groupBox1.Location = new System.Drawing.Point(616, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(160, 300);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "历史记录";
//
// lstHistory
//
this.lstHistory.ItemHeight = 12;
this.lstHistory.Location = new System.Drawing.Point(20, 22);
this.lstHistory.Name = "lstHistory";
this.lstHistory.Size = new System.Drawing.Size(120, 256);
this.lstHistory.TabIndex = 0;
//
// groupBox2
//
this.groupBox2.Controls.Add(this.lblWhitePlayer);
this.groupBox2.Controls.Add(this.pictureBox2);
this.groupBox2.Controls.Add(this.pictureBox1);
this.groupBox2.Controls.Add(this.lblBlackPlayer);
this.groupBox2.Location = new System.Drawing.Point(616, 336);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(160, 144);
this.groupBox2.TabIndex = 1;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "玩家信息";
//
// lblWhitePlayer
//
this.lblWhitePlayer.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lblWhitePlayer.ForeColor = System.Drawing.Color.Blue;
this.lblWhitePlayer.Location = new System.Drawing.Point(56, 80);
this.lblWhitePlayer.Name = "lblWhitePlayer";
this.lblWhitePlayer.Size = new System.Drawing.Size(90, 40);
this.lblWhitePlayer.TabIndex = 3;
this.lblWhitePlayer.Text = "电脑";
this.lblWhitePlayer.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// pictureBox2
//
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox2.Location = new System.Drawing.Point(8, 80);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(40, 40);
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(8, 24);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(40, 40);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// lblBlackPlayer
//
this.lblBlackPlayer.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lblBlackPlayer.ForeColor = System.Drawing.Color.Red;
this.lblBlackPlayer.Location = new System.Drawing.Point(56, 24);
this.lblBlackPlayer.Name = "lblBlackPlayer";
this.lblBlackPlayer.Size = new System.Drawing.Size(90, 40);
this.lblBlackPlayer.TabIndex = 2;
this.lblBlackPlayer.Text = "玩家";
this.lblBlackPlayer.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// groupBox3
//
this.groupBox3.Controls.Add(this.lblXY);
this.groupBox3.Location = new System.Drawing.Point(616, 504);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(160, 80);
this.groupBox3.TabIndex = 2;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "落子点";
//
// lblXY
//
this.lblXY.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.lblXY.ForeColor = System.Drawing.Color.Red;
this.lblXY.Location = new System.Drawing.Point(20, 29);
this.lblXY.Name = "lblXY";
this.lblXY.Size = new System.Drawing.Size(120, 23);
this.lblXY.TabIndex = 0;
this.lblXY.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(792, 623);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MaximumSize = new System.Drawing.Size(800, 650);
this.Menu = this.mainMenu;
this.MinimumSize = new System.Drawing.Size(800, 650);
this.Name = "frmMain";
this.Text = "C#版五子棋";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseDown);
this.Load += new System.EventHandler(this.frmMain_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmMain_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseMove);
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox3.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void frmMain_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//绘制棋盘内容
chessboard.Draw();
}
private void frmMain_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
//人下棋子
chessboard.PersonDownStone(e.X,e.Y);
//显示历史记录
Array arr = chessboard.StarckHistory.ToArray();
lstHistory.DataSource = arr;
}
private void frmMain_Load(object sender, System.EventArgs e)
{
//初始载入默认玩家先
chessboard.Start(false);
}
//显示落子点的位置
private void frmMain_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.X < 600 && e.Y < 600)
{
//取下棋点
int m = (int)Math.Floor(e.X/40);
int n = (int)Math.Floor(e.Y/40);
lblXY.Text = "X:"+m.ToString()+" Y:"+n.ToString();
}
}
#region 游戏菜单操作
//开始
private void mnuNew_Click(object sender, System.EventArgs e)
{
if (mnuPersonFirst.Checked)
{
chessboard.Start(false);
}
else
{
chessboard.Start(true);
}
}
//玩家先
private void mnuPersonFirst_Click(object sender, System.EventArgs e)
{
mnuPersonFirst.Checked = true;
mnuComputerFirst.Checked = false;
lblBlackPlayer.Text = "玩家";
lblWhitePlayer.Text = "电脑";
chessboard.Start(false);
}
//电脑先
private void mnuComputerFirst_Click(object sender, System.EventArgs e)
{
mnuPersonFirst.Checked = false;
mnuComputerFirst.Checked = true;
lblBlackPlayer.Text = "电脑";
lblWhitePlayer.Text = "玩家";
chessboard.Start(true);
}
//关闭
private void mnuClose_Click(object sender, System.EventArgs e)
{
this.Close();
}
#endregion
#region 帮助菜单操作
//帮助
private void mnuHelp_Click(object sender, System.EventArgs e)
{
try
{
System.Diagnostics.Process.Start("help.chm");
}
catch
{
MessageBox.Show("未找到帮助文件!");
}
}
//关于
private void mnuAbout_Click(object sender, System.EventArgs e)
{
frmAbout frmabout = new frmAbout();
frmabout.Show();
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -