SilverLight 画箭头

<!-----------------------------------前台代码开始---------------------->
    <Grid x:Name="LayoutRoot" Background="White">

        <Canvas x:Name="CanvasDevice" >

        </Canvas>
        <Canvas>
            <TextBlock Text="X1:" Height="20" Width="20"></TextBlock>
            <TextBox Text="250" x:Name="X1" Canvas.Left="30" Canvas.Top="0"></TextBox>
            <TextBlock Text="Y1:"  Height="20" Width="20" Canvas.Left="80" Canvas.Top="0"></TextBlock>
            <TextBox Text="250" x:Name="Y1" Canvas.Left="110" Canvas.Top="0"></TextBox>
            <TextBlock Text="X2:" Height="20" Width="20" Canvas.Left="150" Canvas.Top="3"></TextBlock>
            <TextBox Text="250" x:Name="X2" Canvas.Left="178" Canvas.Top="0"></TextBox>
            <TextBlock Text="Y2:" Height="20" Width="20" Canvas.Left="225" Canvas.Top="4"></TextBlock>
            <TextBox Text="500" x:Name="Y2" Canvas.Left="251" Canvas.Top="0"></TextBox>
            <Button Canvas.Left="529" Canvas.Top="7" Content="生成图线" Height="23" Name="button1" Width="75" Click="button1_Click" />
            <TextBlock Text="线长度:" Height="20" Width="50" Canvas.Left="303" Canvas.Top="5"></TextBlock><TextBox Canvas.Left="348" Canvas.Top="3" Height="23" Name="txtLineLength" Text="10" Width="45" />
<TextBlock Text="偏移角度:" Height="20" Width="60" Canvas.Left="423" Canvas.Top="6"></TextBlock><TextBox Canvas.Left="487" Canvas.Top="2" Height="23" Name="txtAngle" Text="30" Width="36" />
        </Canvas>
    </Grid>

    <!-------------------------------前台代码结束-------------------------------------------------->

/*---------------------------------------------后台代码开始------------------------------------------------------------*/

 private void button1_Click(object sender, RoutedEventArgs e)
        {
            CanvasDevice.Children.Clear();
            //获取需要设置的线的长度
            double lineLenth = double.Parse(this.txtLineLength.Text.Trim().ToString());
            //设置平均角度
            double angle = double.Parse(this.txtAngle.Text.Trim().ToString()); ;
            double LinesStrokeThickness = 1;

            double sinAngle = 0;
            double cosAngle = 0;

            double x1 = double.Parse(X1.Text.Trim());
            double x2 = double.Parse(X2.Text.Trim());
            double y1 = double.Parse(Y1.Text.Trim());
            double y2 = double.Parse(Y2.Text.Trim());
            Line dline = new Line();
            dline.X1 = x1;
            dline.Y1 = y1;

            dline.X2 = x2;
            dline.Y2 = y2;

            dline.Stroke = new SolidColorBrush(Colors.Black);
            dline.StrokeThickness = LinesStrokeThickness;

            double Angle = Math.Atan2(Math.Abs(y1 - y2), Math.Abs(x1 - x2)) * 180 / Math.PI;

            Line line2 = new Line();
            line2.X1 = dline.X2;
            line2.Y1 = dline.Y2;
            double angle1 = Angle + angle;

            sinAngle = Math.Sin((angle1) * Math.PI / 180);
            cosAngle = Math.Cos((angle1) * Math.PI / 180);

            if (Angle == 0.0)
            {
                line2.X2 = line2.X1 + (x2 > x1 ? -lineLenth * cosAngle : lineLenth * cosAngle);

            }
            else if (Angle == 90.0)
            {
                line2.X2 = line2.X1 - lineLenth * cosAngle;
            }
            else
            {
                if (x2 > x1)
                {
                    line2.X2 = line2.X1 - lineLenth * cosAngle;
                }
                else
                {
                    line2.X2 = line2.X1 + lineLenth * cosAngle;
                }
            }
            if (y2 > y1)
            {
                line2.Y2 = line2.Y1 - lineLenth * sinAngle;
            }
            else
            {
                line2.Y2 = line2.Y1 + lineLenth * sinAngle;
            }
            line2.StrokeThickness = LinesStrokeThickness;
            line2.Stroke = new SolidColorBrush(Colors.Red);

            Line line3 = new Line();
            line3.X1 = x2;
            line3.Y1 = y2;

            angle1 = Angle - angle;

            sinAngle = Math.Sin((angle1) * Math.PI / 180);
            cosAngle = Math.Cos((angle1) * Math.PI / 180);

            if (x1 > x2)
            {
                line3.X2 = line3.X1 + lineLenth * cosAngle;
            }
            else
            {
                line3.X2 = line3.X1 - lineLenth * cosAngle;
            }
            if (y2 > y1)
            {
                line3.Y2 = line3.Y1 - lineLenth * sinAngle;
            }
            else
            {
                line3.Y2 = line3.Y1 + lineLenth * sinAngle;
            }
            line3.StrokeThickness = LinesStrokeThickness;
            line3.Stroke = new SolidColorBrush(Colors.Red);
            CanvasDevice.Children.Add(line2);
            CanvasDevice.Children.Add(line3);
            CanvasDevice.Children.Add(dline);

        }

  /*---------------------------------------------后台代码结束------------------------------------------------------------*/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值