Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / Misc / GDI / WindowsRegion.cs / 1 / WindowsRegion.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
#if WINFORMS_NAMESPACE
namespace System.Windows.Forms.Internal
#elif DRAWING_NAMESPACE
namespace System.Drawing.Internal
#else
namespace System.Experimental.Gdi
#endif
{
using System;
using System.Internal;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
///
///
/// Encapsulates a GDI Region object.
///
///
#if WINFORMS_PUBLIC_GRAPHICS_LIBRARY
public
#else
internal
#endif
sealed partial class WindowsRegion : MarshalByRefObject, ICloneable, IDisposable
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
private IntPtr nativeHandle; // The hRegion, this class always takes ownership of the hRegion.
private bool ownHandle;
#if GDI_FINALIZATION_WATCH
private string AllocationSite = DbgUtil.StackTrace;
#endif
///
///
private WindowsRegion() {
}
///
///
public WindowsRegion(Rectangle rect) {
CreateRegion(rect);
}
///
///
public WindowsRegion(int x, int y, int width, int height) {
CreateRegion(new Rectangle(x,y,width, height));
}
// Consider implementing a constructor that calls ExtCreateRegion(XFORM lpXform, DWORD nCount, RGNDATA lpRgnData) if needed.
///
/// Creates a WindowsRegion from a region handle, if 'takeOwnership' is true, the handle is added to the HandleCollector
/// and is removed & destroyed on dispose.
///
public static WindowsRegion FromHregion(IntPtr hRegion, bool takeOwnership) {
WindowsRegion wr = new WindowsRegion();
// Note: Passing IntPtr.Zero for hRegion is ok. GDI+ infinite regions will have hRegion == null.
// GDI's SelectClipRgn interprets null region handle as resetting the clip region (all region will be available for painting).
if( hRegion != IntPtr.Zero ) {
wr.nativeHandle = hRegion;
if (takeOwnership) {
wr.ownHandle = true;
System.Internal.HandleCollector.Add(hRegion, IntSafeNativeMethods.CommonHandles.GDI);
}
}
return wr;
}
///
/// Creates a WindowsRegion from a System.Drawing.Region.
///
public static WindowsRegion FromRegion( Region region, Graphics g ){
if (region.IsInfinite(g)){
// An infinite region would cover the entire device region which is the same as
// not having a clipping region. Observe that this is not the same as having an
// empty region, which when clipping to it has the effect of excluding the entire
// device region.
// To remove the clip region from a dc the SelectClipRgn() function needs to be
// called with a null region ptr - that's why we use the empty constructor here.
// GDI+ will return IntPtr.Zero for Region.GetHrgn(Graphics) when the region is
// Infinite.
return new WindowsRegion();
}
return WindowsRegion.FromHregion(region.GetHrgn(g), true);
}
///
///
public object Clone() {
// WARNING: WindowsRegion currently supports rectangulare regions only, if the WindowsRegion was created
// from an HRegion and it is not rectangular this method won't work as expected.
// Note: This method is currently not used and is here just to implement ICloneable.
return this.IsInfinite ?
new WindowsRegion() :
new WindowsRegion(this.ToRectangle());
}
///
/// Combines region1 & region2 into this region. The regions cannot be null.
/// The three regions need not be distinct. For example, the sourceRgn1 can equal this region.
///
public IntNativeMethods.RegionFlags CombineRegion(WindowsRegion region1, WindowsRegion region2, RegionCombineMode mode) {
return IntUnsafeNativeMethods.CombineRgn(new HandleRef(this, this.HRegion), new HandleRef(region1, region1.HRegion), new HandleRef(region2, region2.HRegion), mode);
}
///
///
private void CreateRegion(Rectangle rect) {
Debug.Assert(nativeHandle == IntPtr.Zero, "nativeHandle should be null, we're leaking handle");
this.nativeHandle = IntSafeNativeMethods.CreateRectRgn(rect.X, rect.Y, rect.X+rect.Width, rect.Y+rect.Height);
ownHandle = true;
}
///
///
public void Dispose() {
Dispose(true);
}
///
///
public void Dispose(bool disposing) {
if (this.nativeHandle != IntPtr.Zero) {
DbgUtil.AssertFinalization(this, disposing);
if( this.ownHandle ) {
IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, this.nativeHandle));
}
this.nativeHandle = IntPtr.Zero;
if (disposing) {
GC.SuppressFinalize(this);
}
}
}
///
///
~WindowsRegion() {
Dispose(false);
}
///
/// The native region handle.
///
public IntPtr HRegion {
get {
return this.nativeHandle;
}
}
///
///
public bool IsInfinite {
get {
return this.nativeHandle == IntPtr.Zero;
}
}
///
/// A rectangle representing the window region set with the SetWindowRgn function.
///
public Rectangle ToRectangle() {
if( this.IsInfinite ) {
return new Rectangle( -Int32.MaxValue, -Int32.MaxValue, Int32.MaxValue, Int32.MaxValue );
}
IntNativeMethods.RECT rect = new IntNativeMethods.RECT();
IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, this.nativeHandle), ref rect);
return new Rectangle(new Point(rect.left, rect.top), rect.Size);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
#if WINFORMS_NAMESPACE
namespace System.Windows.Forms.Internal
#elif DRAWING_NAMESPACE
namespace System.Drawing.Internal
#else
namespace System.Experimental.Gdi
#endif
{
using System;
using System.Internal;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;
using System.Globalization;
///
///
/// Encapsulates a GDI Region object.
///
///
#if WINFORMS_PUBLIC_GRAPHICS_LIBRARY
public
#else
internal
#endif
sealed partial class WindowsRegion : MarshalByRefObject, ICloneable, IDisposable
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources")]
private IntPtr nativeHandle; // The hRegion, this class always takes ownership of the hRegion.
private bool ownHandle;
#if GDI_FINALIZATION_WATCH
private string AllocationSite = DbgUtil.StackTrace;
#endif
///
///
private WindowsRegion() {
}
///
///
public WindowsRegion(Rectangle rect) {
CreateRegion(rect);
}
///
///
public WindowsRegion(int x, int y, int width, int height) {
CreateRegion(new Rectangle(x,y,width, height));
}
// Consider implementing a constructor that calls ExtCreateRegion(XFORM lpXform, DWORD nCount, RGNDATA lpRgnData) if needed.
///
/// Creates a WindowsRegion from a region handle, if 'takeOwnership' is true, the handle is added to the HandleCollector
/// and is removed & destroyed on dispose.
///
public static WindowsRegion FromHregion(IntPtr hRegion, bool takeOwnership) {
WindowsRegion wr = new WindowsRegion();
// Note: Passing IntPtr.Zero for hRegion is ok. GDI+ infinite regions will have hRegion == null.
// GDI's SelectClipRgn interprets null region handle as resetting the clip region (all region will be available for painting).
if( hRegion != IntPtr.Zero ) {
wr.nativeHandle = hRegion;
if (takeOwnership) {
wr.ownHandle = true;
System.Internal.HandleCollector.Add(hRegion, IntSafeNativeMethods.CommonHandles.GDI);
}
}
return wr;
}
///
/// Creates a WindowsRegion from a System.Drawing.Region.
///
public static WindowsRegion FromRegion( Region region, Graphics g ){
if (region.IsInfinite(g)){
// An infinite region would cover the entire device region which is the same as
// not having a clipping region. Observe that this is not the same as having an
// empty region, which when clipping to it has the effect of excluding the entire
// device region.
// To remove the clip region from a dc the SelectClipRgn() function needs to be
// called with a null region ptr - that's why we use the empty constructor here.
// GDI+ will return IntPtr.Zero for Region.GetHrgn(Graphics) when the region is
// Infinite.
return new WindowsRegion();
}
return WindowsRegion.FromHregion(region.GetHrgn(g), true);
}
///
///
public object Clone() {
// WARNING: WindowsRegion currently supports rectangulare regions only, if the WindowsRegion was created
// from an HRegion and it is not rectangular this method won't work as expected.
// Note: This method is currently not used and is here just to implement ICloneable.
return this.IsInfinite ?
new WindowsRegion() :
new WindowsRegion(this.ToRectangle());
}
///
/// Combines region1 & region2 into this region. The regions cannot be null.
/// The three regions need not be distinct. For example, the sourceRgn1 can equal this region.
///
public IntNativeMethods.RegionFlags CombineRegion(WindowsRegion region1, WindowsRegion region2, RegionCombineMode mode) {
return IntUnsafeNativeMethods.CombineRgn(new HandleRef(this, this.HRegion), new HandleRef(region1, region1.HRegion), new HandleRef(region2, region2.HRegion), mode);
}
///
///
private void CreateRegion(Rectangle rect) {
Debug.Assert(nativeHandle == IntPtr.Zero, "nativeHandle should be null, we're leaking handle");
this.nativeHandle = IntSafeNativeMethods.CreateRectRgn(rect.X, rect.Y, rect.X+rect.Width, rect.Y+rect.Height);
ownHandle = true;
}
///
///
public void Dispose() {
Dispose(true);
}
///
///
public void Dispose(bool disposing) {
if (this.nativeHandle != IntPtr.Zero) {
DbgUtil.AssertFinalization(this, disposing);
if( this.ownHandle ) {
IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, this.nativeHandle));
}
this.nativeHandle = IntPtr.Zero;
if (disposing) {
GC.SuppressFinalize(this);
}
}
}
///
///
~WindowsRegion() {
Dispose(false);
}
///
/// The native region handle.
///
public IntPtr HRegion {
get {
return this.nativeHandle;
}
}
///
///
public bool IsInfinite {
get {
return this.nativeHandle == IntPtr.Zero;
}
}
///
/// A rectangle representing the window region set with the SetWindowRgn function.
///
public Rectangle ToRectangle() {
if( this.IsInfinite ) {
return new Rectangle( -Int32.MaxValue, -Int32.MaxValue, Int32.MaxValue, Int32.MaxValue );
}
IntNativeMethods.RECT rect = new IntNativeMethods.RECT();
IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, this.nativeHandle), ref rect);
return new Rectangle(new Point(rect.left, rect.top), rect.Size);
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TargetParameterCountException.cs
- SQLInt32.cs
- EncoderParameter.cs
- Privilege.cs
- coordinator.cs
- XmlSerializerAssemblyAttribute.cs
- CheckoutException.cs
- IPPacketInformation.cs
- HttpStreamXmlDictionaryReader.cs
- UnionExpr.cs
- PrintDialog.cs
- CommandDesigner.cs
- GridViewSortEventArgs.cs
- UnsafeNativeMethods.cs
- Scene3D.cs
- SessionEndedEventArgs.cs
- RTLAwareMessageBox.cs
- CustomErrorsSection.cs
- ProfileGroupSettingsCollection.cs
- WindowsListViewItem.cs
- TextComposition.cs
- SiteMapPath.cs
- SerialErrors.cs
- loginstatus.cs
- AspCompat.cs
- TaskbarItemInfo.cs
- MatrixAnimationUsingKeyFrames.cs
- DependencyObjectPropertyDescriptor.cs
- _ContextAwareResult.cs
- SafeCryptContextHandle.cs
- Permission.cs
- SmtpMail.cs
- AnonymousIdentificationModule.cs
- ObjectIDGenerator.cs
- MappingMetadataHelper.cs
- SparseMemoryStream.cs
- LockedHandleGlyph.cs
- TypeExtensionConverter.cs
- DataGridItem.cs
- ToolboxComponentsCreatingEventArgs.cs
- DateTime.cs
- CommandCollectionEditor.cs
- Variable.cs
- DecimalStorage.cs
- LogicalChannelCollection.cs
- CellTreeNodeVisitors.cs
- ExceptionHandlerDesigner.cs
- WorkflowView.cs
- TemplateContentLoader.cs
- ComboBoxAutomationPeer.cs
- SoapConverter.cs
- ScheduleChanges.cs
- DataGridRelationshipRow.cs
- SimpleTypesSurrogate.cs
- BroadcastEventHelper.cs
- ImmComposition.cs
- PolyBezierSegment.cs
- CultureInfoConverter.cs
- RSAOAEPKeyExchangeFormatter.cs
- TextRenderer.cs
- SqlConnection.cs
- MemoryStream.cs
- ConfigurationStrings.cs
- PropertyGridEditorPart.cs
- MultipleViewProviderWrapper.cs
- SafePointer.cs
- Facet.cs
- NameValueSectionHandler.cs
- ValueUnavailableException.cs
- PeerResolverElement.cs
- InputBinding.cs
- CodeMethodInvokeExpression.cs
- PhysicalFontFamily.cs
- ButtonChrome.cs
- SqlServer2KCompatibilityCheck.cs
- EntitySqlQueryBuilder.cs
- GridItem.cs
- SettingsAttributeDictionary.cs
- RayMeshGeometry3DHitTestResult.cs
- ShaderEffect.cs
- AxHost.cs
- BindingMAnagerBase.cs
- CookielessHelper.cs
- RuntimeHandles.cs
- RSATokenProvider.cs
- ObjectListItemCollection.cs
- XmlAttributes.cs
- PrintPreviewControl.cs
- HostnameComparisonMode.cs
- XmlSchemaSimpleType.cs
- CursorConverter.cs
- _AcceptOverlappedAsyncResult.cs
- WebPartActionVerb.cs
- WSSecurityOneDotZeroReceiveSecurityHeader.cs
- ManagementPath.cs
- Clause.cs
- EditorPart.cs
- CryptoStream.cs
- ILGenerator.cs
- VectorCollection.cs