Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / Misc / GDI / WindowsBrush.cs / 1 / WindowsBrush.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope="member", Target="System.Windows.Forms.Internal.WindowsBrush.FromLogBrush(System.Windows.Forms.Internal.IntNativeMethods+LOGBRUSH):System.Windows.Forms.Internal.WindowsBrush")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope="member", Target="System.Windows.Forms.Internal.WindowsBrush.FromHdc(System.IntPtr):System.Windows.Forms.Internal.WindowsBrush")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope="member", Target="System.Windows.Forms.Internal.WindowsBrush.FromBrush(System.Drawing.Brush):System.Windows.Forms.Internal.WindowsBrush")]
#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.Drawing;
///
///
/// Encapsulates a GDI Brush object.
///
///
#if WINFORMS_PUBLIC_GRAPHICS_LIBRARY
public
#else
internal
#endif
abstract class WindowsBrush : MarshalByRefObject, ICloneable, IDisposable
{
// Handle to the native Windows brush object.
//
private DeviceContext dc;
private IntPtr nativeHandle; // Cannot be protected because the class is internal (C# doesn't allow it).
private Color color = Color.White; // GDI brushes have just one color as opposed to GDI+ that can have background color.
// Note: We may need to implement background color too.
#if WINGRAPHICS_FINALIZATION_WATCH
private string AllocationSite = DbgUtil.StackTrace;
#endif
public abstract object Clone(); // Declaration required by C# even though this is an abstract class.
protected abstract void CreateBrush();
///
/// Parameterless constructor to use default color.
/// Notice that the actual object construction is done in the derived classes.
///
public WindowsBrush(DeviceContext dc)
{
this.dc = dc;
}
public WindowsBrush(DeviceContext dc, Color color)
{
this.dc = dc;
this.color = color;
}
~WindowsBrush()
{
Dispose(false);
}
protected DeviceContext DC {
get {
return this.dc;
}
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (dc != null && this.nativeHandle != IntPtr.Zero)
{
DbgUtil.AssertFinalization(this, disposing);
dc.DeleteObject(this.nativeHandle, GdiObjectType.Brush);
this.nativeHandle = IntPtr.Zero;
}
if (disposing)
{
GC.SuppressFinalize(this);
}
}
public Color Color
{
get
{
return this.color;
}
}
///
/// Gets the native Win32 brush handle. It creates it on demand.
///
protected IntPtr NativeHandle
{
get
{
if( this.nativeHandle == IntPtr.Zero )
{
CreateBrush();
}
return this.nativeHandle;
}
set
{
Debug.Assert(this.nativeHandle == IntPtr.Zero, "WindowsBrush object is immutable");
Debug.Assert(value != IntPtr.Zero, "WARNING: assigning IntPtr.Zero to the nativeHandle object.");
this.nativeHandle = value;
}
}
#if WINFORMS_PUBLIC_GRAPHICS_LIBRARY
///
/// Derived classes implement this method to get a native GDI brush wrapper with the same
/// properties as this object.
///
public static WindowsBrush FromBrush(DeviceContext dc, Brush originalBrush)
{
if(originalBrush is SolidBrush) {
return new WindowsSolidBrush(dc, ((SolidBrush)originalBrush).Color);
}
if(originalBrush is System.Drawing.Drawing2D.HatchBrush) {
System.Drawing.Drawing2D.HatchBrush hatchBrush = ((System.Drawing.Drawing2D.HatchBrush)originalBrush);
return new WindowsHatchBrush(dc, (WindowsHatchStyle) hatchBrush.HatchStyle, hatchBrush.ForegroundColor, hatchBrush.BackgroundColor);
}
/*
*/
Debug.Fail("Don't know how to convert this brush!");
return null;
}
///
/// Creates a WindowsBrush from the DC currently selected HBRUSH
///
public static WindowsBrush FromDC(DeviceContext dc)
{
IntPtr hBrush = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(null, dc.Hdc), IntNativeMethods.OBJ_BRUSH);
IntNativeMethods.LOGBRUSH logBrush = new IntNativeMethods.LOGBRUSH();
IntUnsafeNativeMethods.GetObject(new HandleRef(null, hBrush), logBrush);
// don't call DeleteObject on handle from GetCurrentObject, it is the one selected in the hdc.
return WindowsBrush.FromLogBrush(dc, logBrush );
}
///
/// Creates a WindowsBrush from a LOGBRUSH.
///
public static WindowsBrush FromLogBrush( DeviceContext dc, IntNativeMethods.LOGBRUSH logBrush )
{
Debug.Assert( logBrush != null, "logBrush is null" );
switch( logBrush.lbStyle )
{
// currently supported brushes:
case IntNativeMethods.BS_HATCHED:
return new WindowsHatchBrush(dc, (WindowsHatchStyle) logBrush.lbHatch );
case IntNativeMethods.BS_SOLID:
return new WindowsSolidBrush( dc, Color.FromArgb(logBrush.lbColor) );
default:
Debug.Fail( "Don't know how to create WindowsBrush from specified logBrush" );
return null;
}
}
#endif
///
///
/// Returns the native Win32 brush handle.
///
///
public IntPtr HBrush
{
get
{
return this.NativeHandle;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope="member", Target="System.Windows.Forms.Internal.WindowsBrush.FromLogBrush(System.Windows.Forms.Internal.IntNativeMethods+LOGBRUSH):System.Windows.Forms.Internal.WindowsBrush")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope="member", Target="System.Windows.Forms.Internal.WindowsBrush.FromHdc(System.IntPtr):System.Windows.Forms.Internal.WindowsBrush")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Scope="member", Target="System.Windows.Forms.Internal.WindowsBrush.FromBrush(System.Drawing.Brush):System.Windows.Forms.Internal.WindowsBrush")]
#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.Drawing;
///
///
/// Encapsulates a GDI Brush object.
///
///
#if WINFORMS_PUBLIC_GRAPHICS_LIBRARY
public
#else
internal
#endif
abstract class WindowsBrush : MarshalByRefObject, ICloneable, IDisposable
{
// Handle to the native Windows brush object.
//
private DeviceContext dc;
private IntPtr nativeHandle; // Cannot be protected because the class is internal (C# doesn't allow it).
private Color color = Color.White; // GDI brushes have just one color as opposed to GDI+ that can have background color.
// Note: We may need to implement background color too.
#if WINGRAPHICS_FINALIZATION_WATCH
private string AllocationSite = DbgUtil.StackTrace;
#endif
public abstract object Clone(); // Declaration required by C# even though this is an abstract class.
protected abstract void CreateBrush();
///
/// Parameterless constructor to use default color.
/// Notice that the actual object construction is done in the derived classes.
///
public WindowsBrush(DeviceContext dc)
{
this.dc = dc;
}
public WindowsBrush(DeviceContext dc, Color color)
{
this.dc = dc;
this.color = color;
}
~WindowsBrush()
{
Dispose(false);
}
protected DeviceContext DC {
get {
return this.dc;
}
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (dc != null && this.nativeHandle != IntPtr.Zero)
{
DbgUtil.AssertFinalization(this, disposing);
dc.DeleteObject(this.nativeHandle, GdiObjectType.Brush);
this.nativeHandle = IntPtr.Zero;
}
if (disposing)
{
GC.SuppressFinalize(this);
}
}
public Color Color
{
get
{
return this.color;
}
}
///
/// Gets the native Win32 brush handle. It creates it on demand.
///
protected IntPtr NativeHandle
{
get
{
if( this.nativeHandle == IntPtr.Zero )
{
CreateBrush();
}
return this.nativeHandle;
}
set
{
Debug.Assert(this.nativeHandle == IntPtr.Zero, "WindowsBrush object is immutable");
Debug.Assert(value != IntPtr.Zero, "WARNING: assigning IntPtr.Zero to the nativeHandle object.");
this.nativeHandle = value;
}
}
#if WINFORMS_PUBLIC_GRAPHICS_LIBRARY
///
/// Derived classes implement this method to get a native GDI brush wrapper with the same
/// properties as this object.
///
public static WindowsBrush FromBrush(DeviceContext dc, Brush originalBrush)
{
if(originalBrush is SolidBrush) {
return new WindowsSolidBrush(dc, ((SolidBrush)originalBrush).Color);
}
if(originalBrush is System.Drawing.Drawing2D.HatchBrush) {
System.Drawing.Drawing2D.HatchBrush hatchBrush = ((System.Drawing.Drawing2D.HatchBrush)originalBrush);
return new WindowsHatchBrush(dc, (WindowsHatchStyle) hatchBrush.HatchStyle, hatchBrush.ForegroundColor, hatchBrush.BackgroundColor);
}
/*
*/
Debug.Fail("Don't know how to convert this brush!");
return null;
}
///
/// Creates a WindowsBrush from the DC currently selected HBRUSH
///
public static WindowsBrush FromDC(DeviceContext dc)
{
IntPtr hBrush = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(null, dc.Hdc), IntNativeMethods.OBJ_BRUSH);
IntNativeMethods.LOGBRUSH logBrush = new IntNativeMethods.LOGBRUSH();
IntUnsafeNativeMethods.GetObject(new HandleRef(null, hBrush), logBrush);
// don't call DeleteObject on handle from GetCurrentObject, it is the one selected in the hdc.
return WindowsBrush.FromLogBrush(dc, logBrush );
}
///
/// Creates a WindowsBrush from a LOGBRUSH.
///
public static WindowsBrush FromLogBrush( DeviceContext dc, IntNativeMethods.LOGBRUSH logBrush )
{
Debug.Assert( logBrush != null, "logBrush is null" );
switch( logBrush.lbStyle )
{
// currently supported brushes:
case IntNativeMethods.BS_HATCHED:
return new WindowsHatchBrush(dc, (WindowsHatchStyle) logBrush.lbHatch );
case IntNativeMethods.BS_SOLID:
return new WindowsSolidBrush( dc, Color.FromArgb(logBrush.lbColor) );
default:
Debug.Fail( "Don't know how to create WindowsBrush from specified logBrush" );
return null;
}
}
#endif
///
///
/// Returns the native Win32 brush handle.
///
///
public IntPtr HBrush
{
get
{
return this.NativeHandle;
}
}
}
}
// 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
- MethodBuilder.cs
- TextProperties.cs
- WebControlsSection.cs
- SafeViewOfFileHandle.cs
- NamespaceEmitter.cs
- TextTrailingCharacterEllipsis.cs
- errorpatternmatcher.cs
- InfoCardMetadataExchangeClient.cs
- ProviderConnectionPointCollection.cs
- DisplayNameAttribute.cs
- Geometry.cs
- ChannelServices.cs
- GridViewCommandEventArgs.cs
- ReflectionUtil.cs
- HostSecurityManager.cs
- PropertyGeneratedEventArgs.cs
- PageWrapper.cs
- AssemblyInfo.cs
- TextSimpleMarkerProperties.cs
- UserControlCodeDomTreeGenerator.cs
- XmlSubtreeReader.cs
- EnumerableRowCollection.cs
- TypeSource.cs
- WebPartMenuStyle.cs
- CalendarTable.cs
- PluggableProtocol.cs
- bindurihelper.cs
- XmlSchemaComplexContent.cs
- XmlSchemaCompilationSettings.cs
- AsymmetricSignatureDeformatter.cs
- SliderAutomationPeer.cs
- Single.cs
- DataServiceProviderMethods.cs
- TextCharacters.cs
- PrincipalPermissionMode.cs
- PaintEvent.cs
- BuildResult.cs
- Misc.cs
- WebPartMenu.cs
- CollectionViewGroupRoot.cs
- BuiltInPermissionSets.cs
- WindowsTab.cs
- MouseActionValueSerializer.cs
- SiteMapNodeItemEventArgs.cs
- SqlDependency.cs
- Pair.cs
- GroupedContextMenuStrip.cs
- _NestedSingleAsyncResult.cs
- FileUpload.cs
- HasCopySemanticsAttribute.cs
- TextElementAutomationPeer.cs
- TableCellCollection.cs
- XmlQuerySequence.cs
- SafeNativeMethods.cs
- NodeLabelEditEvent.cs
- Storyboard.cs
- SettingsSavedEventArgs.cs
- SafeTimerHandle.cs
- PropertyGridEditorPart.cs
- PrimitiveXmlSerializers.cs
- DesignerImageAdapter.cs
- TextSpanModifier.cs
- XhtmlTextWriter.cs
- DataGridViewEditingControlShowingEventArgs.cs
- CompensationParticipant.cs
- StateMachineSubscriptionManager.cs
- OdbcParameterCollection.cs
- GenericPrincipal.cs
- GridViewRowPresenterBase.cs
- LinqDataSourceEditData.cs
- RuntimeCompatibilityAttribute.cs
- IImplicitResourceProvider.cs
- EmptyStringExpandableObjectConverter.cs
- EditorPartCollection.cs
- ClientSettings.cs
- CatalogPartDesigner.cs
- XsltQilFactory.cs
- TransformGroup.cs
- CompilerInfo.cs
- TextElementEnumerator.cs
- XhtmlConformanceSection.cs
- CacheEntry.cs
- HtmlProps.cs
- HostProtectionPermission.cs
- SByteStorage.cs
- TextComposition.cs
- SelectionListDesigner.cs
- TextDocumentView.cs
- BamlBinaryReader.cs
- BaseHashHelper.cs
- FlowDecisionLabelFeature.cs
- TableItemPatternIdentifiers.cs
- CfgParser.cs
- TokenBasedSet.cs
- SchemaCollectionCompiler.cs
- UserValidatedEventArgs.cs
- SafeWaitHandle.cs
- DataGridTablesFactory.cs
- AnnotationComponentManager.cs
- ParentQuery.cs