Drawing lines in code using C# and WPF

前端 未结 2 1909
盖世英雄少女心
盖世英雄少女心 2020-12-17 09:05

I\'m trying to create a digital clock display using 7 segment displays. I can draw lines in XAML by using code like this:



        
相关标签:
2条回答
  • 2020-12-17 09:10
    public class Cls_Barriere
    {
    
        // animazione periferica
        public static void LineAnimation(Line _line,String _colore)
        {
            Storyboard result = new Storyboard();
            Duration duration = new Duration(TimeSpan.FromSeconds(2));
    
            ColorAnimation animation = new ColorAnimation();
            animation.RepeatBehavior = RepeatBehavior.Forever;
            animation.Duration = duration;
            switch (_colore.ToUpper())
            {
                case "RED": 
                    animation.From = Colors.Red;
                    break;
                case "ORANGE": 
                    animation.From = Colors.Orange;
                    break;
                case "YELLOW": 
                    animation.From = Colors.Yellow;
                    break;
                case "GRAY": 
                    animation.From = Colors.DarkGray;
                    break;
                default: 
                    animation.From = Colors.Green;
                    break;
            }
    
            animation.To = Colors.Gray;
            Storyboard.SetTarget(animation, _line);
            Storyboard.SetTargetProperty(animation, new PropertyPath("(Line.Stroke).(SolidColorBrush.Color)"));
            result.Children.Add(animation);
            result.Begin();
    
        }
    }
    //***************************************************************************  
    
    public partial class MainPage : UserControl
    {
        public Line _line;
    
        public MainPage()
        {
            InitializeComponent();
            Canvas.MouseLeftButtonDown += Canvas_MouseLeftButtonDown;
            Canvas.MouseLeftButtonUp += Canvas_MouseLeftButtonUp;
        }
    
        void Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            _line.X2 = e.GetPosition(this.Canvas).X;
            _line.Y2 = e.GetPosition(this.Canvas).Y;
            _line.Loaded += _line_Loaded;
            Canvas.Children.Add(_line);
        }
    
        void _line_Loaded(object sender, RoutedEventArgs e)
        {
            Cls_Barriere.LineAnimation(sender as Line, "RED");
        }
    
        void Canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _line = new Line();
            _line.Stroke = new SolidColorBrush(Colors.White);
            _line.StrokeThickness = 5;
            _line.StrokeStartLineCap = PenLineCap.Round; 
    
            _line.StrokeEndLineCap = PenLineCap.Round;
            _line.StrokeDashCap = PenLineCap.Round;
    
            _line.X1 = e.GetPosition(this.Canvas).X;
            _line.Y1= e.GetPosition(this.Canvas).Y;
    
        }
    
    0 讨论(0)
  • 2020-12-17 09:22

    Is that your entire drawing code? If so, you need to add the line object to your surface. If you're using a Canvas for example:

    myCanvas.Children.Add(line);
    

    This will add your line to your canvas. At the moment, you're just creating the line but not putting it anywhere.

    You can find more information on drawing in WPF on this MSDN page.

    0 讨论(0)
提交回复
热议问题