using System;
using System.Drawing;
using System.Windows.Forms;
namespace Vanara.Windows.Forms
{
/// Represents an image used within a control.
public class ControlImage
{
private Image image;
private string imageKey = string.Empty;
private ImageList imageList;
private int index = -1;
private bool useIntegerIndex = true;
/// Initializes a new instance of the class.
/// The parent.
public ControlImage(Control parent) { Control = parent; }
/// Gets or sets the image.
/// The image.
public virtual Image Image
{
get
{
if ((image == null) && (imageList != null))
{
int actualIndex = ActualIndex;
if (actualIndex >= imageList.Images.Count)
actualIndex = imageList.Images.Count - 1;
if (actualIndex >= 0)
return imageList.Images[actualIndex];
}
return image;
}
set
{
if (image == value) return;
image = value;
if (image != null)
{
ImageIndex = -1;
ImageList = null;
Control?.Invalidate();
}
}
}
/// Gets or sets the index of the image.
/// The index of the image.
/// ImageIndex
public virtual int ImageIndex
{
get
{
if ((index != -1) && (imageList != null) && (index >= imageList.Images.Count))
return imageList.Images.Count - 1;
return index;
}
set
{
if (value < -1) throw new ArgumentOutOfRangeException(nameof(ImageIndex));
if (index == value) return;
if (value != -1) image = null;
imageKey = string.Empty;
index = value;
useIntegerIndex = true;
Control?.Invalidate();
}
}
/// Gets or sets the image key.
/// The image key.
public virtual string ImageKey
{
get => imageKey; set
{
if (imageKey == value) return;
index = -1;
if (value != null) image = null;
imageKey = value ?? string.Empty;
useIntegerIndex = false;
Control?.Invalidate();
}
}
/// Gets or sets the image list.
/// The image list.
public virtual ImageList ImageList
{
get => imageList; set
{
if (imageList == value) return;
if (imageList != null)
imageList.RecreateHandle -= ImageListOnRecreateHandle;
if (value != null)
image = null;
imageList = value;
if (imageList != null)
imageList.RecreateHandle += ImageListOnRecreateHandle;
Control?.Invalidate();
}
}
/// Gets the actual index.
/// The actual index.
protected virtual int ActualIndex
{
get
{
if (useIntegerIndex)
return ImageIndex;
if (ImageList != null)
return ImageList.Images.IndexOfKey(ImageKey);
return -1;
}
}
/// Gets or sets the control.
/// The control.
protected Control Control { get; set; }
private void ImageListOnRecreateHandle(object sender, EventArgs eventArgs)
{
if (Control?.IsHandleCreated ?? false) Control.Invalidate();
}
}
}