none
自定了一个图,如何封装成一个画图类? RRS feed

  • 问题

  •   <Grid Background="White" x:Name="Hello">
            <Grid.RowDefinitions>
                <RowDefinition Height="30"/>
                <RowDefinition />
            </Grid.RowDefinitions>
            <Button Grid.Row="0" Click="Button_Click"/>
            <Border BorderBrush="Red" BorderThickness="5"
                    Grid.Row="1"
                    Height="500"
                    Width="500"
                    x:Name="my">
                <Canvas x:Name="Canvastest"
                        MouseLeftButtonDown="MouseLeftButtonDown"
                        MouseLeftButtonUp="MouseLeftButtonUp"
                        MouseMove="MouseMove"
                        MouseRightButtonDown="MouseRightButtonDown"
                        Grid.Row="1"
                        Height="500"
                        Width="500">
                    <Canvas.Background>
                        <SolidColorBrush Color="White"
                                         Opacity="0.1" />
                    </Canvas.Background>
                </Canvas>
            </Border>
    
        </Grid>

      public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                Canvastest.Cursor = Cursors.Cross;
                //Canvastest.Cursor = Cursors.Hand;
                //Cursors
                //History(164, 119, 157, 166);
            }
    
            private bool isMouseDown = false;
            Point startPoint = new Point();
            double xx1 = 0;
            double yy1 = 0;
            double xx2 = 0;
            double yy2 = 0;
            public Line Lines;
            public int index;
    
            public double PointOneX { get; set; }
            public double PointOneY { get; set; }
            public double PointTwoX { get; set; }
            public double PointTwoY { get; set; }
    
            public double TearHight { get; set; }
    
    
    
            public Point StartPoint { get; set; }
    
            public Point EndPoint { get; set; }
    
            private void MouseLeftButtonDown(object sender, MouseEventArgs e)
            {
                Lines = new Line();
                isMouseDown = true;
                startPoint = e.GetPosition(this.Canvastest);
                Point pp = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法
                Lines.StrokeThickness = 3;
                Lines.X1 = pp.X;
                Lines.Y1 = pp.Y;
                this.Canvastest.Children.Add(Lines);
            }
    
            private void MouseLeftButtonUp(object sender, MouseEventArgs e)
            {
                isMouseDown = false;
                CommandDrawLine();
                Debug.WriteLine("PointOneX:" + PointOneX);
                Debug.WriteLine("PointOneY:" + PointOneY);
                Debug.WriteLine("PointTwoX:" + PointTwoX);
                Debug.WriteLine("PointTwoY:" + PointTwoY);
            }
    
            public void calcpoint(double X1, double Y1, double k1, out double xx1, out double yy1, out double xx2, out double yy2)
            {
                double k = -1 / k1;
                double b = Y1 - k * X1;
                double B = -2 * Y1 * k + 2 * k * b - 2 * X1;
                double C = Math.Pow(X1, 2) + Math.Pow(Y1, 2) - 2 * Y1 * b + Math.Pow(b, 2) - Math.Pow(50, 2);
                double A = (1 + Math.Pow(k, 2));
                double num1 = Math.Sqrt(Math.Pow(B, 2) - 4 * A * C);
                xx1 = (-1 * B + num1) / (2 * A);
                yy1 = k * xx1 + b;
                xx2 = (-1 * B - num1) / (2 * A);
                yy2 = k * xx2 + b;
            }
    
            private void MouseMove(object sender, MouseEventArgs e)
            {
                if (isMouseDown)
                {
                    Point pp = Mouse.GetPosition(e.Source as FrameworkElement);//WPF方法
                    Lines.X2 = pp.X;
                    Lines.Y2 = pp.Y;
                    Lines.Stroke = Brushes.Green;
                }
            }
    
            public void History(double pointonex,double pointoney,double pointtwox ,double pointtwoy)
            {
                Lines = new Line();
                Lines.X1 = pointonex;
                Lines.Y1 = pointoney;
                Lines.X2 = pointtwox;
                Lines.Y2= pointtwoy;
                Lines.Stroke = Brushes.Green;
                this.Canvastest.Children.Add(Lines);
                CommandDrawLine();
            }
    
            public void History(Point startpoint,Point endpoint)
            {
                Lines = new Line();
                Lines.X1 = startpoint.X;
                Lines.Y1 = startpoint.Y;
                Lines.X2 = endpoint.X;
                Lines.Y2 = endpoint.Y;
                Lines.Stroke = Brushes.Green;
                this.Canvastest.Children.Add(Lines);
                CommandDrawLine();
            }
    
            public void CommandDrawLine()
            {
                PointOneX = Lines.X1;
                PointOneY = Lines.Y1;
                PointTwoX = Lines.X2;
                PointTwoY = Lines.Y2;
    
               var k1 = (Lines.Y2 - Lines.Y1) / (Lines.X2 - Lines.X1);
    
                Line lineC = new Line();
    
                //lineC.X1 = (Lines.X1-(Lines.Y1-Lines.Y2));
                //lineC.Y1 = (Lines.Y1+(Lines.X1-Lines.X2));
                //lineC.X2 = (Lines.X1+(Lines.Y1-Lines.Y2));
                //lineC.Y2 = (Lines.Y1-(Lines.X1-Lines.X2));
    
                calcpoint(Lines.X1, Lines.Y1, k1, out xx1, out yy1, out xx2, out yy2);
    
                lineC.X1 = xx1;
                lineC.Y1 = yy1;
                lineC.X2 = xx2;
                lineC.Y2 = yy2;
                lineC.Stroke = Brushes.Blue;
                lineC.StrokeThickness = 3;
                this.Canvastest.Children.Add(lineC);
    
                Line lineD = new Line();
    
                //lineD.X1 = (Lines.X2-(Lines.Y1-Lines.Y2));
                //lineD.Y1 = (Lines.Y2+(Lines.X1-Lines.X2));
                //lineD.X2 = (Lines.X1+(Lines.Y1-Lines.Y2));
                //lineD.Y2 = (Lines.Y2-(Lines.X1-Lines.X2));
    
                calcpoint(Lines.X2, Lines.Y2, k1, out xx1, out yy1, out xx2, out yy2);
                lineD.X1 = xx1;
                lineD.Y1 = yy1;
                lineD.X2 = xx2;
                lineD.Y2 = yy2;
                lineD.StrokeThickness = 3;
                lineD.Stroke = Brushes.Blue;
                this.Canvastest.Children.Add(lineD);
    
                //上面直线
                Line lineUp = new Line();
                lineUp.X1 = Lines.X1;
                lineUp.Y1 = Lines.Y1;
                lineUp.X2 = Lines.X1;
                lineUp.Y2 = Lines.Y1 - 80;
                lineUp.StrokeThickness = 3;
                lineUp.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineUp);
    
                //下面直线
                Line lineDown = new Line();
                lineDown.X1 = Lines.X2;
                lineDown.Y1 = Lines.Y2;
                lineDown.X2 = Lines.X2;
                lineDown.Y2 = Lines.Y2 + 80;
                lineDown.StrokeThickness = 3;
                lineDown.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineDown);
    
                var a = Math.Pow((Lines.X1 - Lines.X2), 2.0);
                var b = Math.Pow((Lines.Y1 - Lines.Y2), 2.0);
                Debug.WriteLine("a+b:"+(a+b));
                TearHight = a + b;
                //添加文字
                index++;
                TextBlock textBlock = new TextBlock();
                textBlock.Text = index + "," + Math.Round(Math.Abs((Lines.Y2 - Lines.Y1) / 100), 3) + "mm";
                textBlock.FontSize = 16;
               
                textBlock.Foreground = new SolidColorBrush(Colors.Green);
                textBlock.Margin = new Thickness(Lines.X1 + 5, Lines.Y1 - 80, 0, 0);
                this.Canvastest.Children.Add(textBlock);
    
                //添加正方形
    
                //上边
                Line lineReUp = new Line();
                lineReUp.X1 = Lines.X1;
                lineReUp.Y1 = Lines.Y1 - 80;
                lineReUp.X2 = Lines.X1 + 80;
                lineReUp.Y2 = Lines.Y1 - 80;
                lineReUp.StrokeThickness = 3;
                lineReUp.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineReUp);
    
                //下边
                Line lineReDown = new Line();
                lineReDown.X1 = Lines.X1;
                lineReDown.Y1 = Lines.Y1 - 50;
                lineReDown.X2 = Lines.X1 + 80;
                lineReDown.Y2 = Lines.Y1 - 50;
                lineReDown.StrokeThickness = 3;
                lineReDown.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineReDown);
    
                //右边
                Line lineReRight = new Line();
                lineReRight.X1 = Lines.X1 + 80;
                lineReRight.Y1 = Lines.Y1 - 80;
                lineReRight.X2 = Lines.X1 + 80;
                lineReRight.Y2 = Lines.Y1 - 50;
                lineReRight.StrokeThickness = 3;
                lineReRight.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineReRight);
    
                //上三角形
    
                //上右边斜角
                Line lineDeltaRightUp = new Line();
                lineDeltaRightUp.X1 = Lines.X1;
                lineDeltaRightUp.Y1 = Lines.Y1;
                lineDeltaRightUp.X2 = Lines.X1 + 20;
                lineDeltaRightUp.Y2 = Lines.Y1 - 20;
                lineDeltaRightUp.StrokeThickness = 3;
                lineDeltaRightUp.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineDeltaRightUp);
    
                //上左边斜角
                Line lineDeltaLeftUp = new Line();
                lineDeltaLeftUp.X1 = Lines.X1;
                lineDeltaLeftUp.Y1 = Lines.Y1;
                lineDeltaLeftUp.X2 = Lines.X1 - 20;
                lineDeltaLeftUp.Y2 = Lines.Y1 - 20;
                lineDeltaLeftUp.StrokeThickness = 3;
                lineDeltaLeftUp.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineDeltaLeftUp);
    
                //下三角形
    
                //下右边斜角
                Line lineDeltaRightDown = new Line();
                lineDeltaRightDown.X1 = Lines.X2;
                lineDeltaRightDown.Y1 = Lines.Y2;
                lineDeltaRightDown.X2 = Lines.X2 + 20;
                lineDeltaRightDown.Y2 = Lines.Y2 + 20;
                lineDeltaRightDown.StrokeThickness = 3;
                lineDeltaRightDown.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineDeltaRightDown);
    
                //下左边斜角
                Line lineDeltaLeftDown = new Line();
                lineDeltaLeftDown.X1 = Lines.X2;
                lineDeltaLeftDown.Y1 = Lines.Y2;
                lineDeltaLeftDown.X2 = Lines.X2 - 20;
                lineDeltaLeftDown.Y2 = Lines.Y2 + 20;
                lineDeltaLeftDown.StrokeThickness = 3;
                lineDeltaLeftDown.Stroke = new SolidColorBrush(Colors.Green);
                this.Canvastest.Children.Add(lineDeltaLeftDown);
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                var abc = Canvastest.Children.Count / 13;
    
                var test = this.Canvastest.FindName("");
    
                Canvastest.Children.RemoveAt(0);
    
                for (int i = 0; i < Canvastest.Children.Count; i++)
                {
    
                }
    
                //this.Canvastest.Children.Clear();
            }
    
            private void PreviewMouseDown(object sender, MouseButtonEventArgs e)
            {
                Point po = e.GetPosition(Hello);
                TransformGroup transformGroup = Canvastest.RenderTransform as TransformGroup;
                if (transformGroup==null)
                {
                    transformGroup = new TransformGroup();
                }
                transformGroup.Children.Add(new ScaleTransform(0.6,0.6,po.X,po.Y));
                Hello.RenderTransform = transformGroup;
            }
    
            private void MouseRightButtonDown(object sender, MouseButtonEventArgs e)
            {
                for (int i = 0; i < 13; i++)
                {
                    Canvastest.Children.RemoveAt(0);
                }
                
            }
        }
    这个是实现了鼠标点击然后拖动鼠标,当鼠标弹起那一刻进行画图,当直线画完以后,去生成其他线合并在一起,做成了一个图,现在是想把这个组装成一个类,像Line,Rectangle这样的类怎么实现?这方面的资料好像很少

    2020年6月12日 16:51

全部回复

  • 你好,

    这个实现的话很复杂,要用到大量的反射,要把画完控件保存到容器,官方文档中没有直接的方法去实现。

    谢谢


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2020年6月16日 9:02