简体   繁体   English

使用C#和WPF在代码中绘制线条

[英]Drawing lines in code using C# and WPF

I'm trying to create a digital clock display using 7 segment displays. 我正在尝试使用7段显示器创建数字时钟显示器。 I can draw lines in XAML by using code like this: 我可以使用以下代码在XAML中绘制线条:

<Line Name="line7" Stroke="Black" StrokeThickness="4" X1="10" X2="40" Y1="70" Y2="70" Margin="101,-11,362,250" />

But when I try to do it in code(from MainWindow()), it doesn't work: 但是,当我尝试在代码中(从MainWindow())执行此操作时,它不起作用:

        Line line = new Line();
        Thickness thickness = new Thickness(101,-11,362,250);
        line.Margin = thickness;
        line.Visibility = System.Windows.Visibility.Visible;
        line.StrokeThickness = 4;
        line.Stroke = System.Windows.Media.Brushes.Black;
        line.X1 = 10;
        line.X2 = 40;
        line.Y1 = 70;
        line.Y2 = 70;

The idea is I can draw 7 lines, then toggle their visibility as required for different numbers. 我的想法是我可以绘制7条线,然后根据需要为不同的数字切换它们的可见性。 I'm sure this can be done many ways, but why can't I draw lines in code like this? 我敢肯定,这可以通过多种方式完成,但是为什么我不能在这样的代码中画线呢?

Is that your entire drawing code? 那是您的整个绘图代码吗? If so, you need to add the line object to your surface. 如果是这样,则需要将line对象添加到曲面。 If you're using a Canvas for example: 例如,如果您使用的是Canvas:

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 . 您可以在此MSDN页面上找到有关在WPF中进行绘图的更多信息。

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;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM