1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
public class ImageCombo : System.Windows.Forms.ComboBox
{
public ImageCombo()
{
//on veut le dessiner nous même
this.DrawMode = DrawMode.OwnerDrawFixed;
//forcement un DropDownList
this.DropDownStyle = ComboBoxStyle.DropDownList;
m_oColumns = 1;
}
private ImageList m_oImageList;
public ImageList ImageList
{
get { return m_oImageList; }
set { m_oImageList = value; }
}
private int m_oColumns;
public int Columns
{
get { return m_oColumns; }
set
{
if (value > 0)
m_oColumns = value;
else
throw new Exception("Nombre de colonnes incorrect");
}
}
protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
{
this.DropDownStyle = ComboBoxStyle.DropDownList;
base.OnDrawItem(e);
if (e.Index == -1)
return;
System.Drawing.SolidBrush oB;
if (this.ImageList != null)
{
ImageItem oItem = this.Items[e.Index] as ImageItem;
//l'image associée
Image oImg = null;
if (oItem != null && oItem.ImageIndex >= 0)
{
oImg = this.ImageList.Images[oItem.ImageIndex];
//dessin de l'image
e.Graphics.DrawImage(oImg, e.Bounds.X, e.Bounds.Y);
}
//dessin du texte
StringFormat oSF = new StringFormat();
oSF.Alignment = StringAlignment.Near;
oSF.LineAlignment = StringAlignment.Center;
string text = this.Items[e.Index].ToString();
string[] itms = text.Split('\t');
int tW = e.Bounds.Width - this.ImageList.ImageSize.Width;
int lX = e.Bounds.X + this.ImageList.ImageSize.Width;
int cC = itms.Length;
int CW = tW / m_oColumns;
int[] CX = new int[m_oColumns];
oB = new System.Drawing.SolidBrush(this.ForeColor);
for (int i = 0; i < m_oColumns; i++)
{
CX[i] = (i > 0) ? CX[i - 1] + CW : lX;
Rectangle oRect = new Rectangle(
CX[i],
e.Bounds.Y,
CW,
e.Bounds.Height);
e.Graphics.DrawString((i < cC) ? itms[i] : "", this.Font, oB, oRect, oSF);
}
oB.Dispose();
return;
}
oB = new System.Drawing.SolidBrush(this.ForeColor);
e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, oB, e.Bounds);
oB.Dispose();
}
} |
Partager