2017-07-01 29 views
0

我對這些東西很新,所以任何幫助將不勝感激。另外,我看了很多解決方案,但沒有找到具體的解決方案。 所以問題是:如何在c中重用形狀#

我試圖創建一個程序,從文件讀取一些圖形矢量形狀,繪製它們,然後可以選擇其中之一,修改形狀並重新保存整個文件。 我在想,建立一個形狀的xaml資源文件將是一個開始。 第一個問題:如果我在xaml資源中定義了一個簡單的形狀(橢圓,矩形,路徑),如何在代碼中重用它?一個簡單的例子會對我有很大幫助。 第二個問題:爲了修改形狀,我可能必須將所有基本形狀轉換爲路徑,才能在點(頂點)上應用代碼。我怎麼能這樣做? 第三個問題:一些形狀是幾個基本形狀相結合的結果。假設我多次使用組合幾何體以達到最終形狀。但是,如何獲得組合幾何的路徑數據,以便解決第二個問題?

我知道,這很多,而且還有更多,但對於這些我還找不到任何答案,所以請和我一起裸照。 謝謝。

+1

何不你是不是在尋找和開始一些事情並回到你正面臨的確切問題?另請檢查:[我如何問一個好問題](https://stackoverflow.com/help/how-to-ask) –

回答

1

要「重複使用」形狀,您可以將樣式定義爲App.xaml。例如:

App.xaml

<Application x:Class="WpfApp4.App" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:local="clr-namespace:WpfApp4" 
      StartupUri="MainWindow.xaml"> 

    <Application.Resources> 

     <Style x:Key="YellowEllipseWithBlackBorder" TargetType="Ellipse"> 

      <Setter Property="Fill" Value="Yellow" /> 
      <Setter Property="Width" Value="100" /> 
      <Setter Property="Height" Value="100" /> 
      <Setter Property="StrokeThickness" Value="5" /> 
      <Setter Property="Stroke" Value="Black" /> 

     </Style> 

    </Application.Resources> 

</Application> 

MainWindow.xaml

<Window x:Class="WpfApp4.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d" 
     Title="MainWindow" Height="350" Width="525"> 

    <Grid> 

     <StackPanel Orientation="Horizontal" HorizontalAlignment="Center"> 
      <Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" /> 
      <Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" /> 
      <Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" /> 
     </StackPanel> 

    </Grid> 

</Window> 

結果:

Three ellipses with the same style

正如你所看到的,你應該定義一個樣式爲你塑造和t母雞從窗口這個語法應用它:<Ellipse Style="{StaticResource YellowEllipseWithBlackBorder}" />


如果您希望以編程方式做同樣的,你可以在前面的例子中定義形狀的風格一樣,然後在窗口的後臺代碼:

public partial class MainWindow : Window 
{ 
    private StackPanel _stackPanel; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     _stackPanel = new StackPanel() 
     { 
      Orientation = Orientation.Horizontal, 
      HorizontalAlignment = HorizontalAlignment.Center, 
      VerticalAlignment = VerticalAlignment.Center 
     }; 

     Content = _stackPanel; 

     AddEllipse(); 
     AddEllipse(); 
     AddEllipse(); 
    } 

    public void AddEllipse() 
    { 
     var ellipse = new Ellipse() 
     { 
      Style = FindResource("YellowEllipseWithBlackBorder") as Style 
     }; 

     _stackPanel.Children.Add(ellipse); 
    } 
} 

要繪製自定義形狀,你可以使用Polygon控制,您可以在其中定義形狀的點,例如:

public void AddPolygon() 
    { 
     var polygon = new Polygon() 
     { 
      Fill = new SolidColorBrush(Colors.Red), 
      Stroke = new SolidColorBrush(Colors.Black), 
      StrokeThickness = 2.0 
     }; 

     polygon.Points.Add(new Point(0, 0)); 
     polygon.Points.Add(new Point(10, 0)); 
     polygon.Points.Add(new Point(5, -10)); 

     _stackPanel.Children.Add(polygon); 
    } 

結果:

Polygon


畫一些曲線,你可以使用Path控制與BezierSegment,例如:

public void AddPath() 
    { 
     var canvas = new Canvas(); 

     var path = new Path() 
     { 
      Fill = new SolidColorBrush(Colors.Red), 
      Stroke = new SolidColorBrush(Colors.Black), 
      StrokeThickness = 2.0 
     }; 

     var geometry = new PathGeometry(); 
     geometry.Figures.Add(new PathFigure(
      new Point(0, 0), 
      new List<BezierSegment>() 
      { 
       new BezierSegment(
        new Point(0, 0), 
        new Point(100, 0), 
        new Point(50, -100), 
        true) 
      }, 
      false)); 

     path.Data = geometry; 
     canvas.Children.Add(path); 
     _stackPanel.Children.Add(canvas); 
    } 

Path with BezierSegment

+0

謝謝你的例子。但是,我試圖從代碼中重用橢圓,而不是從xaml窗口中,我不知道是否有可能。除此之外,我想要做的一個例子是將橢圓轉換爲路徑,因此在代碼中,我可以移動橢圓的單個點,因此最終會變成不規則形狀。 – Calin

相關問題