WPF实现线条圆弧等几何曲线设计界面,效果及界面程序如下:
<Window x:Class="GeometryDesignerDemo.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:GeometryDesignerDemo"
mc:Ignorable="d"
Title="MainWindow" ContentRendered="OnUiReady">
<StackPanel Orientation="Horizontal" ClipToBounds="True" VerticalAlignment="Stretch">
<Grid Width="200" VerticalAlignment="Stretch" Background="LightGray" Name="LeftPanel">
<Grid.RowDefinitions>
<RowDefinition Height="100" />
<RowDefinition Height="*" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<Border Grid.Row="0" BorderThickness="2" BorderBrush="Black" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Background="LightYellow">
<Canvas>
<Label Canvas.Left="0" Canvas.Top="5" FontStyle="Italic" FontWeight="Bold" FontSize="14">Step 1:</Label>
<Label FontSize="12" Canvas.Left="10" Canvas.Top="25">Select Geometry</Label>
<ComboBox Name="GeometryTypeCB" SelectionChanged="OnGeometrySelectionChange" Canvas.Top="50"
Canvas.Left="12" MaxWidth="80" Width="80" SelectedIndex="0">
<ComboBoxItem>Line</ComboBoxItem>
<ComboBoxItem>Ellipse</ComboBoxItem>
<ComboBoxItem>Rectangle</ComboBoxItem>
<ComboBoxItem>Arc</ComboBoxItem>
<ComboBoxItem>Bezier</ComboBoxItem>
</ComboBox>
</Canvas>
</Border>
<Border Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" BorderThickness="2"
BorderBrush="Black" Background="LightYellow">
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Name="ControlPointPane">
<Canvas Name="LinePane" Width="0" Height="0" ClipToBounds="True">
<Label FontSize="14" FontStyle="Italic" FontWeight="Bold" Canvas.Left="0">Step 2:</Label>
<Label FontSize="12" FontStyle="Italic" FontWeight="Bold" Canvas.Left="55" Canvas.Top="3">Set Control points</Label>
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="47">StartPoint =</Label>
<TextBox AcceptsTab="False" Name="LineStartPoint" Width="50" Canvas.Left="90" Canvas.Top="47" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="75" xml:space="preserve">EndPoint =</Label>
<TextBox AcceptsTab="False" Name="LineEndPoint" Width="50" Canvas.Left="90" Canvas.Top="75" />
<Label FontSize="14" FontWeight="Bold" Canvas.Left="89" Canvas.Top="110">Of</Label>
<Label FontSize="14" FontWeight="Bold" Canvas.Left="45" Canvas.Top="130">Line Geometry</Label>
<Button Canvas.Top="171" Canvas.Left="70" Width="55" Click="OnInsertGeometry">Insert</Button>
</Canvas>
<Canvas Name="EllipsePane" Width="0" Height="0" ClipToBounds="True">
<Label FontSize="14" FontStyle="Italic" FontWeight="Bold" Canvas.Left="0">Step 2:</Label>
<Label FontSize="12" FontStyle="Italic" FontWeight="Bold" Canvas.Left="55" Canvas.Top="3">Set Control points</Label>
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="45" xml:space="preserve">Center = </Label>
<TextBox AcceptsTab="False" Name="EllipseCenterPoint" Width="50" Canvas.Left="90"
Canvas.Top="45" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="75" xml:space="preserve">RadiusX =</Label>
<TextBox Name="EllipseRadiusX" Width="50" Canvas.Left="90" Canvas.Top="75" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="105" xml:space="preserve">RadiusY =</Label>
<TextBox Name="EllipseRadiusY" Width="50" Canvas.Left="90" Canvas.Top="105" />
<Label FontSize="14" FontWeight="Bold" Canvas.Left="89" Canvas.Top="140">Of</Label>
<Label FontSize="14" FontWeight="Bold" Canvas.Left="30" Canvas.Top="160">Ellipse Geometry</Label>
<Button Canvas.Top="201" Canvas.Left="70" Width="55" Click="OnInsertGeometry">Insert</Button>
</Canvas>
<Canvas Name="RectanglePane" Width="0" Height="0" ClipToBounds="True">
<Label FontSize="14" FontStyle="Italic" FontWeight="Bold" Canvas.Left="0">Step 2:</Label>
<Label FontSize="12" FontStyle="Italic" FontWeight="Bold" Canvas.Left="55" Canvas.Top="3">Set Control points</Label>
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="45" xml:space="preserve">TopLeft = </Label>
<TextBox Name="RectangleTopLeftPoint" Width="50" Canvas.Left="90" Canvas.Top="45" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="75" xml:space="preserve">Width =</Label>
<TextBox Name="RectangleWidth" Width="50" Canvas.Left="90" Canvas.Top="75" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="105" xml:space="preserve">Height =</Label>
<TextBox Name="RectangleHeight" Width="50" Canvas.Left="90" Canvas.Top="105" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="135" xml:space="preserve">RadiusX =</Label>
<TextBox Name="RectangleRadiusX" Width="50" Canvas.Left="90" Canvas.Top="135">0</TextBox>
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="165" xml:space="preserve">RadiusY =</Label>
<TextBox Name="RectangleRadiusY" Width="50" Canvas.Left="90" Canvas.Top="165">0</TextBox>
<Label FontSize="14" FontWeight="Bold" Canvas.Left="89" Canvas.Top="200">Of</Label>
<Label FontSize="14" FontWeight="Bold" Canvas.Left="25" Canvas.Top="220">Rectangle Geometry</Label>
<Button Canvas.Top="261" Canvas.Left="70" Width="55" Click="OnInsertGeometry">Insert</Button>
</Canvas>
<Canvas Name="BezierPane" Width="0" Height="0" ClipToBounds="True">
<Label FontSize="14" FontStyle="Italic" FontWeight="Bold" Canvas.Left="0">Step 2:</Label>
<Label FontSize="12" FontStyle="Italic" FontWeight="Bold" Canvas.Left="55" Canvas.Top="3">Set Control points</Label>
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="45" xml:space="preserve">StartPoint = </Label>
<TextBox Name="BezierStartPoint" Width="50" Canvas.Left="90" Canvas.Top="45" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="75" xml:space="preserve">Point1 =</Label>
<TextBox Name="BezierPoint1" Width="50" Canvas.Left="90" Canvas.Top="75" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="105" xml:space="preserve">Point2 =</Label>
<TextBox Name="BezierPoint2" Width="50" Canvas.Left="90" Canvas.Top="105" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="135" xml:space="preserve">Point3 =</Label>
<TextBox Name="BezierPoint3" Width="50" Canvas.Left="90" Canvas.Top="135" />
<Label FontSize="14" FontWeight="Bold" Canvas.Left="89" Canvas.Top="175">Of</Label>
<Label FontSize="14" FontWeight="Bold" Canvas.Left="75" Canvas.Top="195">Bezier</Label>
<Button Canvas.Top="221" Canvas.Left="70" Width="55" Click="OnInsertGeometry">Insert</Button>
</Canvas>
<Canvas Name="ArcPane" Width="0" Height="0" ClipToBounds="True">
<Label FontSize="14" FontStyle="Italic" FontWeight="Bold" Canvas.Left="0">Step 2:</Label>
<Label FontSize="12" FontStyle="Italic" FontWeight="Bold" Canvas.Left="55" Canvas.Top="3">Set Control points</Label>
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="45" xml:space="preserve">StartPoint = </Label>
<TextBox Name="ArcStartPoint" Width="50" Canvas.Left="90" Canvas.Top="45" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="75" xml:space="preserve">Point =</Label>
<TextBox Name="ArcPoint" Width="50" Canvas.Left="90" Canvas.Top="75" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="105" xml:space="preserve">Size =</Label>
<TextBox Name="ArcSize" Width="50" Canvas.Left="90" Canvas.Top="105" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="135" xml:space="preserve">XRotation =</Label>
<TextBox Name="ArcXRotation" Width="50" Canvas.Left="90" Canvas.Top="135" />
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="165" xml:space="preserve">LargeArc =</Label>
<ComboBox Name="ArcLargeArc" Width="60" Canvas.Left="90" Canvas.Top="165" SelectedIndex="0">
<ComboBoxItem>TRUE</ComboBoxItem>
<ComboBoxItem>FALSE</ComboBoxItem>
</ComboBox>
<Label FontSize="12" FontWeight="Bold" Canvas.Left="5" Canvas.Top="195" xml:space="preserve">SweepArcDirection =</Label>
<ComboBox Name="ArcSweepArc" Width="60" Canvas.Left="90" Canvas.Top="195" SelectedIndex="0">
<ComboBoxItem>Clockwise</ComboBoxItem>
<ComboBoxItem>Counterclockwise</ComboBoxItem>
</ComboBox>
<Label FontSize="14" FontWeight="Bold" Canvas.Left="89" Canvas.Top="235">Of</Label>
<Label FontSize="14" FontWeight="Bold" Canvas.Left="25" Canvas.Top="255">Arc Geometry</Label>
<Button Canvas.Top="296" Canvas.Left="70" Width="55" Click="OnInsertGeometry">Insert</Button>
</Canvas>
</StackPanel>
</Border>
<Border Grid.Row="2" BorderThickness="2" BorderBrush="Black" Background="LightYellow"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch">
<StackPanel Orientation="Horizontal">
<Label FontSize="14" FontStyle="Italic" FontWeight="Bold" Canvas.Left="0">Step 3:</Label>
<Label FontSize="12" FontStyle="Italic" FontWeight="Bold" Canvas.Left="55" Canvas.Top="3">Generate xaml</Label>
</StackPanel>
<StackPanel Height="10" />
<Button Name="ShowHideXAMLButton" Width="120" Click="OnShowHideXaml">
<Label FontSize="13" FontWeight="Bold">Show XAML</Label>
</Button>
</StackPanel>
</Border>
</Grid>
<Canvas Width="1000" VerticalAlignment="Stretch" Name="DesignerPane" ClipToBounds="True">
<Canvas Width="1000" Height="1000" Canvas.Left="0" Canvas.Top="0">
<Canvas.Background>
<VisualBrush x:Name="LogoBrush"
TileMode="Tile" Viewport="0,0,0.3,0.1">
<VisualBrush.Visual>
<Border Background="Transparent">
<TextBlock
x:Name="geoText"
Text="Geometry Designer"
FontFamily="Comic Sans MS" FontSize="14pt" Foreground="LightGray"
Margin="5,5,10,5" />
</Border>
</VisualBrush.Visual>
</VisualBrush>
</Canvas.Background>
<Rectangle
Width="{Binding ElementName=DrawingPane, Path=ActualWidth}"
Height="{Binding ElementName=DrawingPane, Path=ActualHeight}">
<Rectangle.Fill>
<DrawingBrush x:Name="GridBrush"
Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile"
Opacity="0">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="#CCCCFF">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0 10,1" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="#CCCCFF">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0 1,10" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
</Rectangle.Fill>
</Rectangle>
<Canvas Width="1000" Height="1000" Canvas.Left="0" Canvas.Top="0" Name="DrawingPane" />
<Canvas.Triggers>
<EventTrigger RoutedEvent="Canvas.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="LogoBrush"
Storyboard.TargetProperty="Opacity"
BeginTime="0:0:2"
Duration="0:0:5"
From="1"
To="0" />
<DoubleAnimation
Storyboard.TargetName="GridBrush"
Storyboard.TargetProperty="Opacity"
BeginTime="0:0:5"
Duration="0:0:5"
From="0"
To="1" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Canvas.Triggers>
</Canvas>
<DockPanel Name="XAMLPane" Width="0" Height="0">
<TextBox AcceptsTab="False" VerticalAlignment="Stretch" AcceptsReturn="True" Background="LightYellow"
TextWrapping="Wrap" />
</DockPanel>
</Canvas>
</StackPanel>
</Window>