frmMain.cs
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:13k
源码类别:

C#编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. using System.Collections;
  4. using System.ComponentModel;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. namespace FiveStones
  8. {
  9. /// <summary>
  10. /// Form1 的摘要说明。
  11. /// </summary>
  12. public class frmMain : System.Windows.Forms.Form
  13. {
  14. /// <summary>
  15. /// 必需的设计器变量。
  16. /// </summary>
  17. private System.ComponentModel.Container components = null;
  18. private System.Windows.Forms.MenuItem menuItem1;
  19. private System.Windows.Forms.MenuItem menuItem2;
  20. private System.Windows.Forms.MenuItem menuItem4;
  21. private System.Windows.Forms.MenuItem menuItem7;
  22. private System.Windows.Forms.MenuItem menuItem10;
  23. private System.Windows.Forms.MainMenu mainMenu;
  24. private System.Windows.Forms.MenuItem mnuClose;
  25. private System.Windows.Forms.MenuItem mnuNew;
  26. private System.Windows.Forms.MenuItem mnuComputerFirst;
  27. private System.Windows.Forms.MenuItem mnuPersonFirst;
  28. private System.Windows.Forms.GroupBox groupBox1;
  29. private System.Windows.Forms.GroupBox groupBox2;
  30. private System.Windows.Forms.PictureBox pictureBox1;
  31. private System.Windows.Forms.PictureBox pictureBox2;
  32. private System.Windows.Forms.Label lblBlackPlayer;
  33. private System.Windows.Forms.Label lblWhitePlayer;
  34. private System.Windows.Forms.ListBox lstHistory;
  35. private System.Windows.Forms.GroupBox groupBox3;
  36. private System.Windows.Forms.Label lblXY;
  37. private System.Windows.Forms.MenuItem mnuAbout;
  38. private System.Windows.Forms.MenuItem mnuHelp;
  39. //定义棋盘类
  40. private Chessboard chessboard;
  41. public frmMain()
  42. {
  43. //
  44. // Windows 窗体设计器支持所必需的
  45. //
  46. InitializeComponent();
  47. //
  48. // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
  49. //
  50. //构造棋盘类
  51. System.Drawing.Graphics g = this.CreateGraphics();
  52. chessboard = new Chessboard(g);
  53. }
  54. /// <summary>
  55. /// 清理所有正在使用的资源。
  56. /// </summary>
  57. protected override void Dispose( bool disposing )
  58. {
  59. if( disposing )
  60. {
  61. if (components != null) 
  62. {
  63. components.Dispose();
  64. }
  65. }
  66. base.Dispose( disposing );
  67. }
  68. #region Windows 窗体设计器生成的代码
  69. /// <summary>
  70. /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  71. /// 此方法的内容。
  72. /// </summary>
  73. private void InitializeComponent()
  74. {
  75. System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmMain));
  76. this.mainMenu = new System.Windows.Forms.MainMenu();
  77. this.menuItem1 = new System.Windows.Forms.MenuItem();
  78. this.mnuNew = new System.Windows.Forms.MenuItem();
  79. this.menuItem7 = new System.Windows.Forms.MenuItem();
  80. this.mnuPersonFirst = new System.Windows.Forms.MenuItem();
  81. this.mnuComputerFirst = new System.Windows.Forms.MenuItem();
  82. this.menuItem10 = new System.Windows.Forms.MenuItem();
  83. this.mnuClose = new System.Windows.Forms.MenuItem();
  84. this.menuItem2 = new System.Windows.Forms.MenuItem();
  85. this.mnuHelp = new System.Windows.Forms.MenuItem();
  86. this.menuItem4 = new System.Windows.Forms.MenuItem();
  87. this.mnuAbout = new System.Windows.Forms.MenuItem();
  88. this.groupBox1 = new System.Windows.Forms.GroupBox();
  89. this.lstHistory = new System.Windows.Forms.ListBox();
  90. this.groupBox2 = new System.Windows.Forms.GroupBox();
  91. this.lblWhitePlayer = new System.Windows.Forms.Label();
  92. this.pictureBox2 = new System.Windows.Forms.PictureBox();
  93. this.pictureBox1 = new System.Windows.Forms.PictureBox();
  94. this.lblBlackPlayer = new System.Windows.Forms.Label();
  95. this.groupBox3 = new System.Windows.Forms.GroupBox();
  96. this.lblXY = new System.Windows.Forms.Label();
  97. this.groupBox1.SuspendLayout();
  98. this.groupBox2.SuspendLayout();
  99. this.groupBox3.SuspendLayout();
  100. this.SuspendLayout();
  101. // 
  102. // mainMenu
  103. // 
  104. this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  105.  this.menuItem1,
  106.  this.menuItem2});
  107. // 
  108. // menuItem1
  109. // 
  110. this.menuItem1.Index = 0;
  111. this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  112.   this.mnuNew,
  113.   this.menuItem7,
  114.   this.mnuPersonFirst,
  115.   this.mnuComputerFirst,
  116.   this.menuItem10,
  117.   this.mnuClose});
  118. this.menuItem1.Text = "游戏(&G)";
  119. // 
  120. // mnuNew
  121. // 
  122. this.mnuNew.Index = 0;
  123. this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.F2;
  124. this.mnuNew.Text = "开始";
  125. this.mnuNew.Click += new System.EventHandler(this.mnuNew_Click);
  126. // 
  127. // menuItem7
  128. // 
  129. this.menuItem7.Index = 1;
  130. this.menuItem7.Text = "-";
  131. // 
  132. // mnuPersonFirst
  133. // 
  134. this.mnuPersonFirst.Checked = true;
  135. this.mnuPersonFirst.Index = 2;
  136. this.mnuPersonFirst.Text = "玩家先";
  137. this.mnuPersonFirst.Click += new System.EventHandler(this.mnuPersonFirst_Click);
  138. // 
  139. // mnuComputerFirst
  140. // 
  141. this.mnuComputerFirst.Index = 3;
  142. this.mnuComputerFirst.Text = "电脑先";
  143. this.mnuComputerFirst.Click += new System.EventHandler(this.mnuComputerFirst_Click);
  144. // 
  145. // menuItem10
  146. // 
  147. this.menuItem10.Index = 4;
  148. this.menuItem10.Text = "-";
  149. // 
  150. // mnuClose
  151. // 
  152. this.mnuClose.Index = 5;
  153. this.mnuClose.Text = "关闭(&X)";
  154. this.mnuClose.Click += new System.EventHandler(this.mnuClose_Click);
  155. // 
  156. // menuItem2
  157. // 
  158. this.menuItem2.Index = 1;
  159. this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
  160.   this.mnuHelp,
  161.   this.menuItem4,
  162.   this.mnuAbout});
  163. this.menuItem2.Text = "帮助(&H)";
  164. // 
  165. // mnuHelp
  166. // 
  167. this.mnuHelp.Index = 0;
  168. this.mnuHelp.Shortcut = System.Windows.Forms.Shortcut.F1;
  169. this.mnuHelp.Text = "帮助";
  170. this.mnuHelp.Click += new System.EventHandler(this.mnuHelp_Click);
  171. // 
  172. // menuItem4
  173. // 
  174. this.menuItem4.Index = 1;
  175. this.menuItem4.Text = "-";
  176. // 
  177. // mnuAbout
  178. // 
  179. this.mnuAbout.Index = 2;
  180. this.mnuAbout.Text = "关于(&A)";
  181. this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click);
  182. // 
  183. // groupBox1
  184. // 
  185. this.groupBox1.Controls.Add(this.lstHistory);
  186. this.groupBox1.Location = new System.Drawing.Point(616, 8);
  187. this.groupBox1.Name = "groupBox1";
  188. this.groupBox1.Size = new System.Drawing.Size(160, 300);
  189. this.groupBox1.TabIndex = 0;
  190. this.groupBox1.TabStop = false;
  191. this.groupBox1.Text = "历史记录";
  192. // 
  193. // lstHistory
  194. // 
  195. this.lstHistory.ItemHeight = 12;
  196. this.lstHistory.Location = new System.Drawing.Point(20, 22);
  197. this.lstHistory.Name = "lstHistory";
  198. this.lstHistory.Size = new System.Drawing.Size(120, 256);
  199. this.lstHistory.TabIndex = 0;
  200. // 
  201. // groupBox2
  202. // 
  203. this.groupBox2.Controls.Add(this.lblWhitePlayer);
  204. this.groupBox2.Controls.Add(this.pictureBox2);
  205. this.groupBox2.Controls.Add(this.pictureBox1);
  206. this.groupBox2.Controls.Add(this.lblBlackPlayer);
  207. this.groupBox2.Location = new System.Drawing.Point(616, 336);
  208. this.groupBox2.Name = "groupBox2";
  209. this.groupBox2.Size = new System.Drawing.Size(160, 144);
  210. this.groupBox2.TabIndex = 1;
  211. this.groupBox2.TabStop = false;
  212. this.groupBox2.Text = "玩家信息";
  213. // 
  214. // lblWhitePlayer
  215. // 
  216. this.lblWhitePlayer.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  217. this.lblWhitePlayer.ForeColor = System.Drawing.Color.Blue;
  218. this.lblWhitePlayer.Location = new System.Drawing.Point(56, 80);
  219. this.lblWhitePlayer.Name = "lblWhitePlayer";
  220. this.lblWhitePlayer.Size = new System.Drawing.Size(90, 40);
  221. this.lblWhitePlayer.TabIndex = 3;
  222. this.lblWhitePlayer.Text = "电脑3";
  223. this.lblWhitePlayer.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  224. // 
  225. // pictureBox2
  226. // 
  227. this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
  228. this.pictureBox2.Location = new System.Drawing.Point(8, 80);
  229. this.pictureBox2.Name = "pictureBox2";
  230. this.pictureBox2.Size = new System.Drawing.Size(40, 40);
  231. this.pictureBox2.TabIndex = 1;
  232. this.pictureBox2.TabStop = false;
  233. // 
  234. // pictureBox1
  235. // 
  236. this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
  237. this.pictureBox1.Location = new System.Drawing.Point(8, 24);
  238. this.pictureBox1.Name = "pictureBox1";
  239. this.pictureBox1.Size = new System.Drawing.Size(40, 40);
  240. this.pictureBox1.TabIndex = 0;
  241. this.pictureBox1.TabStop = false;
  242. // 
  243. // lblBlackPlayer
  244. // 
  245. this.lblBlackPlayer.Font = new System.Drawing.Font("楷体_GB2312", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  246. this.lblBlackPlayer.ForeColor = System.Drawing.Color.Red;
  247. this.lblBlackPlayer.Location = new System.Drawing.Point(56, 24);
  248. this.lblBlackPlayer.Name = "lblBlackPlayer";
  249. this.lblBlackPlayer.Size = new System.Drawing.Size(90, 40);
  250. this.lblBlackPlayer.TabIndex = 2;
  251. this.lblBlackPlayer.Text = "玩家";
  252. this.lblBlackPlayer.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
  253. // 
  254. // groupBox3
  255. // 
  256. this.groupBox3.Controls.Add(this.lblXY);
  257. this.groupBox3.Location = new System.Drawing.Point(616, 504);
  258. this.groupBox3.Name = "groupBox3";
  259. this.groupBox3.Size = new System.Drawing.Size(160, 80);
  260. this.groupBox3.TabIndex = 2;
  261. this.groupBox3.TabStop = false;
  262. this.groupBox3.Text = "落子点";
  263. // 
  264. // lblXY
  265. // 
  266. this.lblXY.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
  267. this.lblXY.ForeColor = System.Drawing.Color.Red;
  268. this.lblXY.Location = new System.Drawing.Point(20, 29);
  269. this.lblXY.Name = "lblXY";
  270. this.lblXY.Size = new System.Drawing.Size(120, 23);
  271. this.lblXY.TabIndex = 0;
  272. this.lblXY.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  273. // 
  274. // frmMain
  275. // 
  276. this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  277. this.ClientSize = new System.Drawing.Size(792, 623);
  278. this.Controls.Add(this.groupBox3);
  279. this.Controls.Add(this.groupBox2);
  280. this.Controls.Add(this.groupBox1);
  281. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  282. this.MaximizeBox = false;
  283. this.MaximumSize = new System.Drawing.Size(800, 650);
  284. this.Menu = this.mainMenu;
  285. this.MinimumSize = new System.Drawing.Size(800, 650);
  286. this.Name = "frmMain";
  287. this.Text = "C#版五子棋";
  288. this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseDown);
  289. this.Load += new System.EventHandler(this.frmMain_Load);
  290. this.Paint += new System.Windows.Forms.PaintEventHandler(this.frmMain_Paint);
  291. this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.frmMain_MouseMove);
  292. this.groupBox1.ResumeLayout(false);
  293. this.groupBox2.ResumeLayout(false);
  294. this.groupBox3.ResumeLayout(false);
  295. this.ResumeLayout(false);
  296. }
  297. #endregion
  298. /// <summary>
  299. /// 应用程序的主入口点。
  300. /// </summary>
  301. [STAThread]
  302. static void Main() 
  303. {
  304. Application.Run(new frmMain());
  305. }
  306. private void frmMain_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  307. {
  308. //绘制棋盘内容
  309. chessboard.Draw();
  310. }
  311. private void frmMain_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  312. {
  313. //人下棋子
  314. chessboard.PersonDownStone(e.X,e.Y);
  315. //显示历史记录
  316. Array arr = chessboard.StarckHistory.ToArray();
  317. lstHistory.DataSource = arr;
  318. }
  319. private void frmMain_Load(object sender, System.EventArgs e)
  320. {
  321. //初始载入默认玩家先
  322. chessboard.Start(false);
  323. }
  324. //显示落子点的位置
  325. private void frmMain_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
  326. {
  327. if (e.X < 600 && e.Y < 600)
  328. {
  329. //取下棋点
  330. int m = (int)Math.Floor(e.X/40);
  331. int n = (int)Math.Floor(e.Y/40);
  332. lblXY.Text = "X:"+m.ToString()+"  Y:"+n.ToString();
  333. }
  334. }
  335. #region 游戏菜单操作
  336. //开始
  337. private void mnuNew_Click(object sender, System.EventArgs e)
  338. {
  339. if (mnuPersonFirst.Checked)
  340. {
  341. chessboard.Start(false);
  342. }
  343. else
  344. {
  345. chessboard.Start(true);
  346. }
  347. }
  348. //玩家先
  349. private void mnuPersonFirst_Click(object sender, System.EventArgs e)
  350. {
  351. mnuPersonFirst.Checked = true;
  352. mnuComputerFirst.Checked = false;
  353. lblBlackPlayer.Text = "玩家";
  354. lblWhitePlayer.Text = "电脑";
  355. chessboard.Start(false);
  356. }
  357. //电脑先
  358. private void mnuComputerFirst_Click(object sender, System.EventArgs e)
  359. {
  360. mnuPersonFirst.Checked = false;
  361. mnuComputerFirst.Checked = true;
  362. lblBlackPlayer.Text = "电脑";
  363. lblWhitePlayer.Text = "玩家";
  364. chessboard.Start(true);
  365. }
  366. //关闭
  367. private void mnuClose_Click(object sender, System.EventArgs e)
  368. {
  369. this.Close();
  370. }
  371. #endregion
  372. #region 帮助菜单操作
  373. //帮助
  374. private void mnuHelp_Click(object sender, System.EventArgs e)
  375. {
  376. }
  377. //关于
  378. private void mnuAbout_Click(object sender, System.EventArgs e)
  379. {
  380. }
  381. #endregion
  382. }
  383. }