2012-02-17 4 views
0

inkCanvas에서 다각형을 그리는 응용 프로그램이 있습니다. 그려진 다각형 중 하나를 클릭 한 후 편집 모드에있는 기능을 추가하고 싶습니다. 그런 다음이 비율 중 일부를 예를 들어 채우기로 변경할 수 있습니다.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