Click here to Skip to main content
15,897,891 members
Articles / Desktop Programming / WPF

Stacked Geometry Brush Factory

Rate me:
Please Sign up or sign in to vote.
4.95/5 (68 votes)
16 Jul 2010CPOL13 min read 80K   2.6K   103  
A geometric visual brush producer with plug-in architecture and customized XAML.
using System;
using StackedGeometry;
using System.Windows;
using System.Windows.Shapes;
using System.Reflection;
using System.Windows.Media;

namespace GeometrySources
{
    [GeometrySource]
    [CustomXaml]
public class ShapeSource:DependencyObject ,IGeometrySource 
    {
        private static MethodInfo sEllipseCacheDefiningGeometry;
        private static PropertyInfo sEllipseDefiningGeometry;

        private static MethodInfo sLineCacheDefiningGeometry;
        private static PropertyInfo sLineDefiningGeometry;   
 
        private static MethodInfo sPolygonCacheDefiningGeometry;
        private static PropertyInfo sPolygonDefiningGeometry;

        private static MethodInfo sPolylineCacheDefiningGeometry;
        private static PropertyInfo sPolylineDefiningGeometry;

        private static MethodInfo sRectangleCacheDefiningGeometry;
        private static PropertyInfo sRectangleDefiningGeometry;
         
        private bool mIsFrozen;
        static ShapeSource()
        {
            Type shptype = typeof(Ellipse);
            sEllipseCacheDefiningGeometry = shptype.GetMethod("CacheDefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
            sEllipseDefiningGeometry = shptype.GetProperty("DefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);

            shptype = typeof(Line);
            sLineCacheDefiningGeometry = shptype.GetMethod("CacheDefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
            sLineDefiningGeometry = shptype.GetProperty("DefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
        
            shptype = typeof(Polygon);
            sPolygonCacheDefiningGeometry = shptype.GetMethod("CacheDefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
            sPolygonDefiningGeometry = shptype.GetProperty("DefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);

            shptype = typeof(Polyline);
            sPolylineCacheDefiningGeometry = shptype.GetMethod("CacheDefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
            sPolylineDefiningGeometry = shptype.GetProperty("DefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);

            shptype = typeof(Rectangle);
            sRectangleCacheDefiningGeometry = shptype.GetMethod("CacheDefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
            sRectangleDefiningGeometry = shptype.GetProperty("DefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
        }

        #region Shape

        public static DependencyProperty ShapeProperty = DependencyProperty.Register("Shape", typeof(Shape), typeof(ShapeSource),
        new PropertyMetadata(null, OnShapeChanged ));

        public Shape Shape
        {
            get { return (Shape)GetValue(ShapeProperty); }

            set { SetValue(ShapeProperty, value); }

        }


        private static void OnShapeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ShapeSource ss = (ShapeSource) d;
            if (ss.Changed != null)
            {
                ss.Changed(ss, new EventArgs());

            }

        }
     
        #endregion

        public   Geometry CreateGeometry()
        {
            if (Shape == null)
            { return null; }

            Type shapeType = Shape.GetType();
            if (shapeType == typeof(Path))
            {
                return ((Path)Shape).Data ;
            }
            if (shapeType == typeof(Polyline))
            {
                sPolylineCacheDefiningGeometry.Invoke(Shape, null);
                return (Geometry)sPolylineDefiningGeometry.GetValue(Shape, null);
            }
            if (shapeType == typeof(Polygon))
            {
                sPolygonCacheDefiningGeometry.Invoke(Shape, null);
                return (Geometry)sPolygonDefiningGeometry.GetValue(Shape, null);

            }
            if (shapeType == typeof(Ellipse))
            {
                sEllipseCacheDefiningGeometry.Invoke(Shape, null);
                return (Geometry)sEllipseDefiningGeometry.GetValue(Shape, null);

            }
            if (shapeType == typeof(Rectangle))
            {
                sRectangleCacheDefiningGeometry.Invoke(Shape, null);
                return (Geometry)sRectangleDefiningGeometry.GetValue(Shape, null);
            }
            if (shapeType == typeof(Line))
            {
                sLineCacheDefiningGeometry.Invoke(Shape, null);
                return (Geometry)sLineDefiningGeometry.GetValue(Shape, null);
            }
            MethodInfo mi = shapeType.GetMethod("CacheDefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
            PropertyInfo pi = shapeType.GetProperty("DefiningGeometry", BindingFlags.NonPublic | BindingFlags.Instance);
            mi.Invoke(Shape, null);
            return (Geometry)pi.GetValue(Shape, null);
        }
  


        #region IGeometrySource Members

        public string DisplayName
        {
            get { return "Shape Geometry"; }
        }

        public DataTemplate DataTemplate
        {
            get
            {
                ResourceDictionary rd = new ResourceDictionary();
                rd.Source = new Uri("GeometrySources;component/ShapeResources.xaml", UriKind.Relative);
                DataTemplate dt = (DataTemplate)rd["ShapeGeometry"];
                return dt;
            }
        }

  
        public event EventHandler Changed;



        public bool IsFrozen
        {
            get
            {
                return mIsFrozen;
            }
            set
            {
                mIsFrozen = value;
            }
        }


        private StackedGeometry.StackedGeometry mStackedGeometry;


        public void SetStackedGeometry(StackedGeometry.StackedGeometry stackedGeometry)
        {
            mStackedGeometry = stackedGeometry;
        }

        public StackedGeometry.StackedGeometry GetStackedGeometry()
        {
            return mStackedGeometry;
        }

        #endregion



        #region IGeometrySource Members

        private Geometry mGeometrySource;
        [XamlIgnore("IsFrozen", false)]
        public Geometry Geometry
        {
            get
            {
                return mGeometrySource;
            }
            set
            {
                mGeometrySource = value;
            }
        }

        private bool mDesignMode;
        public bool DesignMode
        {
            get
            {
                return mDesignMode;
            }
            set
            {
                mDesignMode = value;
            }
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
Written software for what seems like forever. I'm currenly infatuated with WPF. Hopefully my affections are returned.

Comments and Discussions