DrawRectangle.cs
上传用户:jx_fiona
上传日期:2014-03-08
资源大小:1387k
文件大小:7k
源码类别:

打印编程

开发平台:

Others

  1. using System;
  2. using System.Drawing;
  3. namespace GoldPrinter
  4. {
  5. /// <summary>
  6. /// 矩形边框线,左、右、顶、底及四边、右下对角线、右上对角线。
  7. /// 
  8. /// 作 者:长江支流(周方勇)
  9. /// Email:flygoldfish@163.com  QQ:150439795
  10. /// 网 址:www.webmis.com.cn
  11. /// ★★★★★您可以免费使用此程序,但是请您完整保留此说明,以维护知识产权★★★★★
  12. /// 
  13. /// </summary>
  14. public enum BordersEdgeFlag{FourEdge,Left,Right,Top,Bottom,DiagonalDown,DiagonalUp}
  15. /// <summary>
  16. /// 提供在绘图表面用指定的笔在指定的矩形区画线,如左、右、顶、底及四边、右下对角线、右上对角线,可以改变Pen来设置线颜色、宽度、样式。
  17. /// 
  18. /// 程序提供:周方勇(长江支流)
  19. /// 技术及合作Email:flygoldfish@sina.com
  20. ///★★★★★您可以免费使用此程序,但是请您保留此说明,以维护知识产权★★★★★
  21. /// </summary>
  22. public class DrawRectangle:DrawBase
  23. {
  24. //矩形边框线
  25. private BordersEdgeFlag _borderEdge;
  26. //背景颜色
  27. private Color _backColor;
  28. #region 字段属性
  29. /// <summary>
  30. /// 获取或设置矩形边框线,有左、右、顶、底及四边、右下对角线、右上对角线等
  31. /// </summary>
  32. public BordersEdgeFlag BordersEdge
  33. {
  34. get
  35. {
  36. return _borderEdge;
  37. }
  38. set
  39. {
  40. _borderEdge = value;
  41. }
  42. }
  43. /// <summary>
  44. /// 获取或设置填充矩形的背景颜色, 默认为Color.White,只有改变为其它的颜色或显示调用FillRectangle()方法才会去绘背景
  45. /// </summary>
  46. public Color BackColor
  47. {
  48. get
  49. {
  50. return _backColor;
  51. }
  52. set
  53. {
  54. _backColor = value;
  55. }
  56. }
  57. #endregion
  58. /// <summary>
  59. /// 在绘图表面用指定的画笔在指定的矩形区绘制矩形或四周边框线或对角线
  60. /// </summary>
  61. public DrawRectangle()
  62. {
  63.             _borderEdge = BordersEdgeFlag.FourEdge;
  64. _backColor = Color.White;
  65. }
  66. /// <summary>
  67. /// 绘形矩形或边框,如没指定BorderEdge,默认绘制整个矩形。
  68. /// </summary>
  69. public override void Draw()
  70. {
  71. switch(this.BordersEdge)
  72. {
  73. case BordersEdgeFlag.Left:
  74. DrawLeftLine();
  75. break;
  76. case BordersEdgeFlag.Right:
  77. DrawRightLine();
  78. break;
  79. case BordersEdgeFlag.Top:
  80. DrawTopLine();
  81. break;
  82. case BordersEdgeFlag.Bottom:
  83. DrawBottomLine();
  84. break;
  85. case BordersEdgeFlag.DiagonalDown:
  86. DrawDiagonalDownLine();
  87. break;
  88. case BordersEdgeFlag.DiagonalUp:
  89. DrawDiagonalUpLine();
  90. break;
  91. default: //case BordersEdgeFlag.FourEdge:
  92. Draw(this.Graphics,this.Pen,this.Rectangle);
  93. break;
  94. }
  95. //只有改变颜色才画背景
  96. if (this.BackColor != Color.White)
  97. {
  98. FillRectangle();
  99. }
  100. }
  101. /// <summary>
  102. /// 用背景颜色填充矩形
  103. /// </summary>
  104. public void FillRectangle()
  105. {
  106. Pen pen = new Pen(this.BackColor);
  107. this.Brush = pen.Brush;
  108. DrawBackColor(this.Graphics,this.Brush,Rectangle);
  109. }
  110. /// <summary>
  111. /// 画矩形左端线
  112. /// </summary>
  113. /// <param name="g"></param>
  114. /// <param name="rec"></param>
  115. public void DrawLeftLine()
  116. {
  117. DrawLeftLine(this.Graphics,this.Pen,this.Rectangle);
  118. }
  119. /// <summary>
  120. /// 画矩形右端线
  121. /// </summary>
  122. /// <param name="g"></param>
  123. /// <param name="rec"></param>
  124. public void DrawRightLine()
  125. {
  126. DrawRightLine(this.Graphics,this.Pen,this.Rectangle);
  127. }
  128. /// <summary>
  129. /// 画矩形顶端线
  130. /// </summary>
  131. /// <param name="g"></param>
  132. /// <param name="rec"></param>
  133. public void DrawTopLine()
  134. {
  135. DrawTopLine(this.Graphics,this.Pen,this.Rectangle);
  136. }
  137. /// <summary>
  138. /// 画矩形底端线
  139. /// </summary>
  140. /// <param name="g"></param>
  141. /// <param name="rec"></param>
  142. public void DrawBottomLine()
  143. {
  144. DrawBottomLine(this.Graphics,this.Pen,this.Rectangle);
  145. }
  146. /// <summary>
  147. /// 画左上角到右下角的对角线
  148. /// </summary>
  149. /// <param name="g"></param>
  150. /// <param name="rec"></param>
  151. public void DrawDiagonalDownLine()
  152. {
  153. DrawDiagonalDownLine(this.Graphics,this.Pen,this.Rectangle);
  154. }
  155. /// <summary>
  156. /// 画左下角到右上角的对角线
  157. /// </summary>
  158. /// <param name="g"></param>
  159. /// <param name="rec"></param>
  160. public void DrawDiagonalUpLine()
  161. {
  162. DrawDiagonalUpLine(this.Graphics,this.Pen,this.Rectangle);
  163. }
  164. #region protected 全参数函数
  165. /// <summary>
  166. /// 画矩形线
  167. /// </summary>
  168. /// <param name="g"></param>
  169. /// <param name="rec"></param>
  170. protected void Draw(Graphics g,Pen pen,Rectangle rec)
  171. {
  172. //画网格顶端横线
  173. g.DrawRectangle(pen,rec.X,rec.Y,rec.Width,rec.Height);
  174. }
  175. /// <summary>
  176. /// 画矩形左端线
  177. /// </summary>
  178. /// <param name="g"></param>
  179. /// <param name="rec"></param>
  180. protected void DrawLeftLine(Graphics g,Pen pen,Rectangle rec)
  181. {
  182. int X = rec.Left;
  183. int Y1 = rec.Top;
  184. int Y2 = rec.Bottom;
  185. //画网格左端横线
  186. g.DrawLine(pen,X,Y1,X,Y2);
  187. }
  188. /// <summary>
  189. /// 画矩形右端线
  190. /// </summary>
  191. /// <param name="g"></param>
  192. /// <param name="rec"></param>
  193. protected void DrawRightLine(Graphics g,Pen pen,Rectangle rec)
  194. {
  195. int X = rec.Right;
  196. int Y1 = rec.Top;
  197. int Y2 = rec.Bottom;
  198. //画网格右端横线
  199. g.DrawLine(pen,X,Y1,X,Y2);
  200. }
  201. /// <summary>
  202. /// 画矩形顶端线
  203. /// </summary>
  204. /// <param name="g"></param>
  205. /// <param name="rec"></param>
  206. protected void DrawTopLine(Graphics g,Pen pen,Rectangle rec)
  207. {
  208. int X1 = rec.Left;
  209. int X2 = rec.Right;
  210. int Y = rec.Top;
  211. //画网格顶端横线
  212. g.DrawLine(pen,X1,Y,X2,Y);
  213. }
  214. /// <summary>
  215. /// 画矩形底端线
  216. /// </summary>
  217. /// <param name="g"></param>
  218. /// <param name="rec"></param>
  219. protected void DrawBottomLine(Graphics g,Pen pen,Rectangle rec)
  220. {
  221. int X1 = rec.Left;
  222. int X2 = rec.Right;
  223. int Y = rec.Bottom;
  224. //画网格底端横线
  225. g.DrawLine(pen,X1,Y,X2,Y);
  226. }
  227. /// <summary>
  228. /// 画左上角到右下角的对角线
  229. /// </summary>
  230. /// <param name="g"></param>
  231. /// <param name="rec"></param>
  232. protected void DrawDiagonalDownLine(Graphics g,Pen pen,Rectangle rec)
  233. {
  234. int X1 = rec.X; //左上角坐标
  235. int Y1 = rec.Y;
  236. int X2 = rec.Right; //右下角
  237. int Y2 = rec.Bottom;
  238. //画网格底端横线
  239. g.DrawLine(pen,X1,Y1,X2,Y2);
  240. }
  241. /// <summary>
  242. /// 画左下角到右上角的对角线
  243. /// </summary>
  244. /// <param name="g"></param>
  245. /// <param name="rec"></param>
  246. protected void DrawDiagonalUpLine(Graphics g,Pen pen,Rectangle rec)
  247. {
  248. int X1 = rec.X; //左下角坐标
  249. int Y1 = rec.Bottom;
  250. int X2 = rec.Right; //右下角
  251. int Y2 = rec.Top;
  252. //画网格底端横线
  253. g.DrawLine(pen,X1,Y1,X2,Y2);
  254. }
  255. protected void DrawBackColor(Graphics g,Brush brush,Rectangle rec)
  256. {
  257. //填充矩形
  258. g.FillRectangle(brush,rec);
  259. }
  260. #endregion
  261. }//End Class
  262. }//End Namespace