You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

718 lines
27 KiB

1 month ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.Drawing.Text;
  9. using System.Drawing.Imaging;
  10. using System.Windows.Forms;
  11. namespace Customize.Controls
  12. {
  13. internal class GDIHelper
  14. {
  15. #region InitializeGraphics
  16. /// <summary>
  17. /// 初始化Graphics对象为高质量的绘制
  18. /// </summary>
  19. /// <param name="g">The g.</param>
  20. public static void InitializeGraphics(Graphics g)
  21. {
  22. if (g != null)
  23. {
  24. g.SmoothingMode = SmoothingMode.AntiAlias;
  25. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  26. g.CompositingQuality = CompositingQuality.HighQuality;
  27. }
  28. }
  29. #endregion
  30. #region DrawImage
  31. /// <summary>
  32. /// 在指定区域绘制图片(可设置图片透明度) (平铺绘制)
  33. /// Draws the image.
  34. /// </summary>
  35. /// <param name="g">The g.</param>
  36. /// <param name="rect">The rect.</param>
  37. /// <param name="img">The img.</param>
  38. public static void DrawImage(Graphics g, Rectangle rect, Image img, float opacity)
  39. {
  40. if (opacity <= 0)
  41. {
  42. return;
  43. }
  44. using (ImageAttributes imgAttributes = new ImageAttributes())
  45. {
  46. GDIHelper.SetImageOpacity(imgAttributes, opacity >= 1 ? 1 : opacity);
  47. Rectangle imageRect = new Rectangle(rect.X, rect.Y + rect.Height / 2 - img.Size.Height / 2, img.Size.Width, img.Size.Height);
  48. g.DrawImage(img, rect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, imgAttributes);
  49. }
  50. }
  51. /// <summary>
  52. /// 绘制简单图片(平铺绘制)
  53. /// </summary>
  54. /// <param name="g">The g.</param>
  55. /// <param name="rect">The rect.</param>
  56. /// <param name="img">The img.</param>
  57. public static void DrawImage(Graphics g, Rectangle rect, Image img)
  58. {
  59. Rectangle imageRect = new Rectangle(rect.X, rect.Y + rect.Height / 2 - img.Size.Height / 2, img.Size.Width, img.Size.Height);
  60. g.DrawImage(img, rect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
  61. }
  62. /// <summary>
  63. /// 按照指定区域绘制指定大小的图片
  64. /// </summary>
  65. /// <param name="g">The g.</param>
  66. /// <param name="rect">The rect.</param>
  67. /// <param name="img">The img.</param>
  68. /// <param name="imgSize">Size of the img.</param>
  69. public static void DrawImage(Graphics g, Rectangle rect, Image img, Size imgSize)
  70. {
  71. if (g == null || img == null)
  72. {
  73. return;
  74. }
  75. int x = rect.X + rect.Width / 2 - imgSize.Width / 2, y = rect.Y;
  76. Rectangle imageRect = new Rectangle(x, y + rect.Height / 2 - imgSize.Height / 2, imgSize.Width, imgSize.Height);
  77. g.DrawImage(img, imageRect);
  78. }
  79. #endregion
  80. #region Draw image and string
  81. /// <summary>
  82. /// 在指定的区域绘制绘制图像和文字
  83. /// </summary>
  84. /// <param name="g">The g.</param>
  85. /// <param name="roundRect">The roundRect.</param>
  86. /// <param name="image">The image.</param>
  87. /// <param name="imageSize">Size of the image.</param>
  88. /// <param name="text">The text.</param>
  89. /// <param name="font">The font.</param>
  90. /// <param name="forceColor">Color of the force.</param>
  91. public static void DrawImageAndString(Graphics g, Rectangle rect, Image image, Size imageSize, string text, Font font, Color forceColor)
  92. {
  93. int x = rect.X, y = rect.Y, len;
  94. SizeF sf = g.MeasureString(text, font);
  95. len = Convert.ToInt32(sf.Width);
  96. x += rect.Width / 2 - len / 2;
  97. if (image != null)
  98. {
  99. x -= imageSize.Width / 2;
  100. Rectangle imageRect = new Rectangle(x, y + rect.Height / 2 - imageSize.Height / 2, imageSize.Width, imageSize.Height);
  101. g.DrawImage(image, imageRect);
  102. x += imageSize.Width + 2;
  103. }
  104. g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
  105. using (SolidBrush brush = new SolidBrush(forceColor))
  106. {
  107. g.DrawString(text, font, brush, x, y + rect.Height / 2 - Convert.ToInt32(sf.Height) / 2 + 2);
  108. }
  109. }
  110. #endregion
  111. #region FillRectangle
  112. /// <summary>
  113. /// 渲染一个矩形区域(渐变色)
  114. /// Fills the rectangle.
  115. /// </summary>
  116. /// <param name="g">The g.</param>
  117. /// <param name="rect">The rect.</param>
  118. /// <param name="color">The color.</param>
  119. public static void FillRectangle(Graphics g, Rectangle rect, GradientColor color)
  120. {
  121. if (rect.Width <= 0 || rect.Height <= 0 || g == null)
  122. {
  123. return;
  124. }
  125. using (LinearGradientBrush brush = new LinearGradientBrush(rect, color.First, color.Second, LinearGradientMode.Vertical))
  126. {
  127. brush.Blend.Factors = color.Factors;
  128. brush.Blend.Positions = color.Positions;
  129. g.FillRectangle(brush, rect);
  130. }
  131. }
  132. /// <summary>
  133. /// 渲染一个矩形区域(单色)
  134. /// Fills the rectangle.
  135. /// </summary>
  136. /// <param name="g">The g.</param>
  137. /// <param name="rect">The rect.</param>
  138. /// <param name="color">The color.</param>
  139. public static void FillRectangle(Graphics g, Rectangle rect, Color color)
  140. {
  141. if (rect.Width <= 0 || rect.Height <= 0 || g == null)
  142. {
  143. return;
  144. }
  145. using (Brush brush = new SolidBrush(color))
  146. {
  147. g.FillRectangle(brush, rect);
  148. }
  149. }
  150. /// <summary>
  151. /// 渲染一个圆角矩形区域(渐变色)
  152. /// Fills the rectangle.
  153. /// </summary>
  154. /// <param name="g">The g.</param>
  155. /// <param name="roundRect">The round rect.</param>
  156. /// <param name="color">The color.</param>
  157. public static void FillRectangle(Graphics g, RoundRectangle roundRect, GradientColor color)
  158. {
  159. if (roundRect.Rect.Width <= 0 || roundRect.Rect.Height <= 0)
  160. {
  161. return;
  162. }
  163. using (GraphicsPath path = roundRect.ToGraphicsBezierPath())
  164. {
  165. using (LinearGradientBrush brush = new LinearGradientBrush(roundRect.Rect, color.First, color.Second, LinearGradientMode.Vertical))
  166. {
  167. brush.Blend.Factors = color.Factors;
  168. brush.Blend.Positions = color.Positions;
  169. g.FillPath(brush, path);
  170. }
  171. }
  172. }
  173. /// <summary>
  174. /// 渲染一个圆角矩形区域(单色)
  175. /// Fills the rectangle.
  176. /// </summary>
  177. /// <param name="g">The g.</param>
  178. /// <param name="roundRect">The round rect.</param>
  179. /// <param name="color">The color.</param>
  180. public static void FillRectangle(Graphics g, RoundRectangle roundRect, Color color)
  181. {
  182. if (roundRect.Rect.Width <= 0 || roundRect.Rect.Height <= 0)
  183. {
  184. return;
  185. }
  186. using (GraphicsPath path = roundRect.ToGraphicsBezierPath())
  187. {
  188. using (Brush brush = new SolidBrush(color))
  189. {
  190. g.FillPath(brush, path);
  191. }
  192. }
  193. }
  194. #endregion
  195. #region FillPath
  196. /// <summary>
  197. /// 使用渐变色渲染一个图形区域
  198. /// </summary>
  199. /// <param name="g">The g.</param>
  200. /// <param name="path">The path.</param>
  201. /// <param name="rect">The rect.</param>
  202. /// <param name="color">The color.</param>
  203. public static void FillPath(Graphics g, GraphicsPath path, Rectangle rect, GradientColor color)
  204. {
  205. using (LinearGradientBrush brush = new LinearGradientBrush(rect, color.First, color.Second, LinearGradientMode.Vertical))
  206. {
  207. brush.Blend.Factors = color.Factors;
  208. brush.Blend.Positions = color.Positions;
  209. g.FillPath(brush, path);
  210. }
  211. }
  212. /// <summary>
  213. /// 渲染圆角矩形区域(级渲染)
  214. /// </summary>
  215. /// <param name="g">The Graphics.</param>
  216. /// <param name="roundRect">The RoundRectangle.</param>
  217. /// <param name="color1">The color1.</param>
  218. /// <param name="color2">The color2.</param>
  219. /// <param name="blend">色彩混合渲染方案</param>
  220. public static void FillPath(Graphics g, RoundRectangle roundRect, Color color1, Color color2, Blend blend)
  221. {
  222. GradientColor color = new GradientColor(color1, color2, blend.Factors, blend.Positions);
  223. GDIHelper.FillRectangle(g, roundRect, color);
  224. }
  225. /// <summary>
  226. /// 使用渐变色填充区域
  227. /// </summary>
  228. /// <param name="g">The g.</param>
  229. /// <param name="roundRect">The round rect.</param>
  230. /// <param name="color1">The color1.</param>
  231. /// <param name="color2">The color2.</param>
  232. public static void FillPath(Graphics g, RoundRectangle roundRect, Color color1, Color color2)
  233. {
  234. if (roundRect.Rect.Width <= 0 || roundRect.Rect.Height <= 0)
  235. {
  236. return;
  237. }
  238. using (GraphicsPath path = roundRect.ToGraphicsBezierPath())
  239. {
  240. using (LinearGradientBrush brush = new LinearGradientBrush(roundRect.Rect, color1, color2, LinearGradientMode.Vertical))
  241. {
  242. g.FillPath(brush, path);
  243. }
  244. }
  245. }
  246. #endregion
  247. #region DrawPathBorder
  248. /// <summary>
  249. /// 绘制一个图形区域的边框
  250. /// </summary>
  251. /// <param name="g">The g.</param>
  252. /// <param name="path">The path.</param>
  253. /// <param name="color">The color.</param>
  254. public static void DrawPathBorder(Graphics g, GraphicsPath path, Color color)
  255. {
  256. using (Pen pen = new Pen(color, 1))
  257. {
  258. g.DrawPath(pen, path);
  259. }
  260. }
  261. /// <summary>
  262. /// 绘制指定区域路径的边框.
  263. /// 注意:若要设置边框宽度,需要自己调整区域,它是向外绘制的
  264. /// </summary>
  265. /// <param name="g">The Graphics.</param>
  266. /// <param name="roundRect">The RoundRectangle.</param>
  267. /// <param name="color">The color.</param>
  268. /// <param name="borderWidth">Width of the border.</param>
  269. public static void DrawPathBorder(Graphics g, RoundRectangle roundRect, Color color, int borderWidth)
  270. {
  271. using (GraphicsPath path = roundRect.ToGraphicsBezierPath())
  272. {
  273. using (Pen pen = new Pen(color, borderWidth))
  274. {
  275. g.DrawPath(pen, path);
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// 绘制指定区域路径的边框
  281. /// </summary>
  282. public static void DrawPathBorder(Graphics g, RoundRectangle roundRect, Color color)
  283. {
  284. DrawPathBorder(g, roundRect, color, 1);
  285. }
  286. /// <summary>
  287. /// 绘制指定区域路径的边框
  288. /// </summary>
  289. public static void DrawPathInnerBorder(Graphics g, RoundRectangle roundRect, Color color)
  290. {
  291. Rectangle rect = roundRect.Rect;
  292. rect.X++; rect.Y++; rect.Width -= 2; rect.Height -= 2;
  293. DrawPathBorder(g, new RoundRectangle(rect, roundRect.CornerRadius), color);
  294. }
  295. /// <summary>
  296. /// 绘制指定区域路径的边框
  297. /// </summary>
  298. public static void DrawPathInnerBorder(Graphics g, RoundRectangle roundRect)
  299. {
  300. Color c = Color.FromArgb(232, 218, 222);
  301. ////c = Color.Green;
  302. DrawPathInnerBorder(g, roundRect, c);
  303. }
  304. /// <summary>
  305. /// 绘制指定区域路径的边框
  306. /// </summary>
  307. public static void DrawPathOuterBorder(Graphics g, RoundRectangle roundRect, Color color)
  308. {
  309. Rectangle rect = roundRect.Rect;
  310. rect.X--; rect.Y--; rect.Width += 2; rect.Height += 2;
  311. DrawPathBorder(g, new RoundRectangle(rect, roundRect.CornerRadius), color);
  312. }
  313. /// <summary>
  314. /// 绘制指定区域路径的边框
  315. /// </summary>
  316. public static void DrawPathOuterBorder(Graphics g, RoundRectangle roundRect, Color color, int borderWidth)
  317. {
  318. Rectangle rect = roundRect.Rect;
  319. rect.X--; rect.Y--; rect.Width += 2; rect.Height += 2;
  320. DrawPathBorder(g, new RoundRectangle(rect, roundRect.CornerRadius), color, borderWidth);
  321. }
  322. /// <summary>
  323. /// 绘制指定区域路径的边框
  324. /// </summary>
  325. public static void DrawPathBorder(Graphics g, RoundRectangle roundRect)
  326. {
  327. DrawPathBorder(g, roundRect, Color.FromArgb(221, 221, 221));
  328. }
  329. #endregion
  330. #region DrawGradientLine
  331. /// <summary>
  332. /// 绘制阶梯渐变的线条,可以在参数Blend对象中设置色彩混合规则
  333. /// </summary>
  334. public static void DrawGradientLine(Graphics g, Color lineColor, Blend blend, int angle, int lineWidth, int x1, int y1, int x2, int y2)
  335. {
  336. Color c1 = lineColor;
  337. Color c2 = Color.FromArgb(10, c1);
  338. Rectangle rect = new Rectangle(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
  339. using (LinearGradientBrush brush = new LinearGradientBrush(rect, c1, c2, angle))
  340. {
  341. brush.Blend = blend;
  342. using (Pen pen = new Pen(brush, lineWidth))
  343. {
  344. g.SmoothingMode = SmoothingMode.AntiAlias;
  345. g.DrawLine(pen, x1, y1, x2, y2);
  346. }
  347. }
  348. }
  349. /// <summary>
  350. /// 绘制向两边阶梯渐变的线条
  351. /// </summary>
  352. public static void DrawGradientLine(Graphics g, Color lineColor, int angle, int x1, int y1, int x2, int y2)
  353. {
  354. Blend blend = new Blend();
  355. blend.Positions = new float[] { 0f, .15f, .5f, .85f, 1f };
  356. blend.Factors = new float[] { 1f, .4f, 0f, .4f, 1f };
  357. DrawGradientLine(g, lineColor, blend, angle, 1, x1, y1, x2, y2);
  358. }
  359. #endregion
  360. #region 设置图片透明度(SetImageOpacity)
  361. /// <summary>
  362. /// 设置图片透明度.
  363. /// </summary>
  364. /// <param name="imgAttributes">The ImageAttributes.</param>
  365. /// <param name="opacity">透明度,0完全透明,1不透明(The opacity.)</param>
  366. public static void SetImageOpacity(ImageAttributes imgAttributes, float opacity)
  367. {
  368. float[][] nArray ={ new float[] {1, 0, 0, 0, 0},
  369. new float[] {0, 1, 0, 0, 0},
  370. new float[] {0, 0, 1, 0, 0},
  371. new float[] {0, 0, 0, opacity, 0},
  372. new float[] {0, 0, 0, 0, 1}};
  373. ColorMatrix matrix = new ColorMatrix(nArray);
  374. imgAttributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
  375. }
  376. #endregion
  377. #region DrawArrow
  378. /// <summary>
  379. /// 绘制指针(方向箭头),默认指针颜色为(55, 63, 78)
  380. /// </summary>
  381. /// <param name="g">The Graphics.</param>
  382. /// <param name="direction">指针的方向.</param>
  383. /// <param name="rect">绘制的区域.</param>
  384. /// <param name="arrowSize">指针的大小,即长宽.</param>
  385. public static void DrawArrow(Graphics g, ArrowDirection direction, Rectangle rect, Size arrowSize)
  386. {
  387. float offset = 1.8f;
  388. DrawArrow(g, direction, rect, arrowSize, offset, Color.FromArgb(55, 63, 78));
  389. }
  390. /// <summary>
  391. /// 绘制指针(方向箭头)
  392. /// </summary>
  393. /// <param name="g">The Graphics.</param>
  394. /// <param name="direction">指针的方向.</param>
  395. /// <param name="rect">绘制的区域.</param>
  396. /// <param name="arrowSize">指针的大小,即长宽.</param>
  397. /// <param name="c">指针颜色</param>
  398. public static void DrawArrow(Graphics g, ArrowDirection direction, Rectangle rect, Size arrowSize, float offset, Color c)
  399. {
  400. Point center = new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2);
  401. using (GraphicsPath path = new GraphicsPath())
  402. {
  403. PointF[] points = null;
  404. switch (direction)
  405. {
  406. case ArrowDirection.Down:
  407. points = new PointF[] {
  408. new PointF(center.X,center.Y+arrowSize.Height/2),
  409. new PointF(center.X-arrowSize.Width/2,center.Y-arrowSize.Height/2),
  410. new PointF(center.X,center.Y-arrowSize.Height/2+offset),
  411. new PointF(center.X+arrowSize.Width/2,center.Y-arrowSize.Height/2),};
  412. break;
  413. case ArrowDirection.Up:
  414. points = new PointF[] {
  415. new PointF(center.X,center.Y-arrowSize.Height/2),
  416. new PointF(center.X-arrowSize.Width/2,center.Y+arrowSize.Height/2),
  417. new PointF(center.X,center.Y+arrowSize.Height/2-offset),
  418. new Point(center.X+arrowSize.Width/2,center.Y+arrowSize.Height/2),};
  419. break;
  420. case ArrowDirection.Left:
  421. points = new PointF[] {
  422. new PointF(center.X-arrowSize.Width/2,center.Y),
  423. new PointF(center.X+arrowSize.Width/2,center.Y-arrowSize.Height/2),
  424. new PointF(center.X+arrowSize.Width/2-offset,center.Y),
  425. new PointF(center.X+arrowSize.Width/2,center.Y+arrowSize.Height/2),};
  426. break;
  427. case ArrowDirection.Right:
  428. points = new PointF[] {
  429. new PointF(center.X+arrowSize.Width/2,center.Y),
  430. new PointF(center.X-arrowSize.Width/2,center.Y-arrowSize.Height/2),
  431. new PointF(center.X-arrowSize.Width/2+offset,center.Y),
  432. new PointF(center.X-arrowSize.Width/2,center.Y+arrowSize.Height/2),};
  433. break;
  434. }
  435. path.AddLines(points);
  436. using (Brush brush = new SolidBrush(c))
  437. {
  438. g.FillPath(brush, path);
  439. }
  440. }
  441. }
  442. #endregion
  443. #region DrawCrystalButton
  444. /// <summary>
  445. /// 绘制水晶原形按钮
  446. /// </summary>
  447. /// <param name="g">The Graphics.</param>
  448. /// <param name="rect">The Rectangle.</param>
  449. /// <param name="surroundColor">Color of the surround.</param>
  450. /// <param name="centerColor">Color of the center.</param>
  451. /// <param name="lightColor">Color of the light.</param>
  452. /// <param name="blend">The blend.</param>
  453. public static void DrawCrystalButton(Graphics g, Rectangle rect, Color surroundColor, Color centerColor, Color lightColor, Blend blend)
  454. {
  455. int sweep, start;
  456. Point p1, p2, p3;
  457. Rectangle rinner = rect;
  458. rinner.Inflate(-1, -1);
  459. using (GraphicsPath p = new GraphicsPath())
  460. {
  461. p.AddEllipse(rect);
  462. using (PathGradientBrush gradient = new PathGradientBrush(p))
  463. {
  464. gradient.WrapMode = WrapMode.Clamp;
  465. gradient.CenterPoint = new PointF(Convert.ToSingle(rect.Left + rect.Width / 2), Convert.ToSingle(rect.Bottom));
  466. gradient.CenterColor = centerColor;
  467. gradient.SurroundColors = new Color[] { surroundColor };
  468. gradient.Blend = blend;
  469. g.FillPath(gradient, p);
  470. }
  471. }
  472. // Bottom round shine
  473. Rectangle bshine = new Rectangle(0, 0, rect.Width / 2, rect.Height / 2);
  474. bshine.X = rect.X + (rect.Width - bshine.Width) / 2;
  475. bshine.Y = rect.Y + rect.Height / 2;
  476. using (GraphicsPath p = new GraphicsPath())
  477. {
  478. p.AddEllipse(bshine);
  479. using (PathGradientBrush gradient = new PathGradientBrush(p))
  480. {
  481. gradient.WrapMode = WrapMode.Clamp;
  482. gradient.CenterPoint = new PointF(Convert.ToSingle(rect.Left + rect.Width / 2), Convert.ToSingle(rect.Bottom));
  483. gradient.CenterColor = Color.White;
  484. gradient.SurroundColors = new Color[] { Color.Transparent };
  485. g.FillPath(gradient, p);
  486. }
  487. }
  488. // Upper Glossy
  489. using (GraphicsPath p = new GraphicsPath())
  490. {
  491. sweep = 160;
  492. start = 180 + (180 - sweep) / 2;
  493. p.AddArc(rinner, start, sweep);
  494. p1 = Point.Round(p.PathData.Points[0]);
  495. p2 = Point.Round(p.PathData.Points[p.PathData.Points.Length - 1]);
  496. p3 = new Point(rinner.Left + rinner.Width / 2, p2.Y - 3);
  497. p.AddCurve(new Point[] { p2, p3, p1 });
  498. using (PathGradientBrush gradient = new PathGradientBrush(p))
  499. {
  500. gradient.WrapMode = WrapMode.Clamp;
  501. gradient.CenterPoint = p3;
  502. gradient.CenterColor = Color.Transparent;
  503. gradient.SurroundColors = new Color[] { lightColor };
  504. blend = new Blend(3);
  505. blend.Factors = new float[] { .3f, .8f, 1f };
  506. blend.Positions = new float[] { 0, 0.50f, 1f };
  507. gradient.Blend = blend;
  508. g.FillPath(gradient, p);
  509. }
  510. using (LinearGradientBrush b = new LinearGradientBrush(new Point(rect.Left, rect.Top), new Point(rect.Left, p1.Y), Color.White, Color.Transparent))
  511. {
  512. blend = new Blend(4);
  513. blend.Factors = new float[] { 0f, .4f, .8f, 1f };
  514. blend.Positions = new float[] { 0f, .3f, .4f, 1f };
  515. b.Blend = blend;
  516. g.FillPath(b, p);
  517. }
  518. }
  519. // Upper shine
  520. using (GraphicsPath p = new GraphicsPath())
  521. {
  522. sweep = 160;
  523. start = 180 + (180 - sweep) / 2;
  524. p.AddArc(rinner, start, sweep);
  525. using (Pen pen = new Pen(Color.White))
  526. {
  527. g.DrawPath(pen, p);
  528. }
  529. }
  530. // Lower Shine
  531. using (GraphicsPath p = new GraphicsPath())
  532. {
  533. sweep = 160;
  534. start = (180 - sweep) / 2;
  535. p.AddArc(rinner, start, sweep);
  536. Point pt = Point.Round(p.PathData.Points[0]);
  537. Rectangle rrinner = rinner; rrinner.Inflate(-1, -1);
  538. sweep = 160;
  539. start = (180 - sweep) / 2;
  540. p.AddArc(rrinner, start, sweep);
  541. using (LinearGradientBrush b = new LinearGradientBrush(
  542. new Point(rinner.Left, rinner.Bottom),
  543. new Point(rinner.Left, pt.Y - 1),
  544. lightColor, Color.FromArgb(50, lightColor)))
  545. {
  546. g.FillPath(b, p);
  547. }
  548. }
  549. }
  550. #endregion
  551. #region EllipseRender
  552. /// <summary>
  553. ///绘制椭圆的边框
  554. /// </summary>
  555. public static void DrawEllipseBorder(Graphics g, Rectangle rect, Color color, int borderWidth)
  556. {
  557. using (Pen pen = new Pen(color, borderWidth))
  558. {
  559. g.DrawEllipse(pen, rect);
  560. }
  561. }
  562. /// <summary>
  563. /// 渲染一个圆形区域(简单渲染)
  564. /// </summary>
  565. public static void FillEllipse(Graphics g, Rectangle rect, Color color)
  566. {
  567. using (SolidBrush brush = new SolidBrush(color))
  568. {
  569. g.FillEllipse(brush, rect);
  570. }
  571. }
  572. /// <summary>
  573. /// 渲染一个圆形区域(高级渲染)
  574. /// </summary>
  575. public static void FillEllipse(Graphics g, Rectangle rect, Color color1, Color color2)
  576. {
  577. using (GraphicsPath path = new GraphicsPath())
  578. {
  579. path.AddEllipse(rect);
  580. using (PathGradientBrush brush = new PathGradientBrush(path))
  581. {
  582. brush.CenterColor = color1;
  583. brush.SurroundColors = new Color[] { color2 };
  584. Blend blend = new Blend();
  585. blend.Factors = new float[] { 0f, 0.8f, 1f };
  586. blend.Positions = new float[] { 0f, 0.5f, 1f };
  587. brush.Blend = blend;
  588. g.FillPath(brush, path);
  589. }
  590. }
  591. }
  592. #endregion
  593. #region DrawCheckBox
  594. /// <summary>
  595. /// 绘制选择状态(勾选状态)
  596. /// 手动绘制勾勾
  597. /// </summary>
  598. public static void DrawCheckedState(Graphics g, Rectangle rect, Color color)
  599. {
  600. PointF[] points = new PointF[3];
  601. points[0] = new PointF(
  602. rect.X + rect.Width / 5f,
  603. rect.Y + rect.Height / 2.5f);
  604. points[1] = new PointF(
  605. rect.X + rect.Width / 2.5f,
  606. rect.Bottom - rect.Height / 3.6f);
  607. points[2] = new PointF(
  608. rect.Right - rect.Width / 5.0f,
  609. rect.Y + rect.Height / 7.0f);
  610. using (Pen pen = new Pen(color, 2))
  611. {
  612. g.SmoothingMode = SmoothingMode.AntiAlias;
  613. g.CompositingQuality = CompositingQuality.HighQuality;
  614. g.DrawLines(pen, points);
  615. }
  616. }
  617. #endregion
  618. #region GetOppositeColor
  619. /// <summary>
  620. /// 获取指定颜色的相反颜色
  621. /// </summary>
  622. /// <param name="sourceColor">Color of the source.</param>
  623. /// <returns></returns>
  624. public static Color GetOppositeColor(Color sourceColor)
  625. {
  626. return Color.FromArgb(255 - sourceColor.A, 255 - sourceColor.R, 255 - sourceColor.G, 255 - sourceColor.B);
  627. }
  628. #endregion
  629. #region
  630. #endregion
  631. }
  632. }