WPF自定義座標系並且繪製
界面代碼如下:
<Window x:Class="Line.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Line"
mc:Ignorable="d"
Title="MainWindow" Height="420" Width="360">
<Viewbox Stretch="Uniform">
<StackPanel Height="420" Width="360">
<Canvas x:Name="plotCanvas" ClipToBounds="True"
Width="300" Height="250" Margin="30 30 30 30">
<Rectangle x:Name="plotArea" Width="300" Height="250"
Stroke="Black" StrokeThickness="1"/>
</Canvas>
<Grid Width="340" Height="100" HorizontalAlignment="Left"
VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="110"/>
<ColumnDefinition Width="60"/>
<ColumnDefinition Width="110"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Margin="20 5 10 0">Xmin</TextBlock>
<TextBox x:Name="tbxMin" Grid.Column="1" Grid.Row="0" TextAlignment="Center"
>0</TextBox>
<TextBlock Grid.Column="2" Grid.Row="0" Margin="20 5 10 5">XMax</TextBlock>
<TextBox x:Name="tbxMax" Grid.Column="3" Grid.Row="0" TextAlignment="Center">10</TextBox>
<TextBlock Grid.Column="0" Grid.Row="1" Margin="20 5 10 5">YMin</TextBlock>
<TextBox Name="tbxYMin" Grid.Column="1" Grid.Row="1" TextAlignment="Center">0</TextBox>
<TextBlock Grid.Column="2" Grid.Row="1" Margin="20 5 10 5">YMax</TextBlock>
<TextBox Name="tbYMax" Grid.Column="3" Grid.Row="1" TextAlignment="Center">10</TextBox>
<Button Click="Button_Click" Margin="40 20 20 0" Height="25" Grid.ColumnSpan="2"
Grid.Column="0" Grid.Row="2">Apply</Button>
<Button Click="Button_Click_1" Margin="40 20 20 0" Height="25" Grid.ColumnSpan="2"
Grid.Column="2" Grid.Row="2">Close</Button>
</Grid>
</StackPanel>
</Viewbox>
</Window>
後臺代碼:
#region NameSpaces
using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
#endregion
namespace Line
{
/// <summary>
/// MainWindow.xaml 的交互邏輯
/// </summary>
public partial class MainWindow : Window
{
private double xMin = 0;
private double xMax = 10;
private double yMin = 0;
private double yMax = 10;
private System.Windows.Shapes.Line line1;
private Polyline polyline1;
public MainWindow()
{
InitializeComponent();
AddGraphics();
}
private void AddGraphics()
{
line1 = new System.Windows.Shapes.Line();
line1.X1 = XNormalize(2.0);
line1.Y1 = YNormmalize(4.0);
line1.X2 = XNormalize(8.0);
line1.Y2 = YNormmalize(10.0);
line1.Stroke = Brushes.Blue;
line1.StrokeThickness = 2;
plotCanvas.Children.Add(line1);
// polyline1
polyline1 = new Polyline();
polyline1.Points.Add(new Point(XNormalize(8), YNormmalize(8)));
polyline1.Points.Add(new Point(XNormalize(6),YNormmalize(8)));
polyline1.Points.Add(new Point(XNormalize(4), YNormmalize(4)));
polyline1.Points.Add(new Point(XNormalize(4), YNormmalize(6)));
polyline1.Points.Add(new Point(XNormalize(6), YNormmalize(6)));
polyline1.Stroke = Brushes.Red;
polyline1.StrokeThickness = 5;
plotCanvas.Children.Add(polyline1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
xMin = Convert.ToDouble(tbxMin.Text);
xMax = Convert.ToDouble(tbxMax.Text);
yMin = Convert.ToDouble(tbxYMin.Text);
yMax = Convert.ToDouble(tbYMax.Text);
plotCanvas.Children.Remove(line1);
plotCanvas.Children.Remove(polyline1);
AddGraphics();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.Close();
}
private double XNormalize(double x)
{
double res = (x - xMin) * plotCanvas.Width / (xMax - xMin);
return res;
}
/// <summary>
/// 單位化Y值
/// </summary>
/// <param name="y"></param>
/// <returns></returns>
private double YNormmalize(double y)
{
double res = plotCanvas.Height - (y - yMin) * plotCanvas.Height / (yMax - yMin);
return res;
}
}
}
界面效果如下:
- 參考:《Practical WPF Graphics Programming》