Drawing.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. using CommonLang;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using CommonLang.Vector;
  8. using System.Windows.Forms;
  9. namespace CommonFroms.Drawing
  10. {
  11. public static class DrawingUtils
  12. {
  13. public static void DrawCross(Graphics g, Pen pen, float cx, float cy, float cr)
  14. {
  15. g.DrawLine(pen, cx - cr, cy, cx + cr, cy);
  16. g.DrawLine(pen, cx, cy - cr, cx, cy + cr);
  17. }
  18. public static void DrawFan(Graphics g, Pen pen, float direction, float range, float angle)
  19. {
  20. if (angle != 0)
  21. {
  22. float startRadians = direction - angle / 2;
  23. float endRadians = direction + angle / 2;
  24. g.DrawArc(pen,
  25. -range,
  26. -range,
  27. range * 2,
  28. range * 2,
  29. CMath.RadiansToDegrees(startRadians),
  30. CMath.RadiansToDegrees(angle));
  31. g.DrawLine(pen, 0, 0,
  32. (float)Math.Cos(startRadians) * range,
  33. (float)Math.Sin(startRadians) * range);
  34. g.DrawLine(pen, 0, 0,
  35. (float)Math.Cos(endRadians) * range,
  36. (float)Math.Sin(endRadians) * range);
  37. }
  38. else
  39. {
  40. g.DrawLine(pen, 0, 0,
  41. (float)Math.Cos(direction) * range,
  42. (float)Math.Sin(direction) * range);
  43. }
  44. }
  45. public static void FillFan(Graphics g, Brush pen, float direction, float range, float angle)
  46. {
  47. float startRadians = direction - angle / 2;
  48. float endRadians = direction + angle / 2;
  49. g.FillPie(pen,
  50. -range,
  51. -range,
  52. range * 2,
  53. range * 2,
  54. CMath.RadiansToDegrees(startRadians),
  55. CMath.RadiansToDegrees(angle));
  56. }
  57. /// <summary>
  58. /// 画粗线条
  59. /// </summary>
  60. /// <param name="sx"></param>
  61. /// <param name="sy"></param>
  62. /// <param name="dx"></param>
  63. /// <param name="dy"></param>
  64. /// <param name="width"></param>
  65. public static void DrawLineRect(Graphics g, Pen pen, float sx, float sy, float dx, float dy, float line_r)
  66. {
  67. float direction = MathVector.getDegree(sx, sy, dx, dy);
  68. Vector2 s_l = new Vector2(sx, sy);
  69. Vector2 s_r = new Vector2(sx, sy);
  70. Vector2 d_l = new Vector2(dx, dy);
  71. Vector2 d_r = new Vector2(dx, dy);
  72. MathVector.movePolar(s_l, direction + CMath.PI_DIV_2, line_r);
  73. MathVector.movePolar(d_l, direction + CMath.PI_DIV_2, line_r);
  74. MathVector.movePolar(s_r, direction - CMath.PI_DIV_2, line_r);
  75. MathVector.movePolar(d_r, direction - CMath.PI_DIV_2, line_r);
  76. g.DrawLine(pen, s_l.X, s_l.Y, d_l.X, d_l.Y);
  77. g.DrawLine(pen, s_r.X, s_r.Y, d_r.X, d_r.Y);
  78. g.DrawLine(pen, s_l.X, s_l.Y, s_r.X, s_r.Y);
  79. g.DrawLine(pen, d_l.X, d_l.Y, d_r.X, d_r.Y);
  80. }
  81. public static void FillLineRect(Graphics g, Brush brush, float sx, float sy, float dx, float dy, float line_r)
  82. {
  83. float direction = MathVector.getDegree(sx, sy, dx, dy);
  84. Vector2 s_l = new Vector2(sx, sy);
  85. Vector2 s_r = new Vector2(sx, sy);
  86. Vector2 d_l = new Vector2(dx, dy);
  87. Vector2 d_r = new Vector2(dx, dy);
  88. MathVector.movePolar(s_l, direction + CMath.PI_DIV_2, line_r);
  89. MathVector.movePolar(d_l, direction + CMath.PI_DIV_2, line_r);
  90. MathVector.movePolar(s_r, direction - CMath.PI_DIV_2, line_r);
  91. MathVector.movePolar(d_r, direction - CMath.PI_DIV_2, line_r);
  92. g.FillPolygon(brush,
  93. new PointF[] {
  94. new PointF(s_l.X, s_l.Y),
  95. new PointF(s_r.X, s_r.Y),
  96. new PointF(d_r.X, d_r.Y),
  97. new PointF(d_l.X, d_l.Y)
  98. },
  99. System.Drawing.Drawing2D.FillMode.Winding);
  100. }
  101. /// <summary>
  102. /// 画圆角粗线条
  103. /// </summary>
  104. /// <param name="g"></param>
  105. /// <param name="pen"></param>
  106. /// <param name="sx"></param>
  107. /// <param name="sy"></param>
  108. /// <param name="dx"></param>
  109. /// <param name="dy"></param>
  110. /// <param name="size"></param>
  111. public static void DrawLineRoundRect(Graphics g, Pen pen, float sx, float sy, float dx, float dy, float line_r)
  112. {
  113. float direction = MathVector.getDegree(sx, sy, dx, dy);
  114. float r = line_r;
  115. float size = r * 2;
  116. Vector2 s_l = new Vector2(sx, sy);
  117. Vector2 s_r = new Vector2(sx, sy);
  118. Vector2 d_l = new Vector2(dx, dy);
  119. Vector2 d_r = new Vector2(dx, dy);
  120. MathVector.movePolar(s_l, direction + CMath.PI_DIV_2, r);
  121. MathVector.movePolar(d_l, direction + CMath.PI_DIV_2, r);
  122. MathVector.movePolar(s_r, direction - CMath.PI_DIV_2, r);
  123. MathVector.movePolar(d_r, direction - CMath.PI_DIV_2, r);
  124. g.DrawLine(pen, s_l.X, s_l.Y, d_l.X, d_l.Y);
  125. g.DrawLine(pen, s_r.X, s_r.Y, d_r.X, d_r.Y);
  126. float angle = CMath.RadiansToDegrees(direction);
  127. g.DrawArc(pen, sx - r, sy - r, size, size, angle + 180 - 90, 180);
  128. g.DrawArc(pen, dx - r, dy - r, size, size, angle + 360 - 90, 180);
  129. }
  130. public static void FillLineRoundRect(Graphics g, Brush brush, float sx, float sy, float dx, float dy, float line_r)
  131. {
  132. float direction = MathVector.getDegree(sx, sy, dx, dy);
  133. float r = line_r;
  134. float size = r * 2;
  135. Vector2 s_l = new Vector2(sx, sy);
  136. Vector2 s_r = new Vector2(sx, sy);
  137. Vector2 d_l = new Vector2(dx, dy);
  138. Vector2 d_r = new Vector2(dx, dy);
  139. MathVector.movePolar(s_l, direction + CMath.PI_DIV_2, r);
  140. MathVector.movePolar(d_l, direction + CMath.PI_DIV_2, r);
  141. MathVector.movePolar(s_r, direction - CMath.PI_DIV_2, r);
  142. MathVector.movePolar(d_r, direction - CMath.PI_DIV_2, r);
  143. g.FillPolygon(brush,
  144. new PointF[] {
  145. new PointF(s_l.X, s_l.Y),
  146. new PointF(s_r.X, s_r.Y),
  147. new PointF(d_r.X, d_r.Y),
  148. new PointF(d_l.X, d_l.Y)
  149. },
  150. System.Drawing.Drawing2D.FillMode.Winding);
  151. float angle = CMath.RadiansToDegrees(direction);
  152. g.FillPie(brush, sx - r, sy - r, size, size, angle + 180 - 90, 180);
  153. g.FillPie(brush, dx - r, dy - r, size, size, angle + 360 - 90, 180);
  154. }
  155. public static PointF[] GenCursor(float sx, float sy, float dx, float dy, float width, float cursor_width, float cursor_height)
  156. {
  157. if (cursor_width < width)
  158. cursor_width = width;
  159. float ox = sx - dx;
  160. float oy = sy - dy;
  161. float ds = CMath.getDistance(sx, sy, dx, dy);
  162. float hw = width / 2f;
  163. float cw = cursor_width / 2;
  164. float od = (float)Math.Atan2(oy, ox);
  165. PointF sC = new Point(0, 0);
  166. PointF sL = new PointF(-hw, 0);
  167. PointF sR = new PointF(+hw, 0);
  168. PointF dC = new PointF(0, ds);
  169. PointF dL = new PointF(-hw, ds - cursor_height);
  170. PointF dR = new PointF(+hw, ds - cursor_height);
  171. PointF dLL = new PointF(-cw, ds - cursor_height);
  172. PointF dRR = new PointF(+cw, ds - cursor_height);
  173. PointF[] points = new PointF[] { sC, sL, dL, dLL, dC, dRR, dR, sR, sC };
  174. for (int i = 0; i < points.Length; i++)
  175. {
  176. float x = points[i].X;
  177. float y = points[i].Y;
  178. MathVector.rotate(ref x, ref y, 0, 0, od + CMath.PI_DIV_2);
  179. points[i].X = x + sx;
  180. points[i].Y = y + sy;
  181. }
  182. return points;
  183. }
  184. public static void FillCursor(Graphics g, Brush brush, float sx, float sy, float dx, float dy, float width, float cursor_width = 0, float cursor_height = 0)
  185. {
  186. PointF[] points = GenCursor(sx, sy, dx, dy, width, cursor_width, cursor_height);
  187. System.Drawing.Drawing2D.GraphicsState gs = g.Save();
  188. try
  189. {
  190. g.FillPolygon(brush, points);
  191. }
  192. finally
  193. {
  194. g.Restore(gs);
  195. }
  196. }
  197. public static void DrawCursor(Graphics g, Pen pen, float sx, float sy, float dx, float dy, float width, float cursor_width = 0, float cursor_height = 0)
  198. {
  199. PointF[] points = GenCursor(sx, sy, dx, dy, width, cursor_width, cursor_height);
  200. System.Drawing.Drawing2D.GraphicsState gs = g.Save();
  201. try
  202. {
  203. g.DrawPolygon(pen, points);
  204. }
  205. finally
  206. {
  207. g.Restore(gs);
  208. }
  209. }
  210. }
  211. public abstract class Drawable
  212. {
  213. public int spacing = 2;
  214. public StringFormat format = new StringFormat(StringFormat.GenericDefault);
  215. public Font font = Form.DefaultFont;
  216. public AnchorStyles anchor = AnchorStyles.Left | AnchorStyles.Top;
  217. public virtual float Draw(Graphics g, float sx, float sy, float sw, float sh)
  218. {
  219. sx = sx - (((anchor & AnchorStyles.Right) != 0) ? sw : 0);
  220. sy = sy - (((anchor & AnchorStyles.Bottom) != 0) ? sh : 0);
  221. float x = sx + spacing;
  222. float y = sy + spacing;
  223. float w = sw - (spacing << 1);
  224. float h = sh - (spacing << 1);
  225. var clip = new RectangleF(x, y, w, h);
  226. g.SetClip(clip);
  227. try
  228. {
  229. DrawSelf(g, x, y, w, h);
  230. }
  231. finally
  232. {
  233. g.ResetClip();
  234. }
  235. return ((anchor & AnchorStyles.Bottom) != 0) ? -sh : sh;
  236. }
  237. protected abstract void DrawSelf(Graphics g, float x, float y, float w, float h);
  238. }
  239. public class GaugeRectFan : Drawable
  240. {
  241. public Brush body_brush = new SolidBrush(Color.FromArgb(0xFF, 0x40, 0x40, 0x40));
  242. public Brush border_brush = new SolidBrush(Color.Gray);
  243. public Brush cd_brush = new SolidBrush(Color.FromArgb(0x80, 0xFF, 0xFF, 0xFF));
  244. public Brush text_brush = new SolidBrush(Color.White);
  245. public int border = 1;
  246. public string text1;
  247. public string text2;
  248. public float percent;
  249. public GaugeRectFan SetText(string t1, string t2)
  250. {
  251. this.text1 = t1;
  252. this.text2 = t2;
  253. return this;
  254. }
  255. public GaugeRectFan SetPercent(float pct)
  256. {
  257. this.percent = pct;
  258. return this;
  259. }
  260. protected override void DrawSelf(Graphics g, float x, float y, float w, float h)
  261. {
  262. g.FillRectangle(border_brush, x, y, w, h);
  263. x += border;
  264. y += border;
  265. w -= (border << 1);
  266. h -= (border << 1);
  267. g.FillRectangle(body_brush, x, y, w, h);
  268. if (percent > 0f && percent < 1f)
  269. {
  270. g.FillPie(cd_brush, x - w, y - h, w * 3, h * 3, -90, 360 * percent);
  271. }
  272. if (!string.IsNullOrEmpty(text1) && string.IsNullOrEmpty(text2))
  273. {
  274. format.Alignment = StringAlignment.Center;
  275. format.LineAlignment = StringAlignment.Center;
  276. g.DrawString(text1, font, text_brush, x, y + border, StringFormat.GenericDefault);
  277. }
  278. else
  279. {
  280. x += 1;
  281. y += 1;
  282. w -= 2;
  283. h -= 2;
  284. if (!string.IsNullOrEmpty(text1))
  285. {
  286. format.Alignment = StringAlignment.Near;
  287. format.LineAlignment = StringAlignment.Near;
  288. g.DrawString(text1, font, text_brush, x, y + border, StringFormat.GenericDefault);
  289. }
  290. if (!string.IsNullOrEmpty(text2))
  291. {
  292. format.Alignment = StringAlignment.Near;
  293. format.LineAlignment = StringAlignment.Far;
  294. g.DrawString(text2, font, text_brush, new RectangleF(x, y, w, h - border), format);
  295. }
  296. }
  297. }
  298. }
  299. public class GaugeStrip : Drawable
  300. {
  301. public Brush back_brush = new SolidBrush(Color.FromArgb(0xFF, 0x40, 0x40, 0x40));
  302. public Brush cd_brush = new SolidBrush(Color.FromArgb(0xA0, 0x20, 0xFF, 0x20));
  303. public Brush text_brush = new SolidBrush(Color.White);
  304. public int border = 1;
  305. public string text;
  306. public float percent;
  307. public GaugeStrip SetText(string t)
  308. {
  309. this.text = t;
  310. return this;
  311. }
  312. public GaugeStrip SetPercent(float pct)
  313. {
  314. this.percent = pct;
  315. return this;
  316. }
  317. public override float Draw(Graphics g, float sx, float sy, float sw, float sh)
  318. {
  319. var trect = g.MeasureString(text, font);
  320. sw = Math.Max(sw, trect.Width + (spacing << 1) + 2);
  321. sh = Math.Max(sh, trect.Height + (spacing << 1) + 2);
  322. return base.Draw(g, sx, sy, sw, sh);
  323. }
  324. protected override void DrawSelf(Graphics g, float x, float y, float w, float h)
  325. {
  326. g.FillRectangle(back_brush, x, y, w, h);
  327. x += border;
  328. y += border;
  329. w -= (border << 1);
  330. h -= (border << 1);
  331. if (percent > 0f && percent < 1f)
  332. {
  333. g.FillRectangle(cd_brush, x, y, w * percent, h);
  334. }
  335. g.DrawString(text, font, text_brush, x + 1, y + 1, format);
  336. }
  337. }
  338. public class TextLine : Drawable
  339. {
  340. public string text;
  341. public Brush text_brush = new SolidBrush(Color.White);
  342. public TextLine SetText(string t)
  343. {
  344. this.text = t;
  345. return this;
  346. }
  347. public override float Draw(Graphics g, float sx, float sy, float sw, float sh)
  348. {
  349. var trect = g.MeasureString(text, font);
  350. sw = Math.Max(sw, trect.Width + (spacing << 1));
  351. sh = Math.Max(sh, trect.Height + (spacing << 1));
  352. return base.Draw(g, sx, sy, sw, sh);
  353. }
  354. protected override void DrawSelf(Graphics g, float x, float y, float w, float h)
  355. {
  356. g.DrawString(text, font, text_brush, x, y, format);
  357. }
  358. }
  359. public class TextRect : Drawable
  360. {
  361. public string text;
  362. public Brush text_brush = new SolidBrush(Color.White);
  363. public TextRect SetText(string t)
  364. {
  365. this.text = t;
  366. return this;
  367. }
  368. public override float Draw(Graphics g, float sx, float sy, float sw, float sh)
  369. {
  370. var trect = g.MeasureString(text, font, (int)sw - (spacing << 1));
  371. sw = Math.Max(sw, trect.Width + (spacing << 1));
  372. sh = Math.Max(sh, trect.Height + (spacing << 1));
  373. return base.Draw(g, sx, sy, sw, sh);
  374. }
  375. protected override void DrawSelf(Graphics g, float x, float y, float w, float h)
  376. {
  377. g.DrawString(text, font, text_brush, new RectangleF(x, y, w, h), format);
  378. }
  379. }
  380. }