Creating Lines : Line « Windows Presentation Foundation « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Windows Presentation Foundation » Line 
24.84.7.Creating Lines
<Window x:Class="WpfApplication1.PerpendicularLine"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Perpendicular Line" Height="300" Width="400">
  <Viewbox Stretch="Uniform">
    <Canvas Name="canvas1" Grid.Column="1" Margin="10"
      ClipToBounds="True" Width="270" Height="280">
        <TextBlock Name="tbPoint1" Canvas.Top="10">
          Point1
        </TextBlock>
        <TextBlock Name="tbPoint2" Canvas.Top="25">
          Point2
        </TextBlock>
        <TextBlock Name="tbPoint3" Canvas.Top="40">
          Point3
        </TextBlock>
        <TextBlock Name="tbPoint4" Canvas.Top="55">
          Point4
        </TextBlock>
      </Canvas>
  </Viewbox>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;


namespace WpfApplication1
{
    public partial class PerpendicularLine : Window
    {
        private Line line1;
        private Line line2;
        public PerpendicularLine()
        {
            InitializeComponent();
            Rectangle rect = new Rectangle();
            rect.Stroke = Brushes.Black;
            rect.Width = canvas1.Width;
            rect.Height = canvas1.Height;
            canvas1.Children.Add(rect);
            line1 = new Line();
            line2 = new Line();
            AddLines();
        }
        private void AddLines()
        {
            Point pt1 = new Point();
            Point pt2 = new Point();

            pt1.X = 30;
            pt1.Y = 300;
            pt2.X = 100;
            pt2.Y = 500;
            double length = 400;
            line1 = new Line();
            line1.X1 = pt1.X;
            line1.Y1 = pt1.Y;
            line1.X2 = pt2.X;
            line1.Y2 = pt2.Y;
            line1.Stroke = Brushes.Gray;
            line1.StrokeThickness = 4;
            canvas1.Children.Add(line1);
            Canvas.SetLeft(tbPoint1, pt1.X);
            Canvas.SetTop(tbPoint1, pt1.Y);
            Canvas.SetLeft(tbPoint2, pt2.X);
            Canvas.SetTop(tbPoint2, pt2.Y);
            tbPoint1.Text = "Pt1(" + pt1.ToString() ")";
            tbPoint2.Text = "Pt2(" + pt2.ToString() ")";

            Vector v1 = pt1 - pt2;
            Matrix m1 = new Matrix();
            Point pt3 = new Point();
            Point pt4 = new Point();
            m1.Rotate(-90);
            v1.Normalize();
            v1 *= length;
            line2 = new Line();
            line2.Stroke = Brushes.Gray;
            line2.StrokeThickness = 4;
            line2.StrokeDashArray =

            DoubleCollection.Parse("3, 1");
            pt3 = pt2 + v1 * m1;
            m1 = new Matrix();
            m1.Rotate(90);
            pt4 = pt2 + v1 * m1;
            line2.X1 = pt3.X;
            line2.Y1 = pt3.Y;
            line2.X2 = pt4.X;
            line2.Y2 = pt4.Y;
            canvas1.Children.Add(line2);
            Canvas.SetLeft(tbPoint3, pt3.X);
            Canvas.SetTop(tbPoint3, pt3.Y);
            Canvas.SetLeft(tbPoint4, pt4.X);
            Canvas.SetTop(tbPoint4, pt4.Y);
            pt3.X = Math.Round(pt3.X, 0);
            pt3.Y = Math.Round(pt3.Y, 0);
            pt4.X = Math.Round(pt4.X, 0);
            pt4.Y = Math.Round(pt4.Y, 0);
            tbPoint3.Text = "Pt3(" + pt3.ToString() ")";
            tbPoint4.Text = "Pt4(" + pt4.ToString() ")";
        }

    }
}
WPF Creating Lines
24.84.Line
24.84.1.Create a line with point valueCreate a line with point value
24.84.2.Set Top and left dimension based on its containerSet Top and left dimension based on its container
24.84.3.Origin in center. Y increases going downOrigin in center. Y increases going down
24.84.4.Draws a line from (10,10) to (50,50)Draws a line from (10,10) to (50,50)
24.84.5.Draws a diagonal line from (10,10) to (50,50) and moves it 100 pixels to the rightDraws a diagonal line from (10,10) to (50,50) and moves it 100 pixels to the right
24.84.6.Draw a GridDraw a Grid
24.84.7.Creating LinesCreating Lines
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.