WpfUtils.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using System.Windows.Shapes;
  10. namespace FurnaceUI.Controls
  11. {
  12. /// <summary>
  13. /// wpf专用的辅助工具类
  14. /// </summary>
  15. public class WpfUtils
  16. {
  17. /// <summary>
  18. /// 将winform的color转换为wpf的颜色信息
  19. /// </summary>
  20. /// <param name="color">winform的颜色信息</param>
  21. /// <returns>wpf的颜色</returns>
  22. public static Color TransMediaColor(System.Drawing.Color color)
  23. {
  24. return Color.FromArgb(color.A, color.R, color.G, color.B);
  25. }
  26. /// <summary>
  27. /// 将点进行转换
  28. /// </summary>
  29. /// <param name="points">点位</param>
  30. /// <returns>点位</returns>
  31. public static System.Drawing.PointF[] TransMediaPoints(Point[] points)
  32. {
  33. System.Drawing.PointF[] points1 = new System.Drawing.PointF[points.Length];
  34. for (int i = 0; i < points.Length; i++)
  35. {
  36. points1[i] = new System.Drawing.PointF((float)points[i].X, (float)points[i].Y);
  37. }
  38. return points1;
  39. }
  40. /// <summary>
  41. /// 将点进行转换
  42. /// </summary>
  43. /// <param name="points">点位</param>
  44. /// <returns>点位</returns>
  45. public static Point[] TransMediaPoints(System.Drawing.Point[] points)
  46. {
  47. Point[] points1 = new Point[points.Length];
  48. for (int i = 0; i < points.Length; i++)
  49. {
  50. points1[i] = new Point((float)points[i].X, (float)points[i].Y);
  51. }
  52. return points1;
  53. }
  54. /// <summary>
  55. /// 将点进行转换
  56. /// </summary>
  57. /// <param name="points">点位</param>
  58. /// <returns>点位</returns>
  59. public static System.Drawing.PointF[] TransMediaPoints(List<Point> points)
  60. {
  61. return TransMediaPoints(points.ToArray());
  62. }
  63. public static System.Drawing.Color TransDrawingColor(Color color)
  64. {
  65. return System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
  66. }
  67. public static Line DrawLine(Canvas canvas, Point point1, Point point2, Color color, double thickness)
  68. {
  69. return DrawLine(canvas, point1.X, point1.Y, point2.X, point2.Y, color, thickness);
  70. }
  71. public static Line DrawLine(Canvas canvas, Point point1, Point point2, Color color, double thickness, double[] dashArray)
  72. {
  73. return DrawLine(canvas, point1.X, point1.Y, point2.X, point2.Y, color, thickness, dashArray);
  74. }
  75. public static Line DrawLine(Canvas canvas, double x1, double y1, double x2, double y2, Color color, double thickness)
  76. {
  77. return DrawLine(canvas, x1, y1, x2, y2, new SolidColorBrush(color), thickness);
  78. }
  79. public static Line DrawLine(Canvas canvas, double x1, double y1, double x2, double y2, Brush brush, double thickness)
  80. {
  81. Line line = new Line();
  82. line.X1 = x1;
  83. line.Y1 = y1;
  84. line.X2 = x2;
  85. line.Y2 = y2;
  86. line.StrokeThickness = thickness;
  87. line.Stroke = brush;
  88. canvas.Children.Add(line);
  89. return line;
  90. }
  91. public static Line DrawLine(Canvas canvas, double x1, double y1, double x2, double y2, Color color, double thickness, double[] dashArray)
  92. {
  93. Line line = new Line();
  94. line.X1 = x1;
  95. line.Y1 = y1;
  96. line.X2 = x2;
  97. line.Y2 = y2;
  98. line.StrokeDashArray = new DoubleCollection(dashArray);
  99. line.StrokeThickness = thickness;
  100. line.Stroke = new SolidColorBrush(color);
  101. canvas.Children.Add(line);
  102. return line;
  103. }
  104. public static Ellipse DrawEllipse(Canvas canvas, double x, double y, double width, double height, Color color, double thickness)
  105. {
  106. Ellipse ellipse = new Ellipse();
  107. ellipse.Width = width;
  108. ellipse.Height = height;
  109. canvas.Children.Add(ellipse);
  110. Canvas.SetLeft(ellipse, x);
  111. Canvas.SetTop(ellipse, y);
  112. ellipse.StrokeThickness = thickness;
  113. ellipse.Stroke = new SolidColorBrush(color);
  114. return ellipse;
  115. }
  116. public static Ellipse DrawEllipse(Canvas canvas, System.Drawing.RectangleF rectangle, Color color, double thickness)
  117. {
  118. return DrawEllipse(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color, thickness);
  119. }
  120. #region Fill Rectangle
  121. public static Rectangle FillRectangle(Canvas canvas, double x, double y, double width, double height, Color color)
  122. {
  123. return FillRectangle(canvas, x, y, width, height, new SolidColorBrush(color));
  124. }
  125. public static Rectangle FillRectangle(Canvas canvas, double x, double y, double width, double height, Brush brush)
  126. {
  127. if (width >= 0 && height >= 0)
  128. {
  129. Rectangle ellipse = new Rectangle();
  130. ellipse.Width = width;
  131. ellipse.Height = height;
  132. canvas.Children.Add(ellipse);
  133. Canvas.SetLeft(ellipse, x);
  134. Canvas.SetTop(ellipse, y);
  135. ellipse.Fill = brush;
  136. return ellipse;
  137. }
  138. return null;
  139. }
  140. public static Rectangle FillRectangle(Canvas canvas, System.Drawing.RectangleF rectangle, Color color)
  141. {
  142. return FillRectangle(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color);
  143. }
  144. public static Rectangle FillRectangle(Canvas canvas, System.Drawing.RectangleF rectangle, Brush brush)
  145. {
  146. return FillRectangle(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, brush);
  147. }
  148. public static Rectangle DrawRectangle(Canvas canvas, double x, double y, double width, double height, Color color, float penWidth)
  149. {
  150. if (width >= 0 && height >= 0)
  151. {
  152. Rectangle rectangle = new Rectangle();
  153. rectangle.Width = width;
  154. rectangle.Height = height;
  155. rectangle.StrokeThickness = penWidth;
  156. rectangle.Stroke = new SolidColorBrush(color);
  157. canvas.Children.Add(rectangle);
  158. Canvas.SetLeft(rectangle, x);
  159. Canvas.SetTop(rectangle, y);
  160. return rectangle;
  161. }
  162. return null;
  163. }
  164. public static Rectangle DrawRectangle(Canvas canvas, System.Drawing.RectangleF rectangle, Color color, float penWidth)
  165. {
  166. return DrawRectangle(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color, penWidth);
  167. }
  168. #endregion
  169. #region Fill Ellipse
  170. public static Ellipse FillEllipse(Canvas canvas, double x, double y, double width, double height, Color color)
  171. {
  172. return FillEllipse(canvas, x, y, width, height, new SolidColorBrush(color));
  173. }
  174. public static Ellipse FillEllipse(Canvas canvas, double x, double y, double width, double height, Brush brush)
  175. {
  176. Ellipse ellipse = new Ellipse();
  177. ellipse.Width = width;
  178. ellipse.Height = height;
  179. canvas.Children.Add(ellipse);
  180. Canvas.SetLeft(ellipse, x);
  181. Canvas.SetTop(ellipse, y);
  182. ellipse.Fill = brush;
  183. return ellipse;
  184. }
  185. public static Ellipse FillEllipse(Canvas canvas, System.Drawing.RectangleF rectangle, Color color)
  186. {
  187. return FillEllipse(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color);
  188. }
  189. public static Ellipse FillEllipse(Canvas canvas, System.Drawing.RectangleF rectangle, Brush brush)
  190. {
  191. return FillEllipse(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, brush);
  192. }
  193. #endregion
  194. public static Polyline DrawLines(Canvas canvas, Point[] points, Color color, double thickness, double[] dashArray = null)
  195. {
  196. Polyline polylines = new Polyline();
  197. polylines.Points = new PointCollection(points);
  198. polylines.StrokeThickness = thickness;
  199. polylines.Stroke = new SolidColorBrush(color);
  200. if (dashArray != null)
  201. polylines.StrokeDashArray = new DoubleCollection(dashArray);
  202. canvas.Children.Add(polylines);
  203. return polylines;
  204. }
  205. public static void DrawLines(Canvas canvas, Point[] points, HslCurveItem line, int pointsRadius)
  206. {
  207. if (points.Length < 2) return;
  208. if (line.Style == CurveStyle.LineSegment || line.Style == CurveStyle.Curve)
  209. {
  210. DrawLines(canvas, points, WpfUtils.TransMediaColor(line.LineColor), line.LineThickness);
  211. }
  212. else if (line.Style == CurveStyle.LineDot || line.Style == CurveStyle.CurveDot)
  213. {
  214. DrawLines(canvas, points, WpfUtils.TransMediaColor(line.LineColor), line.LineThickness, new double[] { 1, 1 });
  215. }
  216. else if (line.Style == CurveStyle.CurveDash || line.Style == CurveStyle.LineDash)
  217. {
  218. DrawLines(canvas, points, WpfUtils.TransMediaColor(line.LineColor), line.LineThickness, new double[] { 2, 2 });
  219. }
  220. else if (line.Style == CurveStyle.CurveLongDath || line.Style == CurveStyle.LineLongDath)
  221. {
  222. DrawLines(canvas, points, WpfUtils.TransMediaColor(line.LineColor), line.LineThickness, new double[] { 5, 5 });
  223. }
  224. else if (line.Style == CurveStyle.StepLine)
  225. {
  226. PointCollection collects = new PointCollection();
  227. for (int i = 0; i < points.Length - 1; i++)
  228. {
  229. Point center = new Point(points[i + 1].X, points[i].Y);
  230. collects.Add(points[i]);
  231. collects.Add(center);
  232. }
  233. collects.Add(points[points.Length - 1]);
  234. DrawLines(canvas, collects.ToArray(), WpfUtils.TransMediaColor(line.LineColor), line.LineThickness);
  235. }
  236. else
  237. {
  238. for (int i = 0; i < points.Length - 1; i++)
  239. {
  240. Point center = new Point(points[i + 1].X, points[i].Y);
  241. Line line1 = new Line();
  242. line1.X1 = points[i].X;
  243. line1.Y1 = points[i].Y;
  244. line1.X2 = center.X;
  245. line1.Y2 = center.Y;
  246. line1.StrokeThickness = line.LineThickness;
  247. line1.Stroke = new SolidColorBrush(WpfUtils.TransMediaColor(line.LineColor));
  248. canvas.Children.Add(line1);
  249. }
  250. }
  251. if (pointsRadius > 0 && pointsRadius < 20)
  252. {
  253. for (int i = 0; i < points.Length; i++)
  254. {
  255. FillEllipse(canvas, points[i].X - pointsRadius, points[i].Y - pointsRadius, pointsRadius * 2, pointsRadius * 2, WpfUtils.TransMediaColor(line.LineColor));
  256. }
  257. }
  258. }
  259. public static Path DrawCurve(Canvas canvas, Point[] points, Color color, double thickness)
  260. {
  261. Path path = new Path();
  262. path.StrokeThickness = thickness;
  263. path.Stroke = new SolidColorBrush(color);
  264. canvas.Children.Add(path);
  265. return path;
  266. }
  267. public static Polygon FillPolygon(Canvas canvas, Point[] points, Color color)
  268. {
  269. return FillPolygon(canvas, points, new SolidColorBrush(color));
  270. }
  271. public static Polygon FillPolygon(Canvas canvas, Point[] points, Brush brush)
  272. {
  273. Polygon polygon = new Polygon();
  274. polygon.Points = new PointCollection(points);
  275. polygon.Fill = brush;
  276. canvas.Children.Add(polygon);
  277. return polygon;
  278. }
  279. public static Polygon DrawPolygon(Canvas canvas, Point[] points, Color color, float penWidth)
  280. {
  281. Polygon polygon = new Polygon();
  282. polygon.Points = new PointCollection(points);
  283. polygon.StrokeThickness = penWidth;
  284. polygon.Stroke = new SolidColorBrush(color);
  285. canvas.Children.Add(polygon);
  286. return polygon;
  287. }
  288. public static Polygon FillTriangle(Canvas canvas, Point point, Color color, int size, GraphDirection direction)
  289. {
  290. Point[] points = new Point[4];
  291. if (direction == GraphDirection.Leftward)
  292. {
  293. points[0] = new Point(point.X, point.Y - size);
  294. points[1] = new Point(point.X, point.Y + size);
  295. points[2] = new Point(point.X - 2 * size, point.Y);
  296. }
  297. else if (direction == GraphDirection.Rightward)
  298. {
  299. points[0] = new Point(point.X, point.Y - size);
  300. points[1] = new Point(point.X, point.Y + size);
  301. points[2] = new Point(point.X + 2 * size, point.Y);
  302. }
  303. else if (direction == GraphDirection.Upward)
  304. {
  305. points[0] = new Point(point.X - size, point.Y);
  306. points[1] = new Point(point.X + size, point.Y);
  307. points[2] = new Point(point.X, point.Y - 2 * size);
  308. }
  309. else
  310. {
  311. points[0] = new Point(point.X - size, point.Y);
  312. points[1] = new Point(point.X + size, point.Y);
  313. points[2] = new Point(point.X, point.Y + 2 * size);
  314. }
  315. points[3] = points[0];
  316. return FillPolygon(canvas, points, color);
  317. }
  318. public static void DrawString(Canvas canvas, string text, Color color, System.Drawing.RectangleF rectangle, HorizontalAlignment horizontal, VerticalAlignment vertical)
  319. {
  320. DrawString(canvas, text, color, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, horizontal, vertical);
  321. }
  322. public static void DrawString(Canvas canvas, string text, Brush brush, System.Drawing.RectangleF rectangle, HorizontalAlignment horizontal, VerticalAlignment vertical)
  323. {
  324. DrawString(canvas, text, brush, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, horizontal, vertical);
  325. }
  326. public static void DrawString(Canvas canvas, string text, Color color, double x, double y, double width, double height, HorizontalAlignment horizontal, VerticalAlignment vertical)
  327. {
  328. DrawString(canvas, text, new SolidColorBrush(color), x, y, width, height, horizontal, vertical);
  329. }
  330. public static void DrawString(Canvas canvas, string text, Brush foreground, double x, double y, double width, double height, HorizontalAlignment horizontal, VerticalAlignment vertical)
  331. {
  332. Label label = new Label();
  333. label.Width = width;
  334. label.Height = height;
  335. label.Foreground = foreground;
  336. label.HorizontalContentAlignment = horizontal;
  337. label.VerticalContentAlignment = vertical;
  338. label.Content = text;
  339. canvas.Children.Add(label);
  340. label.Style = null;
  341. Canvas.SetLeft(label, x);
  342. Canvas.SetTop(label, y);
  343. }
  344. public static void DrawString(Canvas canvas, string text, Brush foreground, double fontSize, double x, double y, double width, double height, HorizontalAlignment horizontal, VerticalAlignment vertical)
  345. {
  346. Label label = new Label();
  347. label.Width = width;
  348. label.Height = height;
  349. label.FontSize = fontSize;
  350. label.Foreground = foreground;
  351. label.HorizontalContentAlignment = horizontal;
  352. label.VerticalContentAlignment = vertical;
  353. label.Content = text;
  354. canvas.Children.Add(label);
  355. Canvas.SetLeft(label, x);
  356. Canvas.SetTop(label, y);
  357. }
  358. }
  359. }