Tuesday, March 8, 2011

Shapes in WPF (code)

Take a look at the namespace System.Windows.Shapes, which contains an abstract class named Shape and six other classes that derive from it. These classes also derive from UIElement by way of FrameworkElement:

Object
--DispatcherObject (abstract)
------DependencyObject
----------Visual (abstract)
--------------UIElement
--------------------FrameworkElement
--------------------------Shape (abstract)
--------------------------------Ellipse
--------------------------------Line
--------------------------------Path
--------------------------------Polygon
--------------------------------Polyline
--------------------------------Rectangle

While Image is the standard way to display raster images, these Shape classes implement simple two-dimensional vector graphics. Eg. lets take ellipse:

Ellipse elips = new Ellipse();
elips.Fill = Brushes.AliceBlue;
elips.StrokeThickness = 24; // 1/4 inch
elips.Stroke = new LinearGradientBrush(Colors.CadetBlue, Colors.Chocolate,
new Point (1, 0), new Point(0, 1));


Neither the Shape class nor the Ellipse class defines any properties to set a size of the ellipse, but the Ellipse class inherits Width and Height properties from FrameworkElement, and these do just fine:

elips.Width = 300;
elips.Height = 300;



No comments:

Post a Comment