using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
namespace EasyProgressBarPacket
{
[Editor(typeof(ColorizerProgressEditor), typeof(UITypeEditor))]
[TypeConverter(typeof(ColorizerProgressConverter))]
public class ColorizerProgress : IProgressColorizer
{
#region Event
/// <summary>
/// Occurs when the sub properties changed of the ProgressColorizer property.
/// </summary>
[Description("Occurs when the sub properties changed of the ProgressColorizer property")]
public event EventHandler ProgressColorizerChanged;
#endregion
#region Instance Members
// Red, Green, Blue, Alpha
private byte[] rgba = { 180, 180, 180, 180 };
// IsColorizerEnabled, IsTransparencyEnabled
private bool[] options = { false, false };
#endregion
#region Constructor
public ColorizerProgress() { }
public ColorizerProgress(byte red, byte green, byte blue, byte alpha,
bool isColorizerEnabled, bool isTransparencyEnabled)
{
// Sets RGBA
rgba[0] = red;
rgba[1] = green;
rgba[2] = blue;
rgba[3] = alpha;
// Sets Options
options[0] = isColorizerEnabled;
options[1] = isTransparencyEnabled;
}
#endregion
#region Virtual Methods
protected virtual void OnProgressColorizerChanged(EventArgs e)
{
if (ProgressColorizerChanged != null)
ProgressColorizerChanged(this, e);
}
#endregion
#region IProgressColorizer Members
/// <summary>
/// Determines whether the colorizer effect is enable or not for progress bitmap.
/// </summary>
[Description("Determines whether the colorizer effect is enable or not for progress bitmap")]
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(false)]
[Browsable(true)]
public bool IsColorizerEnabled
{
get { return options[0]; }
set
{
if (!value.Equals(options[0]))
{
options[0] = value;
OnProgressColorizerChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// Determines whether the transparency effect is visible or not for progress bitmap.
/// </summary>
[Description("Determines whether the transparency effect is visible or not for progress bitmap")]
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(false)]
[Browsable(true)]
public bool IsTransparencyEnabled
{
get { return options[1]; }
set
{
if (!value.Equals(options[1]))
{
options[1] = value;
OnProgressColorizerChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// The red color component property must be in the range of 0 to 255.
/// </summary>
[Description("The red color component property must be in the range of 0 to 255")]
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(typeof(Byte), "180")]
[Browsable(true)]
public byte Red
{
get { return rgba[0]; }
set
{
if (!value.Equals(rgba[0]))
{
rgba[0] = value;
if (IsColorizerEnabled)
OnProgressColorizerChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// The green color component property must be in the range of 0 to 255.
/// </summary>
[Description("The green color component property must be in the range of 0 to 255")]
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(typeof(Byte), "180")]
[Browsable(true)]
public byte Green
{
get { return rgba[1]; }
set
{
if (!value.Equals(rgba[1]))
{
rgba[1] = value;
if (IsColorizerEnabled)
OnProgressColorizerChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// The blue color component property must be in the range of 0 to 255.
/// </summary>
[Description("The blue color component property must be in the range of 0 to 255")]
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(typeof(Byte), "180")]
[Browsable(true)]
public byte Blue
{
get { return rgba[2]; }
set
{
if (!value.Equals(rgba[2]))
{
rgba[2] = value;
if (IsColorizerEnabled)
OnProgressColorizerChanged(EventArgs.Empty);
}
}
}
/// <summary>
/// This property must be in the range of 50 to 255.
/// </summary>
[Description("This property must be in the range of 50 to 255")]
[RefreshProperties(RefreshProperties.Repaint)]
[NotifyParentProperty(true)]
[DefaultValue(typeof(Byte), "180")]
[Browsable(true)]
public byte Alpha
{
get { return rgba[3]; }
set
{
if (!value.Equals(rgba[3]))
{
if (value < 50)
value = 50;
rgba[3] = value;
if (IsTransparencyEnabled)
OnProgressColorizerChanged(EventArgs.Empty);
}
}
}
#endregion
#region IDisposable Members
public void Dispose()
{
GC.SuppressFinalize(this);
}
#endregion
}
class ColorizerProgressConverter : ExpandableObjectConverter
{
#region Destructor
~ColorizerProgressConverter()
{
GC.SuppressFinalize(this);
}
#endregion
#region Override Methods
//All the CanConvertTo() method needs to is check that the target type is a string.
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
else
return base.CanConvertTo(context, destinationType);
}
//ConvertTo() simply checks that it can indeed convert to the desired type.
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
return ToString(value);
else
return base.ConvertTo(context, culture, value, destinationType);
}
/* The exact same process occurs in reverse when converting a ColorizerProgress object to a string.
First the Properties window calls CanConvertFrom(). If it returns true, the next step is to call
the ConvertFrom() method. */
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
return FromString(value);
else
return base.ConvertFrom(context, culture, value);
}
#endregion
#region Helper Methods
private string ToString(object value)
{
// Gelen object tipimizi ColorizerProgress tipine dönüştürüyoruz ve ayıklama işlemine başlıyoruz.
ColorizerProgress colorizer = value as ColorizerProgress;
return String.Format("{0}, {1}, {2}, {3}, {4}, {5}",
colorizer.Red, colorizer.Green, colorizer.Blue, colorizer.Alpha,
colorizer.IsColorizerEnabled, colorizer.IsTransparencyEnabled);
}
private ColorizerProgress FromString(object value)
{
string[] result = ((string)value).Split(',');
if (result.Length != 6)
throw new ArgumentException("Could not convert to value");
try
{
ColorizerProgress colorizer = new ColorizerProgress();
ByteConverter byteConverter = new ByteConverter();
BooleanConverter booleanConverter = new BooleanConverter();
// Retrieve the values of the object.
colorizer.Red = (byte)byteConverter.ConvertFromString(result[0]);
colorizer.Green = (byte)byteConverter.ConvertFromString(result[1]);
colorizer.Blue = (byte)byteConverter.ConvertFromString(result[2]);
colorizer.Alpha = (byte)byteConverter.ConvertFromString(result[3]);
colorizer.IsColorizerEnabled = (bool)booleanConverter.ConvertFromString(result[4]);
colorizer.IsTransparencyEnabled = (bool)booleanConverter.ConvertFromString(result[5]);
return colorizer;
}
catch (Exception)
{
throw new ArgumentException("Could not convert to value");
}
}
#endregion
}
class ColorizerProgressEditor : UITypeEditor
{
#region Constructor
public ColorizerProgressEditor() : base() { }
#endregion
#region Destructor
~ColorizerProgressEditor()
{
GC.SuppressFinalize(this);
}
#endregion
#region Override Methods
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (provider != null)
{
IWindowsFormsEditorService editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (editorService != null)
{
using (DropDownProgress dropDownProgress = new DropDownProgress(value, editorService))
{
editorService.DropDownControl(dropDownProgress);
value = dropDownProgress.Colorizer;
}
}
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown; // We choose the drop-down style.
}
/// <summary>
/// Indicates whether the specified context supports painting a representation of an object's value within the specified context.
/// </summary>
/// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
/// <returns>Normally, true if PaintValue is implemented; otherwise, false.But, in this scope returns false.</returns>
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return false; // We turn down thumbnails.
}
#endregion
}
}