[分享]向量在WPF的简单应用
程序的效果图:由于程序比较简单,所以我就直接上代码好了.
以下是WPF的XAML代码:
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="400"
xmlns:Microsoft_Windows_ThemesAero="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35;component/themes/Royale.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Viewbox Stretch="Uniform">
<Grid Width="430" Height="300" Horiz VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="280"/>
</Grid.ColumnDefinitions>
<Grid Width="140" Height="300" Margin="5,10,5,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="70"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Horiz Grid.Column="0" Grid.Row="0" Margin="5,5,10,5">X1</TextBlock>
<TextBox Name="tbX1" Grid.Column="1" Grid.Row="0" TextAlignment="Center">50</TextBox>
<TextBlock Horiz Grid.Column="0" Grid.Row="1" Margin="5,5,10,5">Y1</TextBlock>
<TextBox Name="tbY1" Grid.Column="1" Grid.Row="1" TextAlignment="Center">200</TextBox>
<TextBlock Horiz Grid.Column="0" Grid.Row="2" Margin="5,5,10,5">X2</TextBlock>
<TextBox Name="tbX2" Grid.Column="1" Grid.Row="2" TextAlignment="Center">150</TextBox>
<TextBlock Horiz Grid.Column="0" Grid.Row="3" Margin="5,5,10,5">Y2</TextBlock>
<TextBox Name="tbY2" Grid.Column="1" Grid.Row="3" TextAlignment="Center">100</TextBox>
<TextBlock Horiz Grid.Column="0" Grid.Row="4" Margin="5,5,10,5">Length</TextBlock>
<TextBox Name="tbLength" Grid.Column="1" Grid.Row="4" TextAlignment="Center">100</TextBox>
<Button Click="Button_Click" Margin="15,20,15,5" Grid.Row="5" Height="25" Grid.ColumnSpan="2" Grid.Column="0">Apply</Button>
<Button Click="Button_Click_1" Margin="15,0,15,5" Grid.Row="6" Height="25" Grid.ColumnSpan="2" Grid.Column="0">Close</Button>
</Grid>
<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="10">Point2</TextBlock>
<TextBlock Name="tbPoint3" Canvas.Top="10">Point3</TextBlock>
<TextBlock Name="tbPoint4" Canvas.Top="10">Point4</TextBlock>
</Canvas>
</Grid>
</Viewbox>
</Window>
以下是WPF后台的C#代码:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
namespace WpfApplication6
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private Line line1;
private Line line2;
public MainWindow()
{
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 = Convert.ToDouble(this.tbX1.Text);
pt1.Y = Convert.ToDouble(this.tbY1.Text);
pt2.X = Convert.ToDouble(this.tbX2.Text);
pt2.Y = Convert.ToDouble(this.tbY2.Text);
double length = 0.5 * Convert.ToDouble(tbLength.Text);
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.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() + ")";
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (line1 != null)
canvas1.Children.Remove(line1);
if (line2 != null)
canvas1.Children.Remove(line2);
AddLines();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
<p>以下为VS2010的源码工程文件:</p>
<p></p> <p>为了满足大众的口味,再提供Vs2008的源码工程文件:</p>
<p></p> 感谢大佬
页:
[1]