2023-09-24 17:26:46 -04:00
|
|
|
|
using System.Drawing;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2023-03-31 11:47:53 -04:00
|
|
|
|
namespace Vanara.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
/// <summary>Represents an image used within a control.</summary>
|
|
|
|
|
public class ControlImage
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2023-09-29 13:58:35 -04:00
|
|
|
|
private Image? image;
|
2023-03-31 11:47:53 -04:00
|
|
|
|
private string imageKey = string.Empty;
|
2023-09-29 13:58:35 -04:00
|
|
|
|
private ImageList? imageList;
|
2023-03-31 11:47:53 -04:00
|
|
|
|
private int index = -1;
|
|
|
|
|
private bool useIntegerIndex = true;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
2023-03-31 11:47:53 -04:00
|
|
|
|
/// <summary>Initializes a new instance of the <see cref="ControlImage"/> class.</summary>
|
|
|
|
|
/// <param name="parent">The parent.</param>
|
2023-09-29 13:58:35 -04:00
|
|
|
|
public ControlImage(Control parent) => Control = parent;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
2023-03-31 11:47:53 -04:00
|
|
|
|
/// <summary>Gets or sets the image.</summary>
|
|
|
|
|
/// <value>The image.</value>
|
2023-09-29 13:58:35 -04:00
|
|
|
|
public virtual Image? Image
|
2023-03-31 11:47:53 -04:00
|
|
|
|
{
|
|
|
|
|
get
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2023-09-29 13:58:35 -04:00
|
|
|
|
if (image == null && imageList != null)
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2023-03-31 11:47:53 -04:00
|
|
|
|
int actualIndex = ActualIndex;
|
|
|
|
|
if (actualIndex >= imageList.Images.Count)
|
|
|
|
|
actualIndex = imageList.Images.Count - 1;
|
|
|
|
|
if (actualIndex >= 0)
|
|
|
|
|
return imageList.Images[actualIndex];
|
2017-11-27 13:11:20 -05:00
|
|
|
|
}
|
2023-03-31 11:47:53 -04:00
|
|
|
|
return image;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
}
|
2023-03-31 11:47:53 -04:00
|
|
|
|
set
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2023-03-31 11:47:53 -04:00
|
|
|
|
if (image == value) return;
|
|
|
|
|
image = value;
|
|
|
|
|
if (image != null)
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2023-03-31 11:47:53 -04:00
|
|
|
|
ImageIndex = -1;
|
|
|
|
|
ImageList = null;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
Control?.Invalidate();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-03-31 11:47:53 -04:00
|
|
|
|
}
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
2023-03-31 11:47:53 -04:00
|
|
|
|
/// <summary>Gets or sets the index of the image.</summary>
|
|
|
|
|
/// <value>The index of the image.</value>
|
2023-09-29 13:58:35 -04:00
|
|
|
|
/// <exception cref="ArgumentOutOfRangeException">ImageIndex</exception>
|
2023-03-31 11:47:53 -04:00
|
|
|
|
public virtual int ImageIndex
|
|
|
|
|
{
|
|
|
|
|
get
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2023-09-29 13:58:35 -04:00
|
|
|
|
if (index != -1 && imageList != null && index >= imageList.Images.Count)
|
2023-03-31 11:47:53 -04:00
|
|
|
|
return imageList.Images.Count - 1;
|
|
|
|
|
return index;
|
2017-11-27 13:11:20 -05:00
|
|
|
|
}
|
2023-03-31 11:47:53 -04:00
|
|
|
|
set
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2023-03-31 11:47:53 -04:00
|
|
|
|
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();
|
2017-11-27 13:11:20 -05:00
|
|
|
|
}
|
2023-03-31 11:47:53 -04:00
|
|
|
|
}
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
2023-03-31 11:47:53 -04:00
|
|
|
|
/// <summary>Gets or sets the image key.</summary>
|
|
|
|
|
/// <value>The image key.</value>
|
|
|
|
|
public virtual string ImageKey
|
|
|
|
|
{
|
|
|
|
|
get => imageKey; set
|
2017-11-27 13:11:20 -05:00
|
|
|
|
{
|
2023-03-31 11:47:53 -04:00
|
|
|
|
if (imageKey == value) return;
|
|
|
|
|
index = -1;
|
|
|
|
|
if (value != null) image = null;
|
|
|
|
|
imageKey = value ?? string.Empty;
|
|
|
|
|
useIntegerIndex = false;
|
|
|
|
|
Control?.Invalidate();
|
2017-11-27 13:11:20 -05:00
|
|
|
|
}
|
2023-03-31 11:47:53 -04:00
|
|
|
|
}
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
2023-03-31 11:47:53 -04:00
|
|
|
|
/// <summary>Gets or sets the image list.</summary>
|
|
|
|
|
/// <value>The image list.</value>
|
2023-09-29 13:58:35 -04:00
|
|
|
|
public virtual ImageList? ImageList
|
2023-03-31 11:47:53 -04:00
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-27 13:11:20 -05:00
|
|
|
|
|
2023-03-31 11:47:53 -04:00
|
|
|
|
/// <summary>Gets the actual index.</summary>
|
|
|
|
|
/// <value>The actual index.</value>
|
|
|
|
|
protected virtual int ActualIndex
|
|
|
|
|
{
|
|
|
|
|
get
|
2020-04-02 23:59:18 -04:00
|
|
|
|
{
|
2023-03-31 11:47:53 -04:00
|
|
|
|
if (useIntegerIndex)
|
|
|
|
|
return ImageIndex;
|
|
|
|
|
if (ImageList != null)
|
|
|
|
|
return ImageList.Images.IndexOfKey(ImageKey);
|
|
|
|
|
return -1;
|
2020-04-02 23:59:18 -04:00
|
|
|
|
}
|
2017-11-27 13:11:20 -05:00
|
|
|
|
}
|
2023-03-31 11:47:53 -04:00
|
|
|
|
|
|
|
|
|
/// <summary>Gets or sets the control.</summary>
|
|
|
|
|
/// <value>The control.</value>
|
|
|
|
|
protected Control Control { get; set; }
|
|
|
|
|
|
2023-09-29 13:58:35 -04:00
|
|
|
|
private void ImageListOnRecreateHandle(object? sender, EventArgs eventArgs)
|
2023-03-31 11:47:53 -04:00
|
|
|
|
{
|
|
|
|
|
if (Control?.IsHandleCreated ?? false) Control.Invalidate();
|
|
|
|
|
}
|
2020-04-02 23:59:18 -04:00
|
|
|
|
}
|