一 引子
为了让更多的编程初学者,轻松愉快地掌握面向对象的思考方法,对象继承和多态的妙用,故推出此系列随笔,还望大家多多支持。
预备知识,无GDI画图基础的童鞋请先阅读一篇文章让你彻底弄懂WinForm GDI 编程基本原理
二 本节内容---画挡板
1.主窗体启动后,一个挡板出现在窗体上,鼠标点击,挡板重新画在鼠标点击的地方,主界面截图如下:
三 挡板类设计
挡板类的定义代码如下:
class Board { /// <summary> /// 挡板自身宽度 /// </summary> public int m_nBoardWidth = 170; public int m_nBoardHeight = 15; //坐标 public int XPos { get; set; } public int YPos { get; set; } /// <summary> /// 构造挡板初始位置坐标,速度和宽度 /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="speed"></param> public Board(int x, int y, int nBoardWidth, int nBoardHeight) { this.XPos = x; this.YPos = y; m_nBoardWidth = nBoardWidth; m_nBoardHeight = nBoardHeight; } /// <summary> /// 画挡板 /// </summary> /// <param name="g"></param> public void Draw(System.Drawing.Graphics g) { using (SolidBrush sbrush = new SolidBrush(Color.LightBlue)) { Pen p = new Pen(Color.SaddleBrown); Rectangle Rect = new Rectangle(XPos, YPos, m_nBoardWidth, m_nBoardHeight); g.DrawRectangle(p, Rect); g.FillRectangle(sbrush, Rect); } g.Dispose(); }