wpf 画图 代码实现

阅读: 评论:0

wpf 画图 代码实现

wpf 画图 代码实现

实现效果

 

Helper:

internal class DrawHelper{/// <summary>/// pixelLength,_ruler, _StartPoint, _EndPoint/// </summary>public Action<double, double, Point, Point>? MoveEvent { get; set; }Panel? _canvas;public DrawHelper(Panel canvas){_canvas = canvas;_points = new List<Point>();EnableMousePath(true);EnableLine(true);}private List<Point> _points;private bool _isEnable = false;private System.Windows.Point _StartPoint = new Point();private System.Windows.Point _EndPoint = new Point();private double _width = 0;private double _height = 0;private bool _MouseLeftButtonDownFlag = false;private double _ruler = 1;private bool _enableMousePath;private bool _enableLine;public void SetRuler(double ruler){_ruler = ruler;}public void EnableLine(bool enable){_enableLine = enable;}public void EnableMousePath(bool enable){_enableMousePath = enable;}public void EnableDraw(){if (_canvas != null && !_isEnable){_canvas.IsHitTestVisible = true;_canvas.MouseLeftButtonDown += DrawRuler_MouseLeftButtonDown;_canvas.MouseLeftButtonUp += DrawRuler_MouseLeftButtonUp;_canvas.MouseMove += DrawRuler_MouseMove;_isEnable = true;}}public void DIsableDraw(){_canvas?.Children?.Clear();if (_canvas != null && _isEnable){_canvas.IsHitTestVisible = false;_canvas.MouseLeftButtonDown -= DrawRuler_MouseLeftButtonDown;_canvas.MouseLeftButtonUp -= DrawRuler_MouseLeftButtonUp;_canvas.MouseMove -= DrawRuler_MouseMove;_isEnable = false;}}public double Ruler => _ruler;public Point StartPoint => _StartPoint;public Point EndPoint => _EndPoint;private void DrawRuler_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e){_MouseLeftButtonDownFlag = true;_canvas?.Children?.Clear();_points?.Clear();_StartPoint = e.GetPosition(_canvas);}private void DrawRuler_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e){_MouseLeftButtonDownFlag = false;_EndPoint = e.GetPosition(_canvas);_canvas.IsHitTestVisible = true;}private void DrawRuler_MouseMove(object sender, System.Windows.Input.MouseEventArgs e){if (!_MouseLeftButtonDownFlag || e.LeftButton != System.Windows.Input.MouseButtonState.Pressed){return;}_canvas?.Children?.Clear();_EndPoint = e.GetPosition(_canvas);if (_enableLine){Line();}if (_enableMousePath){MousePath();}}private void MousePath(){_points?.Add(EndPoint);List<LineSegment> lineSegments = new List<LineSegment>();foreach (var item in _points){lineSegments.Add(new LineSegment(item, true));}PathFigure pathFigure = new PathFigure(_points.FirstOrDefault(), lineSegments, false);PathGeometry pathGeometry = new PathGeometry();pathGeometry.Figures.Add(pathFigure);var path = new Path(); path.Data = pathGeometry;path.Stroke = System.Windows.Media.Brushes.Red;_canvas?.Children.Add(path);}private void Line(){System.Windows.Shapes.Path path = new System.Windows.Shapes.Path() { Stroke = System.Windows.Media.Brushes.Red };System.Windows.Shapes.Path pathC1 = new System.Windows.Shapes.Path() { Stroke = System.Windows.Media.Brushes.Red };System.Windows.Shapes.Path pathC2 = new System.Windows.Shapes.Path() { Stroke = System.Windows.Media.Brushes.Red };_width = _EndPoint.X - _StartPoint.X;_height = _EndPoint.Y - _StartPoint.Y;LineGeometry line = new LineGeometry() { StartPoint = _StartPoint, EndPoint = _EndPoint };LineGeometry lineC1 = new LineGeometry();LineGeometry lineC2 = new LineGeometry();path.Data = line;//---------------------画两头垂线//平行线斜率double k = (_EndPoint.Y - _StartPoint.Y) / (_EndPoint.X - _StartPoint.X);//垂线斜率double k2 = -(1 / k);if (k == 0){//k2 = 0.5;}//垂线常亮bdouble b = _StartPoint.Y - k2 * _StartPoint.X;double b1 = _EndPoint.Y - k2 * _EndPoint.X;double x1 = _StartPoint.X - Math.Sqrt(2000 / (k2 * k2 + 1));double x2 = _StartPoint.X + Math.Sqrt(2000 / (k2 * k2 + 1));System.Windows.Point Chui1 = new System.Windows.Point(x1, k2 * x1 + b);System.Windows.Point Chui2 = new System.Windows.Point(x2, k2 * x2 + b);lineC1.StartPoint = Chui1;lineC1.EndPoint = Chui2;pathC1.Data = lineC1;x1 = _EndPoint.X - Math.Sqrt(2000 / (k2 * k2 + 1));x2 = _EndPoint.X + Math.Sqrt(2000 / (k2 * k2 + 1));System.Windows.Point Chui3 = new System.Windows.Point(x1, k2 * x1 + b1);System.Windows.Point Chui4 = new System.Windows.Point(x2, k2 * x2 + b1);lineC2.StartPoint = Chui3;lineC2.EndPoint = Chui4;pathC2.Data = lineC2;var pixelLength = Math.Sqrt(_width * _width + _height * _height);MoveEvent?.Invoke(pixelLength, _ruler, _StartPoint, _EndPoint);Border border = new Border();try{  //画详情double block_x = x1 > x2 ? x1 : x2;double block_y = Chui3.Y > Chui4.Y ? Chui3.Y : Chui4.Y;TextBlock block = new TextBlock();block.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(App.appSettings?.SystemAccent ?? "#FF007ACC"));block.Text = (pixelLength * _ruler).ToString("f2") + "μm";block.Margin = new System.Windows.Thickness(5, 3, 5, 3);block.HorizontalAlignment = HorizontalAlignment.Center;block.FontWeight = FontWeights.Bold;border = new Border{CornerRadius = new CornerRadius(3),VerticalAlignment = VerticalAlignment.Top,HorizontalAlignment = HorizontalAlignment.Left,Background = System.Windows.Media.Brushes.White,Margin = new System.Windows.Thickness(block_x, block_y, 0, 0),Child = block};}catch (Exception){}_canvas?.Children.Add(path);_canvas?.Children.Add(pathC1);_canvas?.Children.Add(pathC2);_canvas?.Children.Add(border);path.IsHitTestVisible = false;pathC1.IsHitTestVisible = false;pathC2.IsHitTestVisible = false;border.IsHitTestVisible = false;line.Freeze();}}
}

使用:

 xaml:所有继承panl的控件都可以 canvas,grid等

 <Grid Background="Transparent"  Name="Grid_DrawRuler" Grid.Row="1" IsHitTestVisible="False"/>
_drawHelper = new DrawHelper(Grid_DrawRuler);//启用
_drawHelper?.EnableDraw();
//启用鼠标轨迹
_drawHelper?.EnableMousePath(true);
//启用直线、标尺
_drawHelper?.EnableLine(true);
//禁用_drawHelper?.DisableDraw();

本文发布于:2024-01-29 00:01:25,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170645769011257.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:画图   代码   wpf
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23