RenderEngine.4.Brush.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using Microsoft.Drawing;
  5. namespace Microsoft.Windows.Forms
  6. {
  7. public static partial class RenderEngine
  8. {
  9. /// <summary>
  10. /// 获取线段与水平X轴夹角(角度表示)
  11. /// </summary>
  12. /// <param name="pt1">起点</param>
  13. /// <param name="pt2">终点</param>
  14. /// <returns>夹角</returns>
  15. public static float GetLineDegrees(Point pt1, Point pt2)
  16. {
  17. return (float)MathEx.ToDegrees(Math.Atan(((double)pt2.Y - (double)pt1.Y) / ((double)pt2.X - (double)pt1.X)));
  18. }
  19. /// <summary>
  20. /// 获取包含线段的最小矩形(非空)
  21. /// </summary>
  22. /// <param name="pt1">起点</param>
  23. /// <param name="pt2">终点</param>
  24. /// <returns>矩形</returns>
  25. public static Rectangle GetLineRect(Point pt1, Point pt2)
  26. {
  27. Rectangle rect = Rectangle.FromLTRB(Math.Min(pt1.X, pt2.X), Math.Min(pt1.Y, pt2.Y), Math.Max(pt1.X, pt2.X), Math.Max(pt1.Y, pt2.Y));
  28. rect.Inflate(1, 1);
  29. return rect;
  30. }
  31. /// <summary>
  32. /// 创建画刷,渲染背景和边框使用
  33. /// </summary>
  34. /// <param name="rect">画刷区域</param>
  35. /// <param name="baseColor">基色</param>
  36. /// <param name="pos1">基色位置1</param>
  37. /// <param name="pos2">基色位置2</param>
  38. /// <param name="reverse">是否反转</param>
  39. /// <param name="mode">渐变模式</param>
  40. /// <param name="style">样式</param>
  41. /// <returns>画刷</returns>
  42. public static Brush CreateBrush(Rectangle rect, Color baseColor, float pos1, float pos2, bool reverse, LinearGradientMode mode, BlendStyle style)
  43. {
  44. Brush brush = null;
  45. RectangleEx.MakeNotEmpty(ref rect);
  46. switch (style)
  47. {
  48. case BlendStyle.Solid:
  49. {
  50. SolidBrush brushTmp = new SolidBrush(baseColor);
  51. brush = brushTmp;
  52. brushTmp = null;
  53. }
  54. break;
  55. case BlendStyle.Gradient:
  56. {
  57. LinearGradientBrush brushTmp = new LinearGradientBrush(rect, Color.Empty, Color.Empty, mode);
  58. //画刷设置
  59. ColorBlend blendTmp = new ColorBlend();
  60. Color[] colors;
  61. float[] positions;
  62. RenderEngine.GetColorPosGradient(baseColor, pos1, pos2, reverse, false, out colors, out positions);
  63. blendTmp.Colors = colors;
  64. blendTmp.Positions = positions;
  65. //
  66. brushTmp.InterpolationColors = blendTmp;
  67. brush = brushTmp;
  68. brushTmp = null;
  69. }
  70. break;
  71. case BlendStyle.FadeIn:
  72. {
  73. LinearGradientBrush brushTmp = new LinearGradientBrush(rect, Color.Empty, Color.Empty, mode);
  74. //画刷设置
  75. ColorBlend blendTmp = new ColorBlend();
  76. Color[] colors;
  77. float[] positions;
  78. RenderEngine.GetColorPosFadeIn(baseColor, pos1, pos2, reverse, false, out colors, out positions);
  79. blendTmp.Colors = colors;
  80. blendTmp.Positions = positions;
  81. //
  82. brushTmp.InterpolationColors = blendTmp;
  83. brush = brushTmp;
  84. brushTmp = null;
  85. }
  86. break;
  87. case BlendStyle.FadeOut:
  88. {
  89. LinearGradientBrush brushTmp = new LinearGradientBrush(rect, Color.Empty, Color.Empty, mode);
  90. //画刷设置
  91. ColorBlend blendTmp = new ColorBlend();
  92. Color[] colors;
  93. float[] positions;
  94. RenderEngine.GetColorPosFadeOut(baseColor, pos1, pos2, reverse, false, out colors, out positions);
  95. blendTmp.Colors = colors;
  96. blendTmp.Positions = positions;
  97. //
  98. brushTmp.InterpolationColors = blendTmp;
  99. brush = brushTmp;
  100. brushTmp = null;
  101. }
  102. break;
  103. case BlendStyle.FadeInFadeOut:
  104. {
  105. LinearGradientBrush brushTmp = new LinearGradientBrush(rect, Color.Empty, Color.Empty, mode);
  106. //画刷设置
  107. ColorBlend blendTmp = new ColorBlend();
  108. Color[] colors;
  109. float[] positions;
  110. RenderEngine.GetColorPosFadeInFadeOut(baseColor, pos1, pos2, reverse, false, out colors, out positions);
  111. blendTmp.Colors = colors;
  112. blendTmp.Positions = positions;
  113. //
  114. brushTmp.InterpolationColors = blendTmp;
  115. brush = brushTmp;
  116. brushTmp = null;
  117. }
  118. break;
  119. default:
  120. break;
  121. }
  122. return brush;
  123. }
  124. /// <summary>
  125. /// 创建画刷,渲染线段使用
  126. /// </summary>
  127. /// <param name="pt1">起点</param>
  128. /// <param name="pt2">终点</param>
  129. /// <param name="baseColor">基色</param>
  130. /// <param name="style">样式</param>
  131. /// <returns>画刷</returns>
  132. public static Brush CreateBrush(Point pt1, Point pt2, Color baseColor, BlendStyle style)
  133. {
  134. Brush brush = null;
  135. switch (style)
  136. {
  137. case BlendStyle.Solid:
  138. {
  139. SolidBrush brushTmp = new SolidBrush(baseColor);
  140. brush = brushTmp;
  141. brushTmp = null;
  142. }
  143. break;
  144. case BlendStyle.Gradient:
  145. {
  146. LinearGradientBrush brushTmp = new LinearGradientBrush(GetLineRect(pt1, pt2), Color.Empty, Color.Empty, GetLineDegrees(pt1, pt2));
  147. //画刷设置
  148. ColorBlend blendTmp = new ColorBlend();
  149. Color[] colors;
  150. float[] positions;
  151. RenderEngine.GetColorPosGradient(baseColor, float.NaN, float.NaN, false, true, out colors, out positions);
  152. blendTmp.Colors = colors;
  153. blendTmp.Positions = positions;
  154. //
  155. brushTmp.InterpolationColors = blendTmp;
  156. brush = brushTmp;
  157. brushTmp = null;
  158. }
  159. break;
  160. case BlendStyle.FadeIn:
  161. {
  162. LinearGradientBrush brushTmp = new LinearGradientBrush(GetLineRect(pt1, pt2), Color.Empty, Color.Empty, GetLineDegrees(pt1, pt2));
  163. //画刷设置
  164. ColorBlend blendTmp = new ColorBlend();
  165. Color[] colors;
  166. float[] positions;
  167. RenderEngine.GetColorPosFadeIn(baseColor, float.NaN, float.NaN, false, true, out colors, out positions);
  168. blendTmp.Colors = colors;
  169. blendTmp.Positions = positions;
  170. //
  171. brushTmp.InterpolationColors = blendTmp;
  172. brush = brushTmp;
  173. brushTmp = null;
  174. }
  175. break;
  176. case BlendStyle.FadeOut:
  177. {
  178. LinearGradientBrush brushTmp = new LinearGradientBrush(GetLineRect(pt1, pt2), Color.Empty, Color.Empty, GetLineDegrees(pt1, pt2));
  179. //画刷设置
  180. ColorBlend blendTmp = new ColorBlend();
  181. Color[] colors;
  182. float[] positions;
  183. RenderEngine.GetColorPosFadeOut(baseColor, float.NaN, float.NaN, false, true, out colors, out positions);
  184. blendTmp.Colors = colors;
  185. blendTmp.Positions = positions;
  186. //
  187. brushTmp.InterpolationColors = blendTmp;
  188. brush = brushTmp;
  189. brushTmp = null;
  190. }
  191. break;
  192. case BlendStyle.FadeInFadeOut:
  193. {
  194. LinearGradientBrush brushTmp = new LinearGradientBrush(GetLineRect(pt1, pt2), Color.Empty, Color.Empty, GetLineDegrees(pt1, pt2));
  195. //画刷设置
  196. ColorBlend blendTmp = new ColorBlend();
  197. Color[] colors;
  198. float[] positions;
  199. RenderEngine.GetColorPosFadeInFadeOut(baseColor, float.NaN, float.NaN, false, true, out colors, out positions);
  200. blendTmp.Colors = colors;
  201. blendTmp.Positions = positions;
  202. //
  203. brushTmp.InterpolationColors = blendTmp;
  204. brush = brushTmp;
  205. brushTmp = null;
  206. }
  207. break;
  208. default:
  209. break;
  210. }
  211. return brush;
  212. }
  213. /// <summary>
  214. /// 创建画笔
  215. /// </summary>
  216. /// <param name="brush">画刷</param>
  217. /// <param name="width">直线宽度</param>
  218. /// <param name="style">虚线样式</param>
  219. /// <param name="pattern">点线长度数组</param>
  220. /// <param name="cap">每个线段键帽样式</param>
  221. /// <param name="offset">直线的起点到短划线图案起始处的距离</param>
  222. /// <returns>画笔</returns>
  223. public static Pen CreatePen(Brush brush, int width, DashStyle style, float[] pattern, DashCap cap, float offset)
  224. {
  225. Pen pen = new Pen(brush);
  226. pen.Width = width;
  227. pen.DashStyle = style;
  228. if (style == DashStyle.Custom && pattern != null && pattern.Length > 0)
  229. pen.DashPattern = pattern;
  230. pen.DashCap = cap;
  231. pen.DashOffset = offset;
  232. return pen;
  233. }
  234. }
  235. }