namespace Zeta.EnterpriseLibrary.Windows.Controls
{
#region Using directives.
// ----------------------------------------------------------------------
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;
// ----------------------------------------------------------------------
#endregion
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// Extended combo box that draws images and indention.
/// </summary>
public partial class ImageComboBox :
ExtendedComboBox
{
#region Public methods.
// ------------------------------------------------------------------
/// <summary>
/// Initializes a new instance of the <see cref="ImageComboBox"/> class.
/// </summary>
public ImageComboBox()
{
InitializeComponent();
//SetStyle( ControlStyles.AllPaintingInWmPaint, true );
//SetStyle( ControlStyles.UserPaint, true );
//_isVista = ((Environment.OSVersion.Version >= new Version( @"6.0.0" )) &&
// (Environment.OSVersion.Platform == PlatformID.Win32NT));
//DropDownStyle = ComboBoxStyle.DropDownList;
//FlatStyle = FlatStyle.System;
DrawMode = DrawMode.OwnerDrawFixed;
}
/// <summary>
/// Initializes a new instance of the <see cref="ImageComboBox"/> class.
/// </summary>
/// <param name="container">The container.</param>
public ImageComboBox(
IContainer container )
{
container.Add( this );
InitializeComponent();
//SetStyle( ControlStyles.AllPaintingInWmPaint, true );
//SetStyle( ControlStyles.UserPaint, true );
//_isVista =
// ((Environment.OSVersion.Version >= new Version( @"6.0.0" )) &&
// (Environment.OSVersion.Platform == PlatformID.Win32NT));
//DropDownStyle = ComboBoxStyle.DropDownList;
//FlatStyle = FlatStyle.System;
DrawMode = DrawMode.OwnerDrawFixed;
}
// ------------------------------------------------------------------
#endregion
#region Public properties,
// ------------------------------------------------------------------
/// <summary>
/// ImageList property.
/// </summary>
/// <value>The image list.</value>
public ImageList ImageList
{
get
{
return _imageList;
}
set
{
_imageList = value;
}
}
/// <summary>
/// Strongly-typed.
/// </summary>
/// <value></value>
/// <returns>The object that is the currently selected item or null if there is no currently selected item.</returns>
/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
public new ImageComboBoxItem SelectedItem
{
get
{
return base.SelectedItem as ImageComboBoxItem;
}
set
{
base.SelectedItem = value;
}
}
/// <summary>
/// Select by the Tag property of an <see cref="ImageComboBoxItem"/>.
/// </summary>
/// <value>The selected tag.</value>
public virtual object SelectedTag
{
get
{
if ( base.SelectedItem == null )
{
return null;
}
else
{
var icbi = base.SelectedItem as ImageComboBoxItem;
return icbi == null ? base.SelectedItem : icbi.Tag;
}
}
set
{
foreach ( var o in Items )
{
var icbi = o as ImageComboBoxItem;
if ( icbi == null )
{
if ( o == value )
{
base.SelectedItem = o;
return;
}
}
else
{
var tag = icbi.Tag;
if ( tag == null && value == null )
{
base.SelectedItem = o;
return;
}
else if ( tag != null && value != null )
{
// http://www.codeproject.com/dotnet/DotNetEquality.asp.
if ( tag.Equals( value ) )
{
base.SelectedItem = o;
return;
}
}
}
}
// Not found.
base.SelectedIndex = -1;
}
}
// ------------------------------------------------------------------
#endregion
#region Private methods.
// ------------------------------------------------------------------
/// <summary>
/// Raises the <see cref="E:System.Windows.Forms.ComboBox.DrawItem"></see> event.
/// </summary>
/// <param name="e">A <see cref="T:System.Windows.Forms.DrawItemEventArgs"></see>
/// that contains the event data.</param>
protected override void OnDrawItem(
DrawItemEventArgs e )
{
base.OnDrawItem( e );
e.DrawBackground();
drawMyStuff( e );
if ( Enabled )
{
e.DrawFocusRectangle();
}
}
/// <summary>
/// Draws my stuff.
/// </summary>
/// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/>
/// instance containing the event data.</param>
private void drawMyStuff(
DrawItemEventArgs e )
{
// Check if it is an item from the Items collection.
if ( e.Index < 0 )
{
// not an item, draw the text (indented)
e.Graphics.DrawString(
Text,
e.Font,
new SolidBrush( e.ForeColor ),
0 + e.Bounds.Left + _imageList.ImageSize.Width,
e.Bounds.Top );
}
else
{
// Check if item is an ImageComboBoxItem.
if ( Items[e.Index].GetType() == typeof( ImageComboBoxItem ) )
{
// get item to draw
var item = (ImageComboBoxItem)Items[e.Index];
// get forecolor & font
Color foreColor;
if ( Enabled )
{
if ( (e.State & DrawItemState.Selected) != 0 )
{
// If selected, use default color to have
// enough contrast. TreeView and ListView do the
// same.
foreColor = e.ForeColor;
}
else
{
foreColor =
(item.ForeColor != Color.Transparent &&
item.ForeColor != Color.Empty)
? item.ForeColor
: e.ForeColor;
}
}
else
{
foreColor = SystemColors.GrayText;
}
var font = e.Font;
if ( item.Bold || item.Italic )
{
if ( item.Bold && item.Italic )
{
font = new Font( e.Font, FontStyle.Bold | FontStyle.Italic );
}
else if ( item.Bold )
{
font = new Font( e.Font, FontStyle.Bold );
}
else
{
font = new Font( e.Font, FontStyle.Italic );
}
}
// -1: no image
if ( item.ImageIndex != -1 ||
!string.IsNullOrEmpty( item.ImageKey ) )
{
var imageIndex = item.ImageIndex;
if ( item.ImageIndex == -1 )
{
Debug.Assert(
!string.IsNullOrEmpty( item.ImageKey ) );
imageIndex =
ImageList.Images.IndexOfKey( item.ImageKey );
Debug.Assert( imageIndex >= 0 );
}
if ( imageIndex >= 0 && imageIndex < ImageList.Images.Count )
{
// draw image, then draw text next to it.
ImageList.Draw(
e.Graphics,
item.IndentInPixel + e.Bounds.Left,
e.Bounds.Top,
imageIndex );
}
TextRenderer.DrawText(
e.Graphics,
item.Text,
font,
new Point(
item.IndentInPixel + e.Bounds.Left + _imageList.ImageSize.Width,
e.Bounds.Top ),
foreColor );
}
else if ( item.Image != null )
{
// draw image, then draw text next to it.
e.Graphics.DrawImage(
item.Image,
item.IndentInPixel + e.Bounds.Left,
e.Bounds.Top,
item.Image.Width,
item.Image.Height );
TextRenderer.DrawText(
e.Graphics,
item.Text,
font,
new Point(
item.IndentInPixel + e.Bounds.Left + item.Image.Width,
e.Bounds.Top ),
foreColor );
}
else
{
// draw text (indented)
var imageListImageSizeWidth = 0;
if ( _imageList != null && !_imageList.Images.Empty )
{
imageListImageSizeWidth = _imageList.ImageSize.Width;
}
TextRenderer.DrawText(
e.Graphics,
item.Text,
font,
new Point(
item.IndentInPixel + e.Bounds.Left + imageListImageSizeWidth,
e.Bounds.Top ),
foreColor );
}
}
else
{
// it is not an ImageComboBoxItem, draw it
TextRenderer.DrawText(
e.Graphics,
Items[e.Index].ToString(),
e.Font,
new Point(
0 + e.Bounds.Left + _imageList.ImageSize.Width,
e.Bounds.Top ),
e.ForeColor
);
}
}
}
// ------------------------------------------------------------------
#endregion
#region Private variables.
// ------------------------------------------------------------------
private ImageList _imageList = new ImageList();
//private readonly bool _isVista;
// ------------------------------------------------------------------
#endregion
}
/////////////////////////////////////////////////////////////////////////
}