GraphicsPanel.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace OTSIncAMeasureApp
  10. {
  11. public class GraphicsPanel : System.Windows.Forms.Panel
  12. {
  13. private Color _borderColor = Color.Black;
  14. private int _radius = 50;
  15. private RoundStyle _roundeStyle;
  16. private const int WM_PAINT = 0xF;
  17. public GraphicsPanel()
  18. {
  19. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
  20. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  21. this.SetStyle(ControlStyles.UserPaint, true);
  22. }
  23. /// <summary>
  24. /// 建立圆角路径的样式。
  25. /// </summary>
  26. public enum RoundStyle
  27. {
  28. /// <summary>
  29. /// 四个角都不是圆角。
  30. /// </summary>
  31. None = 0,
  32. /// <summary>
  33. /// 四个角都为圆角。
  34. /// </summary>
  35. All = 1,
  36. /// <summary>
  37. /// 左边两个角为圆角。
  38. /// </summary>
  39. Left = 2,
  40. /// <summary>
  41. /// 右边两个角为圆角。
  42. /// </summary>
  43. Right = 3,
  44. /// <summary>
  45. /// 上边两个角为圆角。
  46. /// </summary>
  47. Top = 4,
  48. /// <summary>
  49. /// 下边两个角为圆角。
  50. /// </summary>
  51. Bottom = 5,
  52. }
  53. /// <summary>
  54. /// 建立带有圆角样式的路径。
  55. /// </summary>
  56. /// <param name="rect">用来建立路径的矩形。</param>
  57. /// <param name="_radius">圆角的大小。</param>
  58. /// <param name="style">圆角的样式。</param>
  59. /// <param name="correction">是否把矩形长宽减 1,以便画出边框。</param>
  60. /// <returns>建立的路径。</returns>
  61. GraphicsPath CreatePath(Rectangle rect, int radius, RoundStyle style, bool correction)
  62. {
  63. GraphicsPath path = new GraphicsPath();
  64. int radiusCorrection = correction ? 1 : 0;
  65. switch (style)
  66. {
  67. case RoundStyle.None:
  68. path.AddRectangle(rect);
  69. break;
  70. case RoundStyle.All:
  71. path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
  72. path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
  73. path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
  74. path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
  75. break;
  76. case RoundStyle.Left:
  77. path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
  78. path.AddLine(rect.Right - radiusCorrection, rect.Y, rect.Right - radiusCorrection, rect.Bottom - radiusCorrection);
  79. path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
  80. break;
  81. case RoundStyle.Right:
  82. path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
  83. path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
  84. path.AddLine(rect.X, rect.Bottom - radiusCorrection, rect.X, rect.Y);
  85. break;
  86. case RoundStyle.Top:
  87. path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
  88. path.AddArc(rect.Right - radius - radiusCorrection, rect.Y, radius, radius, 270, 90);
  89. path.AddLine(rect.Right - radiusCorrection, rect.Bottom - radiusCorrection, rect.X, rect.Bottom - radiusCorrection);
  90. break;
  91. case RoundStyle.Bottom:
  92. path.AddArc(rect.Right - radius - radiusCorrection, rect.Bottom - radius - radiusCorrection, radius, radius, 0, 90);
  93. path.AddArc(rect.X, rect.Bottom - radius - radiusCorrection, radius, radius, 90, 90);
  94. path.AddLine(rect.X, rect.Y, rect.Right - radiusCorrection, rect.Y);
  95. break;
  96. }
  97. path.CloseFigure(); //这句很关键,缺少会没有左边线。
  98. return path;
  99. }
  100. [DefaultValue(typeof(Color), "23, 169, 254"), Description("控件边框颜色")]
  101. public Color BorderColor
  102. {
  103. get { return _borderColor; }
  104. set
  105. {
  106. _borderColor = value;
  107. base.Invalidate();
  108. }
  109. }
  110. [DefaultValue(typeof(int), "10"), Description("圆角弧度大小")]
  111. public int Radius
  112. {
  113. get { return _radius; }
  114. set
  115. {
  116. _radius = value;
  117. base.Invalidate();
  118. }
  119. }
  120. public RoundStyle RoundeStyle
  121. {
  122. get { return _roundeStyle; }
  123. set
  124. {
  125. _roundeStyle = value;
  126. base.Invalidate();
  127. }
  128. }
  129. protected override void WndProc(ref Message m)
  130. {
  131. try
  132. {
  133. base.WndProc(ref m);
  134. if (m.Msg == WM_PAINT)
  135. {
  136. if (this.Radius > 0)
  137. {
  138. using (Graphics g = Graphics.FromHwnd(this.Handle))
  139. {
  140. Rectangle r = new Rectangle();
  141. r.Width = this.Width;
  142. r.Height = this.Height;
  143. DrawBorder(g, r, this.RoundeStyle, this.Radius);
  144. }
  145. }
  146. }
  147. }
  148. catch (Exception ex)
  149. {
  150. MessageBox.Show(ex.Message);
  151. }
  152. }
  153. private void DrawBorder(Graphics g, Rectangle rect, RoundStyle roundStyle, int radius)
  154. {
  155. rect.Width -= 1;
  156. rect.Height -= 1;
  157. using (GraphicsPath path = CreatePath(rect, radius, roundStyle, false))
  158. {
  159. using (Pen pen = new Pen(this.BorderColor))
  160. {
  161. Color myColor = ColorTranslator.FromHtml("#c8c8c8");
  162. System.Drawing.SolidBrush brushBG = new System.Drawing.SolidBrush(myColor);
  163. g.FillPath(brushBG, path);
  164. //消除锯齿
  165. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  166. g.DrawPath(pen, path);
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 绘制外框方法
  172. /// </summary>
  173. /// <param name="CenterPonit">中心点</param>
  174. /// <param name="RectSize">尺寸大小 宽高</param>
  175. /// <param name="GraphicsType">图形类型 0:矩形 1:椭圆</param>
  176. /// <param name="IsRound">是否有圆角</param>
  177. /// <param name="Radius">半径</param>
  178. public static GraphicsPanel CreateOutLineBorder(Point CenterPonit, Rectangle RectSize,int GraphicsType, bool IsRound, int Radius)
  179. {
  180. GraphicsPanel grPanel = new GraphicsPanel();
  181. //工作区中心位置
  182. grPanel.Location = RectSize.Location;
  183. //外框尺寸
  184. grPanel.Size = RectSize.Size;
  185. //是否有圆角
  186. if (IsRound)
  187. {
  188. //圆角半径
  189. grPanel.Radius = Radius;
  190. //圆角类型
  191. grPanel.RoundeStyle = RoundStyle.All;
  192. }
  193. Graphics GrRect = grPanel.CreateGraphics();
  194. return grPanel;
  195. }
  196. /// <summary>
  197. /// 绘制图形方法
  198. /// </summary>
  199. /// <param name="GObject">图形对象</param>
  200. /// <param name="RectSize">尺寸大小 宽高</param>
  201. /// <param name="CenterPonit">中心点</param>
  202. /// <param name="GraphicsType">图形类型 0:矩形 1:椭圆</param>
  203. /// <param name="IsRound">是否有圆角</param>
  204. /// <param name="Radius">半径</param>
  205. public static void CreateGraphics(Graphics GObject, Rectangle RectSize, Point CenterPonit, int GraphicsType, bool IsRound, int Radius)
  206. {
  207. //工作区中心位置
  208. //外框尺寸
  209. //是否有圆角
  210. }
  211. #region 画圆角矩形
  212. internal static GraphicsPath CreateRoundedRectanglePath(Rectangle rect, int cornerRadius)
  213. {
  214. GraphicsPath roundedRect = new GraphicsPath();
  215. roundedRect.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  216. roundedRect.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  217. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  218. roundedRect.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  219. roundedRect.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  220. roundedRect.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.X + cornerRadius * 2, rect.Bottom);
  221. roundedRect.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  222. roundedRect.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);
  223. roundedRect.CloseFigure();
  224. return roundedRect;
  225. }
  226. #endregion
  227. }
  228. }