Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Working With Pen in WPF

Working With Pen in WPF

Ratings: (0)|Views: 0|Likes:
Published by Abhi

More info:

Published by: Abhi on Mar 12, 2012
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Working with Pen in WPF
While a brush is used to fill shapes with colors, a pen is used to draw outlines of a shape.The Pen element in XAML is used to create a pen at design-time. The Brush and Thickness are only two requiredattributes you need to set to create a pen. The Brush property represents the brush that is used to draw an outline andThickness property represents the thickness of the outline.A Pen can be created by any kind of brushes discussed above and once a Brush is set for a pen, that brush is used todraw the outlines of a shape. The code listed in Listing 30 creates two Pen elements. First Pen is created using aSolidColorBrush and second Pen is created using a LinearGradientBrush.
<!-- SolidColorBrush Pen --><Penx:Key="SolidYellowPen"Thickness="5"Brush="Yellow" /> <!-- GradientBrush Pen --><Penx:Key="YellowGreenBrush"Thickness="5" >  <Pen.Brush>  <LinearGradientBrush>  <GradientStopOffset="0.0"Color="Green" />  <GradientStopOffset="1.0"Color="Yellow" />  </LinearGradientBrush>  </Pen.Brush> </Pen>
 Listing 30
The code snippet in Listing 31 creates a GeometryDrawing with a Line and two Rectnagle objects` and usesGeometryDrawing.Pen to draw the outline of the GeometryDrawing. As you can see from Pen element, thethickness of Pen is 5.
<RectangleWidth="200"Height="200"Stroke="Black"StrokeThickness="0">  <Rectangle.Fill>  <DrawingBrush>  <DrawingBrush.Drawing>  <GeometryDrawingBrush="Yellow">
 <GeometryDrawing.Geometry>  <GeometryGroup>  <LineGeometryStartPoint="25,25"EndPoint="75,75" />  <RectangleGeometryRect="50,25,25,25" />  <RectangleGeometryRect="25,50,25,25" />  </GeometryGroup>  </GeometryDrawing.Geometry>  <GeometryDrawing.Pen>  <PenThickness="5" >  <Pen.Brush>  <LinearGradientBrush>  <GradientStopOffset="0.0"Color="Blue" />  <GradientStopOffset="1.0"Color="Black" />  </LinearGradientBrush>  </Pen.Brush>  </Pen>  </GeometryDrawing.Pen>  </GeometryDrawing>  </DrawingBrush.Drawing>  </DrawingBrush>  </Rectangle.Fill> </Rectangle>
 Listing 31
The output of Listing 31 looks like Figure 36. As you may notice, the outline of rectangles and line is just flat.
 Figure 36 
 Now let's add some creativity to the Pen. Pen supports several more properties including StartLineCap, EndLineCap,DashCap, LineJoin, and MiterLimit.StartLineCap and EndLineCap define the type of shape to use at the beginning and end of a stroke. The value can beflat, square, triangle and round. LineJoin property defines the type of joint used at the vertices of a shape's outline.The value of LineJoin is type of PenLineJoin enumeration that has Miter, Bevel, and Round values. The followingcode snippet sets StartLineCap, EndLineCap, and LineJoin properties to Triangle, Round, and Bevel respectively.
<PenThickness="5"StartLineCap="Triangle"EndLineCap="Round" LineJoin="Bevel"/> 
The DashCap attribute property defines how the ends of each dash are drawn. DashStyle property defines the styleof dash used in a pen. The value of DashStyle is a member of DashStyles class that can be a Dash, DashDot,DashDotDot, Dot, or Solid. MiterLimit propert represents the limit on the ratio of the miter length to half this pen'sThickness.The following code snippet sets DashStyle, DashCap, and MiterLimit properties to DotDot, Triangle, and 0respectively.
<PenThickness="5"StartLineCap="Triangle"EndLineCap="Round" LineJoin="Bevel"DashStyle="DotDot"DashCap="Triangle" MiterLimit="0"> 
The code snippet in Listing 33 sets some of these properties using attributes of the Pen element,
<RectangleWidth="200"Height="200"Stroke="Black"StrokeThickness="0">  <Rectangle.Fill>

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->