none
WPF的Inkcanvas中添加图片选中的问题。 RRS feed

  • 问题

  • 我在InkCanvas想作为stroke添加图片,以便和墨迹一起处理。使用inkcanvas的select模式时。点击图片大部分区域不能选中,只有点击边框和从右上角到左下角一条斜线区域可以选中。谁能帮我了解下是哪里出了问题。

            private void picbtn1_Click(object sender, RoutedEventArgs e)
            {
                StylusPointCollection pts = new StylusPointCollection();
                pts.Add(new StylusPoint(100, 100)); // LeftTopPoint
                pts.Add(new StylusPoint(200, 100));
                pts.Add(new StylusPoint(100, 200));
                pts.Add(new StylusPoint(200, 200));
                Stroke s = new CustomStroke1(pts);
                ink.Strokes.Add(s);
            }

            #region 自定义画图片1
            public class CustomStroke1 : Stroke
            {
                private StylusPointCollection s;
                public CustomStroke1(StylusPointCollection stylusPoints) : base(stylusPoints)
                {
                    this.s = stylusPoints;
                }
    
                //public bool IsSelected
                //{
                //    //get
                //    //{
                //    //    PropertyInfo property = typeof(Stroke).GetProperty("IsSelected",
                //    //       System.Reflection.BindingFlags.Instance |
                //    //       System.Reflection.BindingFlags.GetProperty |
                //    //       System.Reflection.BindingFlags.NonPublic);
                //    //    return (bool)property.GetValue(this, null);
                //    //}
                //}
    
    
    
    
                protected override void DrawCore(DrawingContext drawingContext, DrawingAttributes drawingAttributes)
                {
                    ImageSource imageSource = new BitmapImage(new Uri("pack://application:,,,/0.bmp"));
                    System.Windows.Point topLeft = new System.Windows.Point(StylusPoints[0].X, StylusPoints[0].Y);//100, 100
                    System.Windows.Point topRight = new System.Windows.Point(StylusPoints[1].X, StylusPoints[1].Y);//200, 100
                    System.Windows.Point centerLeft = new System.Windows.Point(StylusPoints[2].X, StylusPoints[2].Y);//100, 200
                    System.Windows.Point centerRight = new System.Windows.Point(StylusPoints[3].X, StylusPoints[3].Y);//200, 200
                    PathGeometry pathGeomery = new PathGeometry();
                    PathSegmentCollection pathCollection = new PathSegmentCollection();
                    pathCollection.Add(new LineSegment(topLeft, true));
                    pathCollection.Add(new LineSegment(topRight, true));
                    pathCollection.Add(new LineSegment(centerRight, true));
                    pathCollection.Add(new LineSegment(centerLeft, true));
                    pathCollection.Add(new LineSegment(topLeft, true));
                    PathFigure pathFigure = new PathFigure();
                    pathFigure.IsClosed = true;
                    pathFigure.IsFilled = true;
                    
    
                    pathFigure.StartPoint = topLeft;
                    
                    pathFigure.Segments = pathCollection;
                    //图片刷
                    ImageBrush myimageBrush = new ImageBrush();
                    myimageBrush.ImageSource = imageSource;
                    PathFigureCollection pathFigureCollection = new PathFigureCollection();
                    pathFigureCollection.Add(pathFigure);
                    pathGeomery.Figures = pathFigureCollection;
                    drawingContext.DrawGeometry(myimageBrush, new System.Windows.Media.Pen(System.Windows.Media.Brushes.Blue, 2), pathGeomery);
                    this.s = null;
    
                }
            }
    
            #endregion
    

    2016年6月19日 11:29

全部回复

  • 你好 子夜十分:

    问题出在你对Stroke的继承。

    你在DrawCore中的代码只是描述了如何绘制出此Stroke的外观,对于Stroke内部,其逻辑形状依然是你设置的四个点:

    (100, 100),
    (200, 100),
    (100, 200),
    (200, 200)。

    于是,该Stroke的形状为“Z”字形,如图:

    Stroke形状示例

    于是,只有当点击红线所示“Z”字形墨迹时,整个Stroke才会被选中(你可以试试,左右边框是选中不了的)。

    2016年6月19日 14:18