Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / WinForms / Managed / System / WinForms / ColorDialog.cs / 1305376 / ColorDialog.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.Windows.Forms {
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security;
using System.Security.Permissions;
///
///
///
/// Represents a common dialog box that displays available colors along with
/// controls that allow the user to define custom colors.
///
///
[DefaultProperty("Color")]
[SRDescription(SR.DescriptionColorDialog)]
// The only event this dialog has is HelpRequest, which isn't very useful
public class ColorDialog : CommonDialog {
private int options;
private int[] customColors;
///
///
///
///
private Color color;
///
///
///
/// Initializes a new instance of the
/// class.
///
///
[
SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors") // If the constructor does not call Reset
// it would be a breaking change.
]
public ColorDialog() {
customColors = new int[16];
Reset();
}
///
///
///
/// Gets or sets a value indicating whether the user can use the dialog box
/// to define custom colors.
///
///
[
SRCategory(SR.CatBehavior),
DefaultValue(true),
SRDescription(SR.CDallowFullOpenDescr)
]
public virtual bool AllowFullOpen {
get {
return !GetOption(NativeMethods.CC_PREVENTFULLOPEN);
}
set {
SetOption(NativeMethods.CC_PREVENTFULLOPEN, !value);
}
}
///
///
///
/// Gets or sets a value indicating whether the dialog box displays all available colors in
/// the set of basic colors.
///
///
[
SRCategory(SR.CatBehavior),
DefaultValue(false),
SRDescription(SR.CDanyColorDescr)
]
public virtual bool AnyColor {
get {
return GetOption(NativeMethods.CC_ANYCOLOR);
}
set {
SetOption(NativeMethods.CC_ANYCOLOR, value);
}
}
///
///
///
/// Gets or sets the color selected by the user.
///
///
[
SRCategory(SR.CatData),
SRDescription(SR.CDcolorDescr)
]
public Color Color {
get {
return color;
}
set {
if (!value.IsEmpty) {
color = value;
}
else {
color = Color.Black;
}
}
}
///
///
///
/// Gets or sets the set of
/// custom colors shown in the dialog box.
///
///
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
SRDescription(SR.CDcustomColorsDescr)
]
public int[] CustomColors {
get { return(int[]) customColors.Clone();}
set {
int length = value == null? 0: Math.Min(value.Length, 16);
if (length > 0) Array.Copy(value, 0, customColors, 0, length);
for (int i = length; i < 16; i++) customColors[i] = 0x00FFFFFF;
}
}
///
///
///
/// Gets or sets a value indicating whether the controls used to create custom
/// colors are visible when the dialog box is opened
///
///
[
SRCategory(SR.CatAppearance),
DefaultValue(false),
SRDescription(SR.CDfullOpenDescr)
]
public virtual bool FullOpen {
get {
return GetOption(NativeMethods.CC_FULLOPEN);
}
set {
SetOption(NativeMethods.CC_FULLOPEN, value);
}
}
///
///
///
///
/// Our HINSTANCE from Windows.
///
///
protected virtual IntPtr Instance {
[
SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode),
SecurityPermission(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)
]
[ResourceExposure(ResourceScope.Process)]
[ResourceConsumption(ResourceScope.Process)]
get { return UnsafeNativeMethods.GetModuleHandle(null);}
}
///
///
/// Returns our CHOOSECOLOR options.
///
///
protected virtual int Options {
get {
return options;
}
}
///
///
///
/// Gets or sets a value indicating whether a Help button appears
/// in the color dialog box.
///
///
[
SRCategory(SR.CatBehavior),
DefaultValue(false),
SRDescription(SR.CDshowHelpDescr)
]
public virtual bool ShowHelp {
get {
return GetOption(NativeMethods.CC_SHOWHELP);
}
set {
SetOption(NativeMethods.CC_SHOWHELP, value);
}
}
///
///
///
/// Gets
/// or sets a value indicating
/// whether the dialog
/// box will restrict users to selecting solid colors only.
///
///
[
SRCategory(SR.CatBehavior),
DefaultValue(false),
SRDescription(SR.CDsolidColorOnlyDescr)
]
public virtual bool SolidColorOnly {
get {
return GetOption(NativeMethods.CC_SOLIDCOLOR);
}
set {
SetOption(NativeMethods.CC_SOLIDCOLOR, value);
}
}
///
///
/// Lets us control the CHOOSECOLOR options.
///
///
private bool GetOption(int option) {
return(options & option) != 0;
}
///
///
///
/// Resets
/// all options to their
/// default values, the last selected color to black, and the custom
/// colors to their default values.
///
///
public override void Reset() {
options = 0;
color = Color.Black;
CustomColors = null;
}
private void ResetColor() {
Color = Color.Black;
}
///
///
///
///
protected override bool RunDialog(IntPtr hwndOwner) {
NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc);
NativeMethods.CHOOSECOLOR cc = new NativeMethods.CHOOSECOLOR();
IntPtr custColorPtr = Marshal.AllocCoTaskMem(64);
try {
Marshal.Copy(customColors, 0, custColorPtr, 16);
cc.hwndOwner = hwndOwner;
cc.hInstance = Instance;
cc.rgbResult = ColorTranslator.ToWin32(color);
cc.lpCustColors = custColorPtr;
int flags = Options | (NativeMethods.CC_RGBINIT | NativeMethods.CC_ENABLEHOOK);
// Our docs say AllowFullOpen takes precedence over FullOpen; ChooseColor implements the opposite
if (!AllowFullOpen)
flags &= ~NativeMethods.CC_FULLOPEN;
cc.Flags = flags;
cc.lpfnHook = hookProcPtr;
if (!SafeNativeMethods.ChooseColor(cc)) return false;
if (cc.rgbResult != ColorTranslator.ToWin32(color)) color = ColorTranslator.FromOle(cc.rgbResult);
Marshal.Copy(custColorPtr, customColors, 0, 16);
return true;
}
finally {
Marshal.FreeCoTaskMem(custColorPtr);
}
}
///
///
/// Allows us to manipulate the CHOOSECOLOR options
///
///
private void SetOption(int option, bool value) {
if (value) {
options |= option;
}
else {
options &= ~option;
}
}
///
///
///
/// Indicates whether the property should be
/// persisted.
///
///
private bool ShouldSerializeColor() {
return !Color.Equals(Color.Black);
}
///
///
///
///
/// Provides a string version of this object.
///
///
public override string ToString() {
string s = base.ToString();
return s + ", Color: " + Color.ToString();
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.Windows.Forms {
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security;
using System.Security.Permissions;
///
///
///
/// Represents a common dialog box that displays available colors along with
/// controls that allow the user to define custom colors.
///
///
[DefaultProperty("Color")]
[SRDescription(SR.DescriptionColorDialog)]
// The only event this dialog has is HelpRequest, which isn't very useful
public class ColorDialog : CommonDialog {
private int options;
private int[] customColors;
///
///
///
///
private Color color;
///
///
///
/// Initializes a new instance of the
/// class.
///
///
[
SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors") // If the constructor does not call Reset
// it would be a breaking change.
]
public ColorDialog() {
customColors = new int[16];
Reset();
}
///
///
///
/// Gets or sets a value indicating whether the user can use the dialog box
/// to define custom colors.
///
///
[
SRCategory(SR.CatBehavior),
DefaultValue(true),
SRDescription(SR.CDallowFullOpenDescr)
]
public virtual bool AllowFullOpen {
get {
return !GetOption(NativeMethods.CC_PREVENTFULLOPEN);
}
set {
SetOption(NativeMethods.CC_PREVENTFULLOPEN, !value);
}
}
///
///
///
/// Gets or sets a value indicating whether the dialog box displays all available colors in
/// the set of basic colors.
///
///
[
SRCategory(SR.CatBehavior),
DefaultValue(false),
SRDescription(SR.CDanyColorDescr)
]
public virtual bool AnyColor {
get {
return GetOption(NativeMethods.CC_ANYCOLOR);
}
set {
SetOption(NativeMethods.CC_ANYCOLOR, value);
}
}
///
///
///
/// Gets or sets the color selected by the user.
///
///
[
SRCategory(SR.CatData),
SRDescription(SR.CDcolorDescr)
]
public Color Color {
get {
return color;
}
set {
if (!value.IsEmpty) {
color = value;
}
else {
color = Color.Black;
}
}
}
///
///
///
/// Gets or sets the set of
/// custom colors shown in the dialog box.
///
///
[
Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
SRDescription(SR.CDcustomColorsDescr)
]
public int[] CustomColors {
get { return(int[]) customColors.Clone();}
set {
int length = value == null? 0: Math.Min(value.Length, 16);
if (length > 0) Array.Copy(value, 0, customColors, 0, length);
for (int i = length; i < 16; i++) customColors[i] = 0x00FFFFFF;
}
}
///
///
///
/// Gets or sets a value indicating whether the controls used to create custom
/// colors are visible when the dialog box is opened
///
///
[
SRCategory(SR.CatAppearance),
DefaultValue(false),
SRDescription(SR.CDfullOpenDescr)
]
public virtual bool FullOpen {
get {
return GetOption(NativeMethods.CC_FULLOPEN);
}
set {
SetOption(NativeMethods.CC_FULLOPEN, value);
}
}
///
///
///
///
/// Our HINSTANCE from Windows.
///
///
protected virtual IntPtr Instance {
[
SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.UnmanagedCode),
SecurityPermission(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)
]
[ResourceExposure(ResourceScope.Process)]
[ResourceConsumption(ResourceScope.Process)]
get { return UnsafeNativeMethods.GetModuleHandle(null);}
}
///
///
/// Returns our CHOOSECOLOR options.
///
///
protected virtual int Options {
get {
return options;
}
}
///
///
///
/// Gets or sets a value indicating whether a Help button appears
/// in the color dialog box.
///
///
[
SRCategory(SR.CatBehavior),
DefaultValue(false),
SRDescription(SR.CDshowHelpDescr)
]
public virtual bool ShowHelp {
get {
return GetOption(NativeMethods.CC_SHOWHELP);
}
set {
SetOption(NativeMethods.CC_SHOWHELP, value);
}
}
///
///
///
/// Gets
/// or sets a value indicating
/// whether the dialog
/// box will restrict users to selecting solid colors only.
///
///
[
SRCategory(SR.CatBehavior),
DefaultValue(false),
SRDescription(SR.CDsolidColorOnlyDescr)
]
public virtual bool SolidColorOnly {
get {
return GetOption(NativeMethods.CC_SOLIDCOLOR);
}
set {
SetOption(NativeMethods.CC_SOLIDCOLOR, value);
}
}
///
///
/// Lets us control the CHOOSECOLOR options.
///
///
private bool GetOption(int option) {
return(options & option) != 0;
}
///
///
///
/// Resets
/// all options to their
/// default values, the last selected color to black, and the custom
/// colors to their default values.
///
///
public override void Reset() {
options = 0;
color = Color.Black;
CustomColors = null;
}
private void ResetColor() {
Color = Color.Black;
}
///
///
///
///
protected override bool RunDialog(IntPtr hwndOwner) {
NativeMethods.WndProc hookProcPtr = new NativeMethods.WndProc(this.HookProc);
NativeMethods.CHOOSECOLOR cc = new NativeMethods.CHOOSECOLOR();
IntPtr custColorPtr = Marshal.AllocCoTaskMem(64);
try {
Marshal.Copy(customColors, 0, custColorPtr, 16);
cc.hwndOwner = hwndOwner;
cc.hInstance = Instance;
cc.rgbResult = ColorTranslator.ToWin32(color);
cc.lpCustColors = custColorPtr;
int flags = Options | (NativeMethods.CC_RGBINIT | NativeMethods.CC_ENABLEHOOK);
// Our docs say AllowFullOpen takes precedence over FullOpen; ChooseColor implements the opposite
if (!AllowFullOpen)
flags &= ~NativeMethods.CC_FULLOPEN;
cc.Flags = flags;
cc.lpfnHook = hookProcPtr;
if (!SafeNativeMethods.ChooseColor(cc)) return false;
if (cc.rgbResult != ColorTranslator.ToWin32(color)) color = ColorTranslator.FromOle(cc.rgbResult);
Marshal.Copy(custColorPtr, customColors, 0, 16);
return true;
}
finally {
Marshal.FreeCoTaskMem(custColorPtr);
}
}
///
///
/// Allows us to manipulate the CHOOSECOLOR options
///
///
private void SetOption(int option, bool value) {
if (value) {
options |= option;
}
else {
options &= ~option;
}
}
///
///
///
/// Indicates whether the property should be
/// persisted.
///
///
private bool ShouldSerializeColor() {
return !Color.Equals(Color.Black);
}
///
///
///
///
/// Provides a string version of this object.
///
///
public override string ToString() {
string s = base.ToString();
return s + ", Color: " + Color.ToString();
}
}
}
// 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
- TimeIntervalCollection.cs
- UserCancellationException.cs
- HtmlInputControl.cs
- GeometryCombineModeValidation.cs
- UniformGrid.cs
- Constraint.cs
- UserControl.cs
- ManipulationDevice.cs
- TableLayoutPanel.cs
- DataGridViewRowStateChangedEventArgs.cs
- SelectionChangedEventArgs.cs
- XPathDocument.cs
- BaseUriHelper.cs
- DataBindingCollectionConverter.cs
- SupportsEventValidationAttribute.cs
- SiteMapNode.cs
- XmlNullResolver.cs
- PriorityQueue.cs
- RoleService.cs
- WebPartActionVerb.cs
- CompatibleComparer.cs
- XmlIlTypeHelper.cs
- ButtonStandardAdapter.cs
- EventListener.cs
- GridLengthConverter.cs
- SignatureDescription.cs
- CollectionChangeEventArgs.cs
- ByteAnimationUsingKeyFrames.cs
- RoleManagerSection.cs
- ServiceModelEnhancedConfigurationElementCollection.cs
- validation.cs
- SafeCryptContextHandle.cs
- ProcessInputEventArgs.cs
- WebDescriptionAttribute.cs
- ReflectTypeDescriptionProvider.cs
- errorpatternmatcher.cs
- ClonableStack.cs
- DataGridView.cs
- ResourcePool.cs
- XmlReaderDelegator.cs
- XmlSchemaAnyAttribute.cs
- HostExecutionContextManager.cs
- CodeStatementCollection.cs
- CodeAssignStatement.cs
- MulticastDelegate.cs
- SecurityKeyType.cs
- AuthorizationBehavior.cs
- System.Data.OracleClient_BID.cs
- StoreAnnotationsMap.cs
- MimeXmlImporter.cs
- Line.cs
- DataDocumentXPathNavigator.cs
- ProcessManager.cs
- ActivityValidator.cs
- ArrayTypeMismatchException.cs
- FormViewCommandEventArgs.cs
- _SslSessionsCache.cs
- TreeViewHitTestInfo.cs
- RsaSecurityKey.cs
- ViewgenContext.cs
- ServiceNotStartedException.cs
- HtmlTableRowCollection.cs
- WorkflowServiceBuildProvider.cs
- SmiEventSink.cs
- TreeViewHitTestInfo.cs
- _RequestCacheProtocol.cs
- InvalidPropValue.cs
- SoapAttributeOverrides.cs
- XmlBufferReader.cs
- _LoggingObject.cs
- MimeWriter.cs
- SerialErrors.cs
- HwndSubclass.cs
- TextRange.cs
- GridToolTip.cs
- DiagnosticsConfigurationHandler.cs
- TransformValueSerializer.cs
- WindowsUserNameCachingSecurityTokenAuthenticator.cs
- HtmlSelect.cs
- FixUpCollection.cs
- BindingListCollectionView.cs
- Label.cs
- Base64Decoder.cs
- OletxTransactionHeader.cs
- ElementProxy.cs
- CodeDelegateInvokeExpression.cs
- VisualTreeHelper.cs
- NavigationWindowAutomationPeer.cs
- TypeUnloadedException.cs
- PointAnimationClockResource.cs
- DataTableReader.cs
- CancelAsyncOperationRequest.cs
- X509Utils.cs
- DropShadowEffect.cs
- DataAccessException.cs
- ALinqExpressionVisitor.cs
- RowSpanVector.cs
- AdornedElementPlaceholder.cs
- GroupPartitionExpr.cs
- OneToOneMappingSerializer.cs