在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#图形和图像处理 → C# 绘制中国象棋棋盘/棋子示例(可深入学习自定义控件)

C# 绘制中国象棋棋盘/棋子示例(可深入学习自定义控件)

C#图形和图像处理

下载此实例
  • 开发语言:C#
  • 实例大小:2.51M
  • 下载次数:68
  • 浏览次数:711
  • 发布时间:2018-07-25
  • 实例类别:C#图形和图像处理
  • 发 布 人:crazycode
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 象棋 C# 学习 c 源码

实例介绍

【实例简介】

【实例截图】

from clipboardfrom clipboard

【核心代码】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
 
namespace ChineseChess
{
    /// <summary>
    /// 棋盘控件
    /// </summary>
    public partial class ChessBoardControl : UserControl
    {
        private Piece[,] ArrPiece { get; set; }
 
        public ChessBoardControl()
        {
            InitializeComponent();
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            //初始化数组
            InitArrPieceInfo();
 
            Graphics g = e.Graphics;
            int width = this.Width;
            int height = this.Height;
            int padding = this.Padding.All * 20;
            int center = height / 2;//垂直中心位置
            int s_width = (width - 2 * padding) / 8;//每一条横线的间距
            int s_heigth = (height - 2 * padding) / 9;//每一条竖线的间距
            int start_x = padding;//起始位置
            int start_y = padding;//起始位置
            Pen pen = new Pen(Brushes.Black, 1.5f);
            Dictionary<string, string[]> dicNums = new Dictionary<string, string[]>();
            dicNums.Add("up", new string[9] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });
            dicNums.Add("down", new string[9] { "九", "八", "七", "六", "五", "四", "三", "二", "一" });
            Font font = new Font("宋体", 12, FontStyle.Regular);
            for (int i = 0; i < 9; i  )
            {
                //竖线九条
                Point p0 = new Point(start_x   i * s_width, start_y);
                Point p1 = new Point(start_x   i * s_width, start_y   (s_heigth * 4));
                Point p2 = new Point(start_x   i * s_width, start_y   (s_heigth * 5));
                Point p3 = new Point(start_x   i * s_width, start_y   (s_heigth * 9));
                g.DrawLine(pen, p0, p1);
                g.DrawLine(pen, p2, p3);
                //上下的文字
                Point p_up = new Point(start_x   i * s_width - 5, padding / 2);
                Point p_down = new Point(start_x   i * s_width - 5, start_y   (s_heigth * 9)   padding / 3);
                g.DrawString(dicNums["up"][i], font, Brushes.Black, p_up);
                g.DrawString(dicNums["down"][i], font, Brushes.Black, p_down);
                //数组赋值
                for (int j = 0; j < 10; j  )
                {
                    Point absLocation = ArrPiece[i, j].AbsoluteLocation;
                    absLocation.X = start_x   i * s_width;
                    ArrPiece[i, j].AbsoluteLocation = absLocation;
                }
            }
            for (int i = 0; i < 10; i  )
            {
                //横线十条
                Point p0 = new Point(start_x, start_y   i * s_heigth);
                Point p1 = new Point(start_x   s_width * 8, start_y   i * s_heigth);
                g.DrawLine(pen, p0, p1);
                //数组赋值
                for (int j = 0; j < 9; j  )
                {
                    Point absLocation = ArrPiece[j, i].AbsoluteLocation;
                    absLocation.Y = start_y   i * s_heigth;
                    ArrPiece[j, i].AbsoluteLocation = absLocation;
                }
            }
            //绘制九宫格
            for (int i = 0; i < 2; i  )
            {
                Point p0 = new Point(start_x   (3   i * 2) * s_width, start_y);
                Point p1 = new Point(start_x   (5 - i * 2) * s_width, start_y   (s_heigth * 2));
                Point p2 = new Point(start_x   (3   i * 2) * s_width, start_y   (s_heigth * 7));
                Point p3 = new Point(start_x   (5 - i * 2) * s_width, start_y   (s_heigth * 9));
                g.DrawLine(pen, p0, p1);
                g.DrawLine(pen, p2, p3);
            }
 
            //兵和卒处有拐角,从左往右
            for (int i = 0; i < 5; i  )
            {
                int p_x = start_x   2 * i * s_width;
                int p_y = start_y   3 * s_heigth;
                DrawCorner(g, pen, p_x, p_y);//兵
                p_y = start_y   6 * s_heigth;
                DrawCorner(g, pen, p_x, p_y);//卒
            }
            //炮处的拐角,从左往右
            for (int i = 0; i < 2; i  )
            {
                int p_x = start_x   (1   6 * i) * s_width;
                int p_y = start_y   2 * s_heigth;
                DrawCorner(g, pen, p_x, p_y);//炮
                p_y = start_y   7 * s_heigth;
                DrawCorner(g, pen, p_x, p_y);//炮
            }
            //绘制楚河汉界
            Point p_0 = new Point(2 * s_width, center - 25);
            Point p_1 = new Point(7 * s_width, center   20);
            font = new Font("方正隶二繁体", 30, FontStyle.Regular);
            g.DrawString("楚河", font, Brushes.Black, p_0);
            Matrix mtxSave = g.Transform;
            Matrix mtxRotate = g.Transform;
            mtxRotate.RotateAt(180, p_1);
            g.Transform = mtxRotate;
            g.DrawString("汉界", font, Brushes.Black, p_1);
            g.Transform = mtxSave;
            //绘制外边框
            g.DrawRectangle(pen, 3, 3, width - 6, height - 6);
            g.DrawRectangle(pen, 5, 5, width - 10, height - 10);
            g.DrawRectangle(pen, 7, 7, width - 14, height - 14);
        }
 
        /// <summary>
        /// 绘制拐角
        /// </summary>
        /// <param name="g"></param>
        /// <param name="pen"></param>
        /// <param name="p_x"></param>
        /// <param name="p_y"></param>
        private void DrawCorner(Graphics g, Pen pen, int p_x, int p_y)
        {
            //左上
            Point p0 = new Point(p_x - 2, p_y - 7);
            Point p1 = new Point(p_x - 2, p_y - 2);
            Point p2 = new Point(p_x - 7, p_y - 2);
            Point p3 = new Point(p_x - 2, p_y - 2);
            g.DrawLine(pen, p0, p1);
            g.DrawLine(pen, p2, p3);
            //右上
            Point p10 = new Point(p_x   2, p_y - 7);
            Point p11 = new Point(p_x   2, p_y - 2);
            Point p12 = new Point(p_x   2, p_y - 2);
            Point p13 = new Point(p_x   7, p_y - 2);
            g.DrawLine(pen, p10, p11);
            g.DrawLine(pen, p12, p13);
            //左下
            Point p15 = new Point(p_x - 7, p_y   2);
            Point p16 = new Point(p_x - 2, p_y   2);
            Point p17 = new Point(p_x - 2, p_y   2);
            Point p18 = new Point(p_x - 2, p_y   7);
            g.DrawLine(pen, p15, p16);
            g.DrawLine(pen, p17, p18); 
            //右下
            Point p5 = new Point(p_x   2, p_y   7);
            Point p6 = new Point(p_x   2, p_y   2);
            Point p7 = new Point(p_x   7, p_y   2);
            Point p8 = new Point(p_x   2, p_y   2);
            g.DrawLine(pen, p5, p6);
            g.DrawLine(pen, p7, p8);
        }
 
        /// <summary>
        /// 初始化棋子数组
        /// </summary>
        private void InitArrPieceInfo()
        {
            ArrPiece = new Piece[9, 10];
            bool isRed = false;
            //初始化棋子位置信息
            for (int i = 0; i < 9; i  )
            {
                for (int j = 0; j < 10; j  )
                {
                    if (j >= 5)
                    {
                        isRed = true;
                    }
                    else {
                        isRed = false;
                    }
                    ArrPiece[i, j] = new Piece()
                    {
                        RelativeLocation = new Point(i, j),
                        AbsoluteLocation = new Point(0, 0),
                        IsRed = isRed
                    };
                }
            }
        }
 
        /// <summary>
        /// 初始化开始信息
        /// </summary>
        public void InitStartInfo()
        {
            if (ArrPiece != null)
            {
                this.Controls.Clear();
                string[] arrTmp0 = new string[9] { "車", "馬", "象", "士", "将", "士", "象", "馬", "車" };
                string[] arrTmp1 = new string[9] { "車", "馬", "相", "仕", "帅", "仕", "相", "馬", "車" };
                int radius = 45;
                Color backColor = Color.Gold;
                for (int i = 0; i < 9; i  )
                {
                    PieceControl p0 = new PieceControl()
                    {
                        Text = arrTmp0[i],
                        BackColor = backColor,
                        PieceInfo = ArrPiece[i, 0],
                        Width = radius,
                        Height = radius
                    };
                    p0.Click  = Piece_Click;
                    this.Controls.Add(p0);
                    PieceControl p1 = new PieceControl()
                    {
                        Text = arrTmp1[i],
                        BackColor = backColor,
                        PieceInfo = ArrPiece[i, 9],
                        Width = radius,
                        Height = radius
                    };
                    p1.Click  = Piece_Click;
                    this.Controls.Add(p1);
                }
                //炮
                for (int i = 0; i < 2; i  )
                {
                    PieceControl p0 = new PieceControl()
                    {
                        Text = "炮",
                        BackColor = backColor,
                        PieceInfo = ArrPiece[i * 6   1, 2],
                        Width = radius,
                        Height = radius
                    };
                    p0.Click  = Piece_Click;
                    this.Controls.Add(p0);
                    PieceControl p1 = new PieceControl()
                    {
                        Text = "炮",
                        BackColor = backColor,
                        PieceInfo = ArrPiece[i * 6   1, 7],
                        Width = radius,
                        Height = radius
                    };
                    p1.Click  = Piece_Click;
                    this.Controls.Add(p1);
                }
                //兵和卒
                for (int i = 0; i < 5; i  )
                {
                    PieceControl p0 = new PieceControl()
                    {
                        Text = "卒",
                        BackColor = backColor,
                        PieceInfo = ArrPiece[i * 2, 3],
                        Width = radius,
                        Height = radius
                    };
                    p0.Click  = Piece_Click;
                    this.Controls.Add(p0);
                    PieceControl p1 = new PieceControl()
                    {
                        Text = "兵",
                        BackColor = backColor,
                        PieceInfo = ArrPiece[i * 2, 6],
                        Width = radius,
                        Height = radius
                    };
                    p1.Click  = Piece_Click;
                    this.Controls.Add(p1);
                }
            }
        }
 
        /// <summary>
        /// 重置棋盘
        /// </summary>
        public void ResetInfo()
        {
            this.Controls.Clear();
        }
 
        /// <summary>
        /// 点击棋子事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Piece_Click(object sender, EventArgs e) {
            PieceControl p=  (PieceControl)sender;
            MessageBox.Show(string.Format("我是:{0}",p.Text));
        }
    }
 
    /// <summary>
    /// 棋子
    /// </summary>
    public class Piece {
 
        /// <summary>
        /// 绝对位置
        /// </summary>
        public Point AbsoluteLocation { get; set; }
 
        /// <summary>
        /// 相对位置
        /// </summary>
        public Point RelativeLocation { get; set; }
 
        /// <summary>
        /// 是否是红方
        /// </summary>
        public bool IsRed { get; set; }
 
    }
}

标签: 象棋 C# 学习 c 源码

实例下载地址

C# 绘制中国象棋棋盘/棋子示例(可深入学习自定义控件)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

第 1 楼 yx888888 发表于: 2020-08-24 11:39 07
工程没有

支持(0) 盖楼(回复)

发表评论

(您的评论需要经过审核才能显示)

查看所有1条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警