2012-02-17 114 views
0

我有一個應用程序,我在inkCanvas上繪製多邊形。我想添加一個函數,在點擊一個繪製的多邊形之後,它將處於編輯模式,然後我可以改變一些這種比例,例如Fill。InkCanvas上選擇多邊形

我寫了這段代碼,但它從inkcanvas的左上角選擇了所有區域,直到我的多邊形的末端,但我只需要多邊形區域。

的XAML:

<DockPanel> 
    <ToolBarTray DockPanel.Dock="Left" Orientation="Vertical" IsLocked="True"> 
     <ToolBar Padding="2"> 
      <RadioButton x:Name="rbDraw" IsChecked="False" 
        ToolTip="Add Rectangle" Margin="3" Checked="rbDraw_Checked"> 
       <Rectangle Width="20" Height="12" Stroke="Blue" 
        Fill="LightBlue" /> 
      </RadioButton> 
      <RadioButton x:Name="rbSelect" IsChecked="False" 
       ToolTip="Select" Margin="3"> 
       <Path Stroke="Blue" Fill="LightBlue" Width="20" Height="20"> 
        <Path.Data> 
         <PathGeometry Figures="M5,15L 10,0 15,15 12,15 12,20 8,20 8,15Z"> 
          <PathGeometry.Transform> 
           <RotateTransform CenterX="10" CenterY="10" Angle="45"/> 
          </PathGeometry.Transform> 
         </PathGeometry> 
        </Path.Data> 
       </Path> 
      </RadioButton> 
     </ToolBar> 
    </ToolBarTray> 
    <Border BorderThickness="1" BorderBrush="Black"> 
     <InkCanvas x:Name="canvas1" MouseMove="canvas1_MouseMove" PreviewMouseLeftButtonDown="canvas1_PreviewMouseLeftButtonDown" EditingMode="None"> 
     </InkCanvas> 
    </Border> 
</DockPanel> 

代碼背後

private Polyline polyline; 
    private PointCollection polylinePoints; 
    private bool drawOnMove = false; 
    private List<Polygon> polygons = new List<Polygon>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void canvas1_MouseMove(object sender, MouseEventArgs e) 
    { 
     if (drawOnMove && (bool)rbDraw.IsChecked) 
     { 
      polyline.Points = polylinePoints.Clone(); 
      polyline.Points.Add(e.GetPosition(canvas1)); 
     } 
    } 

    private void canvas1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     if (rbDraw.IsChecked ?? false) 
     { 
      if (e.OriginalSource is Ellipse) 
      { 
       canvas1.Children.Remove((Ellipse)e.OriginalSource); 
       canvas1.Children.Remove(polyline); 
       Polygon tmpPolygon = new Polygon(); 
       tmpPolygon.StrokeThickness = 2; 
       tmpPolygon.Stroke = Brushes.Black; 
       tmpPolygon.Points = polylinePoints.Clone(); 
       polylinePoints.Clear(); 

       polygons.Add(tmpPolygon); 
       drawOnMove = false; 
       rbDraw.IsChecked = false; 
       tmpPolygon.Fill = Brushes.Gray; 
       canvas1.Children.Add(tmpPolygon); 
       rbSelect.IsChecked = true; 

      } 
      else 
      { 
       polylinePoints.Add(e.GetPosition(canvas1)); 
       polyline.Points = polylinePoints.Clone(); 

       if (polyline.Points.Count == 1) 
       { 
        Ellipse el = new Ellipse(); 
        el.Width = 10; 
        el.Height = 10; 
        el.Stroke = Brushes.Black; 
        el.StrokeThickness = 2; 
        el.Fill = new SolidColorBrush { Color = Colors.Yellow }; 
        el.Margin = 
         new Thickness(left: polyline.Points[0].X - el.Width/2, top: polyline.Points[0].Y - el.Height/2, right: 0, bottom: 0); 
        canvas1.Children.Add(el); 
       } 

       drawOnMove = true; 
      } 
     } 
     else if (rbSelect.IsChecked ?? false) 
     { 
      if (e.OriginalSource is Polygon) 
      { 
       Polygon pol = (Polygon)e.OriginalSource; 

       canvas1.Select(new UIElement[] { pol }); 
      } 
     } 
    } 

    private void rbDraw_Checked(object sender, RoutedEventArgs e) 
    { 
     polyline = new Polyline(); 
     polylinePoints = new PointCollection(); 
     polyline.StrokeThickness = 2; 
     polyline.Stroke = Brushes.Black; 
     canvas1.Children.Add(polyline); 
    } 

編輯:我編輯我的代碼我的第一個樣本有點過於籠統。選擇多邊形看起來像這樣,但我只想選擇多邊形區域。

Example

回答

0

好吧,我解決我的問題,我添加了一個自定義的依賴項屬性到我的窗口,它擁有選擇多邊形。爲了顯示選擇了多邊形,我改變了它的不透明度。

public static readonly DependencyProperty SelectedShapeProperty = 
     DependencyProperty.Register 
     ("SelectedShape", typeof(Polygon), typeof(MainWindow)); 

    public Polygon Polygon 
    { 
     set{SetValue(SelectedShapeProperty, value);} 
     get{return (Polygon) GetValue(SelectedShapeProperty);} 
    } 


private void canvas1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     if (rbDraw.IsChecked ?? false) 
     { 
      if (e.OriginalSource is Ellipse) 
      { 
       canvas1.Children.Remove((Ellipse)e.OriginalSource); 
       canvas1.Children.Remove(polyline); 
       Polygon tmpPolygon = new Polygon(); 
       tmpPolygon.StrokeThickness = 2; 
       tmpPolygon.Stroke = Brushes.Black; 
       tmpPolygon.Points = polylinePoints.Clone(); 
       polylinePoints.Clear(); 

       polygons.Add(tmpPolygon); 
       drawOnMove = false; 
       rbDraw.IsChecked = false; 
       tmpPolygon.Fill = Brushes.Gray; 
       canvas1.Children.Add(tmpPolygon); 
       rbSelect.IsChecked = true; 

      } 
      else 
      { 
       polylinePoints.Add(e.GetPosition(canvas1)); 
       polyline.Points = polylinePoints.Clone(); 

       if (polyline.Points.Count == 1) 
       { 
        Ellipse el = new Ellipse(); 
        el.Width = 10; 
        el.Height = 10; 
        el.Stroke = Brushes.Black; 
        el.StrokeThickness = 2; 
        el.Fill = new SolidColorBrush { Color = Colors.Yellow }; 
        InkCanvas.SetLeft(el, polyline.Points[0].X - el.Width/2); 
        InkCanvas.SetTop(el, polyline.Points[0].Y - el.Height/2); 

        el.Margin = 
         new Thickness(left: polyline.Points[0].X - el.Width/2, top: polyline.Points[0].Y - el.Height/2, right: 0, bottom: 0); 
        canvas1.Children.Add(el); 
       } 

       drawOnMove = true; 
      } 
     } 
     else if (rbSelect.IsChecked ?? false) 
     { 
      if (e.OriginalSource is Polygon && Polygon == null) 
      { 
       Shape s = (Shape)e.OriginalSource; 
       Polygon = (Polygon)s; 
       Polygon.Opacity = 0.75; 
      } 
      else if (e.OriginalSource is Polygon && Polygon != null) 
      { 
       Polygon.Opacity = 1; 
       Polygon = null; 
       Shape s = (Shape)e.OriginalSource; 
       Polygon = (Polygon)s; 
       Polygon.Opacity = 0.75; 
      } 
      else if (Polygon != null) 
      { 
       Polygon.Opacity = 1; 
       Polygon = null; 
      } 
     } 
     else 
     { 
      if(Polygon != null) 
       Polygon = null; 
     } 
    } 
0

您可以通過設置

drawCanvas.EditingMode = InkCanvasEditingMode.Select; 

,然後只需單擊此圖選擇畫布上的任何圖紙。

1

我知道這是一個非常古老的職位,但我有這個確切的同樣的問題,通過轉換爲多邊形前翻譯點解決它,然後再回來,因爲這樣的:

StrokeCollection sc = InkCanvas1.GetSelectedStrokes(); 
Rect r = sc.GetBounds(); 

PointCollection pc = new PointCollection(); 

//Shift all the points by the calculated extent of the strokes. 
Matrix mat = new Matrix(); 
mat.Translate(-r.Left, -r.Top); 

sc.Transform(mat, false); 
foreach (Stroke s in sc) 
{ 
    foreach (Point p in s.StylusPoints){pc.Add(p);} 
} 
Polygon poly_ = new Polygon(); 

//Shift the polygon back to original location 
poly_.SetValue(InkCanvas.LeftProperty, r.Left); 
poly_.SetValue(InkCanvas.TopProperty, r.Top); 

poly_.Points = pc; 
InkCanvas1.Children.Add(poly_); 

這使選擇框僅等於多邊形的大小:

enter image description here