123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Shapes;
- namespace FurnaceUI.Controls
- {
- /// <summary>
- /// wpf专用的辅助工具类
- /// </summary>
- public class WpfUtils
- {
- /// <summary>
- /// 将winform的color转换为wpf的颜色信息
- /// </summary>
- /// <param name="color">winform的颜色信息</param>
- /// <returns>wpf的颜色</returns>
- public static Color TransMediaColor(System.Drawing.Color color)
- {
- return Color.FromArgb(color.A, color.R, color.G, color.B);
- }
- /// <summary>
- /// 将点进行转换
- /// </summary>
- /// <param name="points">点位</param>
- /// <returns>点位</returns>
- public static System.Drawing.PointF[] TransMediaPoints(Point[] points)
- {
- System.Drawing.PointF[] points1 = new System.Drawing.PointF[points.Length];
- for (int i = 0; i < points.Length; i++)
- {
- points1[i] = new System.Drawing.PointF((float)points[i].X, (float)points[i].Y);
- }
- return points1;
- }
- /// <summary>
- /// 将点进行转换
- /// </summary>
- /// <param name="points">点位</param>
- /// <returns>点位</returns>
- public static Point[] TransMediaPoints(System.Drawing.Point[] points)
- {
- Point[] points1 = new Point[points.Length];
- for (int i = 0; i < points.Length; i++)
- {
- points1[i] = new Point((float)points[i].X, (float)points[i].Y);
- }
- return points1;
- }
- /// <summary>
- /// 将点进行转换
- /// </summary>
- /// <param name="points">点位</param>
- /// <returns>点位</returns>
- public static System.Drawing.PointF[] TransMediaPoints(List<Point> points)
- {
- return TransMediaPoints(points.ToArray());
- }
- public static System.Drawing.Color TransDrawingColor(Color color)
- {
- return System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
- }
- public static Line DrawLine(Canvas canvas, Point point1, Point point2, Color color, double thickness)
- {
- return DrawLine(canvas, point1.X, point1.Y, point2.X, point2.Y, color, thickness);
- }
- public static Line DrawLine(Canvas canvas, Point point1, Point point2, Color color, double thickness, double[] dashArray)
- {
- return DrawLine(canvas, point1.X, point1.Y, point2.X, point2.Y, color, thickness, dashArray);
- }
- public static Line DrawLine(Canvas canvas, double x1, double y1, double x2, double y2, Color color, double thickness)
- {
- return DrawLine(canvas, x1, y1, x2, y2, new SolidColorBrush(color), thickness);
- }
- public static Line DrawLine(Canvas canvas, double x1, double y1, double x2, double y2, Brush brush, double thickness)
- {
- Line line = new Line();
- line.X1 = x1;
- line.Y1 = y1;
- line.X2 = x2;
- line.Y2 = y2;
- line.StrokeThickness = thickness;
- line.Stroke = brush;
- canvas.Children.Add(line);
- return line;
- }
- public static Line DrawLine(Canvas canvas, double x1, double y1, double x2, double y2, Color color, double thickness, double[] dashArray)
- {
- Line line = new Line();
- line.X1 = x1;
- line.Y1 = y1;
- line.X2 = x2;
- line.Y2 = y2;
- line.StrokeDashArray = new DoubleCollection(dashArray);
- line.StrokeThickness = thickness;
- line.Stroke = new SolidColorBrush(color);
- canvas.Children.Add(line);
- return line;
- }
- public static Ellipse DrawEllipse(Canvas canvas, double x, double y, double width, double height, Color color, double thickness)
- {
- Ellipse ellipse = new Ellipse();
- ellipse.Width = width;
- ellipse.Height = height;
- canvas.Children.Add(ellipse);
- Canvas.SetLeft(ellipse, x);
- Canvas.SetTop(ellipse, y);
- ellipse.StrokeThickness = thickness;
- ellipse.Stroke = new SolidColorBrush(color);
- return ellipse;
- }
- public static Ellipse DrawEllipse(Canvas canvas, System.Drawing.RectangleF rectangle, Color color, double thickness)
- {
- return DrawEllipse(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color, thickness);
- }
- #region Fill Rectangle
- public static Rectangle FillRectangle(Canvas canvas, double x, double y, double width, double height, Color color)
- {
- return FillRectangle(canvas, x, y, width, height, new SolidColorBrush(color));
- }
- public static Rectangle FillRectangle(Canvas canvas, double x, double y, double width, double height, Brush brush)
- {
- if (width >= 0 && height >= 0)
- {
- Rectangle ellipse = new Rectangle();
- ellipse.Width = width;
- ellipse.Height = height;
- canvas.Children.Add(ellipse);
- Canvas.SetLeft(ellipse, x);
- Canvas.SetTop(ellipse, y);
- ellipse.Fill = brush;
- return ellipse;
- }
- return null;
- }
- public static Rectangle FillRectangle(Canvas canvas, System.Drawing.RectangleF rectangle, Color color)
- {
- return FillRectangle(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color);
- }
- public static Rectangle FillRectangle(Canvas canvas, System.Drawing.RectangleF rectangle, Brush brush)
- {
- return FillRectangle(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, brush);
- }
- public static Rectangle DrawRectangle(Canvas canvas, double x, double y, double width, double height, Color color, float penWidth)
- {
- if (width >= 0 && height >= 0)
- {
- Rectangle rectangle = new Rectangle();
- rectangle.Width = width;
- rectangle.Height = height;
- rectangle.StrokeThickness = penWidth;
- rectangle.Stroke = new SolidColorBrush(color);
- canvas.Children.Add(rectangle);
- Canvas.SetLeft(rectangle, x);
- Canvas.SetTop(rectangle, y);
- return rectangle;
- }
- return null;
- }
- public static Rectangle DrawRectangle(Canvas canvas, System.Drawing.RectangleF rectangle, Color color, float penWidth)
- {
- return DrawRectangle(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color, penWidth);
- }
- #endregion
- #region Fill Ellipse
- public static Ellipse FillEllipse(Canvas canvas, double x, double y, double width, double height, Color color)
- {
- return FillEllipse(canvas, x, y, width, height, new SolidColorBrush(color));
- }
- public static Ellipse FillEllipse(Canvas canvas, double x, double y, double width, double height, Brush brush)
- {
- Ellipse ellipse = new Ellipse();
- ellipse.Width = width;
- ellipse.Height = height;
- canvas.Children.Add(ellipse);
- Canvas.SetLeft(ellipse, x);
- Canvas.SetTop(ellipse, y);
- ellipse.Fill = brush;
- return ellipse;
- }
- public static Ellipse FillEllipse(Canvas canvas, System.Drawing.RectangleF rectangle, Color color)
- {
- return FillEllipse(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color);
- }
- public static Ellipse FillEllipse(Canvas canvas, System.Drawing.RectangleF rectangle, Brush brush)
- {
- return FillEllipse(canvas, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, brush);
- }
- #endregion
- public static Polyline DrawLines(Canvas canvas, Point[] points, Color color, double thickness, double[] dashArray = null)
- {
- Polyline polylines = new Polyline();
- polylines.Points = new PointCollection(points);
- polylines.StrokeThickness = thickness;
- polylines.Stroke = new SolidColorBrush(color);
- if (dashArray != null)
- polylines.StrokeDashArray = new DoubleCollection(dashArray);
- canvas.Children.Add(polylines);
- return polylines;
- }
- public static void DrawLines(Canvas canvas, Point[] points, HslCurveItem line, int pointsRadius)
- {
- if (points.Length < 2) return;
- if (line.Style == CurveStyle.LineSegment || line.Style == CurveStyle.Curve)
- {
- DrawLines(canvas, points, WpfUtils.TransMediaColor(line.LineColor), line.LineThickness);
- }
- else if (line.Style == CurveStyle.LineDot || line.Style == CurveStyle.CurveDot)
- {
- DrawLines(canvas, points, WpfUtils.TransMediaColor(line.LineColor), line.LineThickness, new double[] { 1, 1 });
- }
- else if (line.Style == CurveStyle.CurveDash || line.Style == CurveStyle.LineDash)
- {
- DrawLines(canvas, points, WpfUtils.TransMediaColor(line.LineColor), line.LineThickness, new double[] { 2, 2 });
- }
- else if (line.Style == CurveStyle.CurveLongDath || line.Style == CurveStyle.LineLongDath)
- {
- DrawLines(canvas, points, WpfUtils.TransMediaColor(line.LineColor), line.LineThickness, new double[] { 5, 5 });
- }
- else if (line.Style == CurveStyle.StepLine)
- {
- PointCollection collects = new PointCollection();
- for (int i = 0; i < points.Length - 1; i++)
- {
- Point center = new Point(points[i + 1].X, points[i].Y);
- collects.Add(points[i]);
- collects.Add(center);
- }
- collects.Add(points[points.Length - 1]);
- DrawLines(canvas, collects.ToArray(), WpfUtils.TransMediaColor(line.LineColor), line.LineThickness);
- }
- else
- {
- for (int i = 0; i < points.Length - 1; i++)
- {
- Point center = new Point(points[i + 1].X, points[i].Y);
- Line line1 = new Line();
- line1.X1 = points[i].X;
- line1.Y1 = points[i].Y;
- line1.X2 = center.X;
- line1.Y2 = center.Y;
- line1.StrokeThickness = line.LineThickness;
- line1.Stroke = new SolidColorBrush(WpfUtils.TransMediaColor(line.LineColor));
- canvas.Children.Add(line1);
- }
- }
- if (pointsRadius > 0 && pointsRadius < 20)
- {
- for (int i = 0; i < points.Length; i++)
- {
- FillEllipse(canvas, points[i].X - pointsRadius, points[i].Y - pointsRadius, pointsRadius * 2, pointsRadius * 2, WpfUtils.TransMediaColor(line.LineColor));
- }
- }
- }
- public static Path DrawCurve(Canvas canvas, Point[] points, Color color, double thickness)
- {
- Path path = new Path();
- path.StrokeThickness = thickness;
- path.Stroke = new SolidColorBrush(color);
- canvas.Children.Add(path);
- return path;
- }
- public static Polygon FillPolygon(Canvas canvas, Point[] points, Color color)
- {
- return FillPolygon(canvas, points, new SolidColorBrush(color));
- }
- public static Polygon FillPolygon(Canvas canvas, Point[] points, Brush brush)
- {
- Polygon polygon = new Polygon();
- polygon.Points = new PointCollection(points);
- polygon.Fill = brush;
- canvas.Children.Add(polygon);
- return polygon;
- }
- public static Polygon DrawPolygon(Canvas canvas, Point[] points, Color color, float penWidth)
- {
- Polygon polygon = new Polygon();
- polygon.Points = new PointCollection(points);
- polygon.StrokeThickness = penWidth;
- polygon.Stroke = new SolidColorBrush(color);
- canvas.Children.Add(polygon);
- return polygon;
- }
- public static Polygon FillTriangle(Canvas canvas, Point point, Color color, int size, GraphDirection direction)
- {
- Point[] points = new Point[4];
- if (direction == GraphDirection.Leftward)
- {
- points[0] = new Point(point.X, point.Y - size);
- points[1] = new Point(point.X, point.Y + size);
- points[2] = new Point(point.X - 2 * size, point.Y);
- }
- else if (direction == GraphDirection.Rightward)
- {
- points[0] = new Point(point.X, point.Y - size);
- points[1] = new Point(point.X, point.Y + size);
- points[2] = new Point(point.X + 2 * size, point.Y);
- }
- else if (direction == GraphDirection.Upward)
- {
- points[0] = new Point(point.X - size, point.Y);
- points[1] = new Point(point.X + size, point.Y);
- points[2] = new Point(point.X, point.Y - 2 * size);
- }
- else
- {
- points[0] = new Point(point.X - size, point.Y);
- points[1] = new Point(point.X + size, point.Y);
- points[2] = new Point(point.X, point.Y + 2 * size);
- }
- points[3] = points[0];
- return FillPolygon(canvas, points, color);
- }
- public static void DrawString(Canvas canvas, string text, Color color, System.Drawing.RectangleF rectangle, HorizontalAlignment horizontal, VerticalAlignment vertical)
- {
- DrawString(canvas, text, color, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, horizontal, vertical);
- }
- public static void DrawString(Canvas canvas, string text, Brush brush, System.Drawing.RectangleF rectangle, HorizontalAlignment horizontal, VerticalAlignment vertical)
- {
- DrawString(canvas, text, brush, rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, horizontal, vertical);
- }
- public static void DrawString(Canvas canvas, string text, Color color, double x, double y, double width, double height, HorizontalAlignment horizontal, VerticalAlignment vertical)
- {
- DrawString(canvas, text, new SolidColorBrush(color), x, y, width, height, horizontal, vertical);
- }
- public static void DrawString(Canvas canvas, string text, Brush foreground, double x, double y, double width, double height, HorizontalAlignment horizontal, VerticalAlignment vertical)
- {
- Label label = new Label();
- label.Width = width;
- label.Height = height;
- label.Foreground = foreground;
- label.HorizontalContentAlignment = horizontal;
- label.VerticalContentAlignment = vertical;
- label.Content = text;
- canvas.Children.Add(label);
- label.Style = null;
- Canvas.SetLeft(label, x);
- Canvas.SetTop(label, y);
- }
- public static void DrawString(Canvas canvas, string text, Brush foreground, double fontSize, double x, double y, double width, double height, HorizontalAlignment horizontal, VerticalAlignment vertical)
- {
- Label label = new Label();
- label.Width = width;
- label.Height = height;
- label.FontSize = fontSize;
- label.Foreground = foreground;
- label.HorizontalContentAlignment = horizontal;
- label.VerticalContentAlignment = vertical;
- label.Content = text;
- canvas.Children.Add(label);
- Canvas.SetLeft(label, x);
- Canvas.SetTop(label, y);
- }
- }
- }
|