Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / Shapes / Rectangle.cs / 1 / Rectangle.cs
//----------------------------------------------------------------------------
// File: Rectangle.cs
//
// Description:
// Implementation of Rectangle shape element.
//
// History:
// 05/30/02 - AdSmith - Created.
//
// Copyright (C) 2003 by Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Media;
using MS.Internal;
using System.ComponentModel;
using System;
namespace System.Windows.Shapes
{
///
/// The rectangle shape element
/// This element (like all shapes) belongs under a Canvas,
/// and will be presented by the parent canvas.
///
///
public sealed class Rectangle : Shape
{
#region Constructors
///
/// Instantiates a new instance of a Rectangle with no parent element.
///
///
public Rectangle()
{
}
// The default stretch mode of Rectangle is Fill
static Rectangle()
{
StretchProperty.OverrideMetadata(typeof(Rectangle), new FrameworkPropertyMetadata(Stretch.Fill));
}
#endregion Constructors
#region Dynamic Properties
///
/// RadiusX Dynamic Property - if set, this rectangle becomes rounded
///
///
public static readonly DependencyProperty RadiusXProperty =
DependencyProperty.Register( "RadiusX", typeof(double), typeof(Rectangle),
new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// Provide public access to RadiusX property.
///
///
///
[TypeConverter(typeof(LengthConverter))]
public double RadiusX
{
get
{
return (double)GetValue(RadiusXProperty);
}
set
{
SetValue(RadiusXProperty, value);
}
}
///
/// RadiusY Dynamic Property - if set, this rectangle becomes rounded
///
///
public static readonly DependencyProperty RadiusYProperty =
DependencyProperty.Register( "RadiusY", typeof(double), typeof(Rectangle),
new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// Provide public access to RadiusY property.
///
///
///
[TypeConverter(typeof(LengthConverter))]
public double RadiusY
{
get
{
return (double)GetValue(RadiusYProperty);
}
set
{
SetValue(RadiusYProperty, value);
}
}
// For a Rectangle, RenderedGeometry = defining geometry and GeometryTransform = Identity
///
/// The RenderedGeometry property returns the final rendered geometry
///
public override Geometry RenderedGeometry
{
get
{
// RenderedGeometry = defining geometry
return new RectangleGeometry(_rect, RadiusX, RadiusY);
}
}
///
/// Return the transformation applied to the geometry before rendering
///
public override Transform GeometryTransform
{
get
{
return Transform.Identity;
}
}
#endregion Dynamic Properties
#region Protected
///
/// Updates DesiredSize of the Rectangle. Called by parent UIElement. This is the first pass of layout.
///
/// Constraint size is an "upper limit" that Rectangle should not exceed.
/// Rectangle's desired size.
protected override Size MeasureOverride(Size constraint)
{
if (Stretch == Stretch.UniformToFill)
{
double width = constraint.Width;
double height = constraint.Height;
if (Double.IsInfinity(width) && Double.IsInfinity(height))
{
return GetNaturalSize();
}
else if (Double.IsInfinity(width) || Double.IsInfinity(height))
{
width = Math.Min(width, height);
}
else
{
width = Math.Max(width, height);
}
return new Size(width, width);
}
return GetNaturalSize();
}
///
/// Returns the final size of the shape and cachnes the bounds.
///
protected override Size ArrangeOverride(Size finalSize)
{
// Since we do NOT want the RadiusX and RadiusY to change with the rendering transformation, we
// construct the rectangle to fit finalSize with the appropriate Stretch mode. The rendering
// transformation will thus be the identity.
double penThickness = GetStrokeThickness();
double margin = penThickness / 2;
_rect = new Rect(
margin, // X
margin, // Y
Math.Max(0, finalSize.Width - penThickness), // Width
Math.Max(0, finalSize.Height - penThickness)); // Height
switch (Stretch)
{
case Stretch.None:
// A 0 Rect.Width and Rect.Height rectangle
_rect.Width = _rect.Height = 0;
break;
case Stretch.Fill:
// The most common case: a rectangle that fills the box.
// _rect has already been initialized for that.
break;
case Stretch.Uniform:
// The maximal square that fits in the final box
if (_rect.Width > _rect.Height)
{
_rect.Width = _rect.Height;
}
else // _rect.Width <= _rect.Height
{
_rect.Height = _rect.Width;
}
break;
case Stretch.UniformToFill:
// The minimal square that fills the final box
if (_rect.Width < _rect.Height)
{
_rect.Width = _rect.Height;
}
else // _rect.Width >= _rect.Height
{
_rect.Height = _rect.Width;
}
break;
}
ResetRenderedGeometry();
return finalSize;
}
///
/// Get the rectangle that defines this shape
///
protected override Geometry DefiningGeometry
{
get
{
return new RectangleGeometry(_rect, RadiusX, RadiusY);
}
}
///
/// Render callback.
///
protected override void OnRender(DrawingContext drawingContext)
{
Pen pen = GetPen();
drawingContext.DrawRoundedRectangle(Fill, pen, _rect, RadiusX, RadiusY);
}
#endregion Protected
#region Internal Methods
internal override void CacheDefiningGeometry()
{
double margin = GetStrokeThickness() / 2;
_rect = new Rect(margin, margin, 0, 0);
}
///
/// Get the natural size of the geometry that defines this shape
///
internal override Size GetNaturalSize()
{
double strokeThickness = GetStrokeThickness();
return new Size(strokeThickness, strokeThickness);
}
///
/// Get the bonds of the rectangle that defines this shape
///
internal override Rect GetDefiningGeometryBounds()
{
return _rect;
}
//
// This property
// 1. Finds the correct initial size for the _effectiveValues store on the current DependencyObject
// 2. This is a performance optimization
//
internal override int EffectiveValuesInitialSize
{
get { return 19; }
}
#endregion Internal Methods
#region Private Fields
private Rect _rect = Rect.Empty;
#endregion Private Fields
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//----------------------------------------------------------------------------
// File: Rectangle.cs
//
// Description:
// Implementation of Rectangle shape element.
//
// History:
// 05/30/02 - AdSmith - Created.
//
// Copyright (C) 2003 by Microsoft Corporation. All rights reserved.
//
//---------------------------------------------------------------------------
using System.Windows.Shapes;
using System.Diagnostics;
using System.Windows.Threading;
using System.Windows;
using System.Windows.Media;
using MS.Internal;
using System.ComponentModel;
using System;
namespace System.Windows.Shapes
{
///
/// The rectangle shape element
/// This element (like all shapes) belongs under a Canvas,
/// and will be presented by the parent canvas.
///
///
public sealed class Rectangle : Shape
{
#region Constructors
///
/// Instantiates a new instance of a Rectangle with no parent element.
///
///
public Rectangle()
{
}
// The default stretch mode of Rectangle is Fill
static Rectangle()
{
StretchProperty.OverrideMetadata(typeof(Rectangle), new FrameworkPropertyMetadata(Stretch.Fill));
}
#endregion Constructors
#region Dynamic Properties
///
/// RadiusX Dynamic Property - if set, this rectangle becomes rounded
///
///
public static readonly DependencyProperty RadiusXProperty =
DependencyProperty.Register( "RadiusX", typeof(double), typeof(Rectangle),
new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// Provide public access to RadiusX property.
///
///
///
[TypeConverter(typeof(LengthConverter))]
public double RadiusX
{
get
{
return (double)GetValue(RadiusXProperty);
}
set
{
SetValue(RadiusXProperty, value);
}
}
///
/// RadiusY Dynamic Property - if set, this rectangle becomes rounded
///
///
public static readonly DependencyProperty RadiusYProperty =
DependencyProperty.Register( "RadiusY", typeof(double), typeof(Rectangle),
new FrameworkPropertyMetadata(0d, FrameworkPropertyMetadataOptions.AffectsRender));
///
/// Provide public access to RadiusY property.
///
///
///
[TypeConverter(typeof(LengthConverter))]
public double RadiusY
{
get
{
return (double)GetValue(RadiusYProperty);
}
set
{
SetValue(RadiusYProperty, value);
}
}
// For a Rectangle, RenderedGeometry = defining geometry and GeometryTransform = Identity
///
/// The RenderedGeometry property returns the final rendered geometry
///
public override Geometry RenderedGeometry
{
get
{
// RenderedGeometry = defining geometry
return new RectangleGeometry(_rect, RadiusX, RadiusY);
}
}
///
/// Return the transformation applied to the geometry before rendering
///
public override Transform GeometryTransform
{
get
{
return Transform.Identity;
}
}
#endregion Dynamic Properties
#region Protected
///
/// Updates DesiredSize of the Rectangle. Called by parent UIElement. This is the first pass of layout.
///
/// Constraint size is an "upper limit" that Rectangle should not exceed.
/// Rectangle's desired size.
protected override Size MeasureOverride(Size constraint)
{
if (Stretch == Stretch.UniformToFill)
{
double width = constraint.Width;
double height = constraint.Height;
if (Double.IsInfinity(width) && Double.IsInfinity(height))
{
return GetNaturalSize();
}
else if (Double.IsInfinity(width) || Double.IsInfinity(height))
{
width = Math.Min(width, height);
}
else
{
width = Math.Max(width, height);
}
return new Size(width, width);
}
return GetNaturalSize();
}
///
/// Returns the final size of the shape and cachnes the bounds.
///
protected override Size ArrangeOverride(Size finalSize)
{
// Since we do NOT want the RadiusX and RadiusY to change with the rendering transformation, we
// construct the rectangle to fit finalSize with the appropriate Stretch mode. The rendering
// transformation will thus be the identity.
double penThickness = GetStrokeThickness();
double margin = penThickness / 2;
_rect = new Rect(
margin, // X
margin, // Y
Math.Max(0, finalSize.Width - penThickness), // Width
Math.Max(0, finalSize.Height - penThickness)); // Height
switch (Stretch)
{
case Stretch.None:
// A 0 Rect.Width and Rect.Height rectangle
_rect.Width = _rect.Height = 0;
break;
case Stretch.Fill:
// The most common case: a rectangle that fills the box.
// _rect has already been initialized for that.
break;
case Stretch.Uniform:
// The maximal square that fits in the final box
if (_rect.Width > _rect.Height)
{
_rect.Width = _rect.Height;
}
else // _rect.Width <= _rect.Height
{
_rect.Height = _rect.Width;
}
break;
case Stretch.UniformToFill:
// The minimal square that fills the final box
if (_rect.Width < _rect.Height)
{
_rect.Width = _rect.Height;
}
else // _rect.Width >= _rect.Height
{
_rect.Height = _rect.Width;
}
break;
}
ResetRenderedGeometry();
return finalSize;
}
///
/// Get the rectangle that defines this shape
///
protected override Geometry DefiningGeometry
{
get
{
return new RectangleGeometry(_rect, RadiusX, RadiusY);
}
}
///
/// Render callback.
///
protected override void OnRender(DrawingContext drawingContext)
{
Pen pen = GetPen();
drawingContext.DrawRoundedRectangle(Fill, pen, _rect, RadiusX, RadiusY);
}
#endregion Protected
#region Internal Methods
internal override void CacheDefiningGeometry()
{
double margin = GetStrokeThickness() / 2;
_rect = new Rect(margin, margin, 0, 0);
}
///
/// Get the natural size of the geometry that defines this shape
///
internal override Size GetNaturalSize()
{
double strokeThickness = GetStrokeThickness();
return new Size(strokeThickness, strokeThickness);
}
///
/// Get the bonds of the rectangle that defines this shape
///
internal override Rect GetDefiningGeometryBounds()
{
return _rect;
}
//
// This property
// 1. Finds the correct initial size for the _effectiveValues store on the current DependencyObject
// 2. This is a performance optimization
//
internal override int EffectiveValuesInitialSize
{
get { return 19; }
}
#endregion Internal Methods
#region Private Fields
private Rect _rect = Rect.Empty;
#endregion Private Fields
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- SHA256.cs
- LocalizedNameDescriptionPair.cs
- StorageMappingFragment.cs
- Point3DAnimation.cs
- PropertyBuilder.cs
- Mutex.cs
- FixedHighlight.cs
- StateWorkerRequest.cs
- login.cs
- BitmapEffectGeneralTransform.cs
- EditorZone.cs
- WebPartZone.cs
- _TLSstream.cs
- LogLogRecordHeader.cs
- TreeViewImageKeyConverter.cs
- DoWhileDesigner.xaml.cs
- KeyConverter.cs
- CharacterMetrics.cs
- UInt64Converter.cs
- FixedTextContainer.cs
- HttpCookiesSection.cs
- LogFlushAsyncResult.cs
- HMACSHA256.cs
- DataGridViewCellValidatingEventArgs.cs
- Rectangle.cs
- SimpleWorkerRequest.cs
- Group.cs
- UnicodeEncoding.cs
- FloaterBaseParaClient.cs
- ScaleTransform.cs
- XmlLinkedNode.cs
- BreakRecordTable.cs
- DateTimeStorage.cs
- SecureEnvironment.cs
- XmlReflectionMember.cs
- ReversePositionQuery.cs
- BooleanExpr.cs
- InvalidBodyAccessException.cs
- TranslateTransform.cs
- StaticResourceExtension.cs
- PolyQuadraticBezierSegment.cs
- Int16KeyFrameCollection.cs
- Crc32.cs
- NavigateEvent.cs
- CapabilitiesState.cs
- SecurityTokenValidationException.cs
- ColorTransformHelper.cs
- ComplexType.cs
- Model3D.cs
- CustomErrorCollection.cs
- RadioButton.cs
- InternalSafeNativeMethods.cs
- SupportsEventValidationAttribute.cs
- ContentControl.cs
- CompilerGeneratedAttribute.cs
- DiscreteKeyFrames.cs
- InitializationEventAttribute.cs
- TemplatedWizardStep.cs
- FocusManager.cs
- AppSettingsSection.cs
- SystemIPGlobalProperties.cs
- MiniModule.cs
- TokenizerHelper.cs
- Constants.cs
- ToolStripDropTargetManager.cs
- ClientEventManager.cs
- ConnectionStringsSection.cs
- SystemResourceKey.cs
- TripleDES.cs
- SystemFonts.cs
- BitConverter.cs
- RuleElement.cs
- LoginName.cs
- PropertyDescriptorComparer.cs
- sapiproxy.cs
- HttpEncoderUtility.cs
- safelink.cs
- UnsafeNativeMethods.cs
- TextEvent.cs
- mediaeventargs.cs
- ProxyGenerator.cs
- XmlUtil.cs
- ObjectSpanRewriter.cs
- TimeSpanOrInfiniteConverter.cs
- bindurihelper.cs
- WebPartTransformer.cs
- SafeNativeMethods.cs
- ComponentCodeDomSerializer.cs
- InstallerTypeAttribute.cs
- SqlError.cs
- ObjectKeyFrameCollection.cs
- TreeNodeStyleCollection.cs
- CalendarBlackoutDatesCollection.cs
- BaseDataBoundControl.cs
- _NetRes.cs
- ResumeStoryboard.cs
- NativeMethods.cs
- ReflectTypeDescriptionProvider.cs
- TimeSpanSecondsOrInfiniteConverter.cs
- EnglishPluralizationService.cs