在DrawingVisual中更改GeometryDrawing的颜色

时间:2015-08-04 14:23:16

标签: c# wpf graphics

我无法使用DrawingVisual的DrawingContext以下列方式更改放置在DrawingVisual中的GeometryDrawing的颜色:

   using (DrawingContext ctx = myRenderSurface.RenderOpen())
            {
                for (int y = -500; y <= 500; y += 40)
                {
                    for (int x = -500; x <= 500; x += 40)
                    {
                        ctx.DrawEllipse(new SolidColorBrush(Color.FromRgb((byte)x, (byte)y, (byte)10)), null, new Point(x, y), 15, 15);
                    }
                }
            }

如果我现在想要进行命中测试并更改GeometryDrawing(画笔)的颜色,那么当我将DrawingVisual渲染为RenderTargetBitmap时,它不会被反射出来:

        protected override void OnMouseMove( MouseEventArgs e )
        {
            base.OnMouseMove( e );

            if( e.LeftButton == MouseButtonState.Pressed )
            {
                aTranslateTransform.X -= LastMouseXY.X - e.GetPosition( null ).X;
                aTranslateTransform.Y -= LastMouseXY.Y - e.GetPosition( null ).Y;

                InvalidateVisual();
                LastMouseXY = e.GetPosition( null );
            }
            else
            {
                Point pt = e.GetPosition( this );
                DrawingGroup dGroup = VisualTreeHelper.GetDrawing( myRenderSurface );
                EnumDrawingGroup( dGroup, pt );
            }

            ReRender();
        }

// Enumerate the drawings in the DrawingGroup.
            public void EnumDrawingGroup(DrawingGroup drawingGroup, Point pt)
            {
                DrawingCollection drawingCollection = drawingGroup.Children;

                // Enumerate the drawings in the DrawingCollection.
                foreach (Drawing drawing in drawingCollection)
                {
                    // If the drawing is a DrawingGroup, call the function recursively.
                    if (drawing.GetType() == typeof(DrawingGroup))
                    {
                        EnumDrawingGroup((DrawingGroup)drawing, pt);
                    }
                    else if( drawing.GetType() == typeof( GeometryDrawing ) )
                    {
                        GeometryDrawing aDrawing = (GeometryDrawing) drawing;

                        // Determine whether the hit test point falls within the geometry.
                        if( aDrawing.Geometry.FillContains( pt ) )
                        {
                            // Perform action based on hit test on geometry.
                            aDrawing.Brush = Brushes.White;
                        }
                    }
                }
            }

所以我的问题是,如果DrawingVisual的绘图列表中的对象之间存在差异,或者是否存在某种支持机制,我无法真正访问DrawingContext创建的对象?

0 个答案:

没有答案