private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
bool selected = false;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
e = new DrawItemEventArgs(e.Graphics,
e.Font,
e.Bounds,
e.Index,
e.State ^ DrawItemState.Selected,
e.ForeColor,
Color.FromArgb(51, 153, 255));//Choose the color
selected = true;
// Draw the selected background
e.DrawBackground();
}
else
{
if (e.Index % 2 == 1)
{
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(239, 248, 253)), e.Bounds);
}
else
{
// Draw the default background
e.DrawBackground();
}
}
// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
if (e.Index >= 0)
{
Suggestion suggestion = (Suggestion)listBox1.Items[e.Index];
String content="", sourceName="";
bool entityMode = false;
switch(suggestion.type){
case Suggestion.ENTITY:
Entity entity = (Entity)suggestion;
content = entity.content;
sourceName = entity.source.name;
entityMode = true;
break;
case Suggestion.SENTENCE:
Sentence sentence = (Sentence)suggestion;
content = sentence.content;
sourceName = sentence.source.name;
break;
}
content = content.Replace("\n", " ");
Rectangle drawBound = e.Bounds;
drawBound.X += padding;
drawBound.Y += padding;
drawBound.Width -= padding * 2;
drawBound.Height -= padding * 2;
StringFormat stringFormat = StringFormat.GenericDefault;
stringFormat.Alignment = StringAlignment.Near;
// Draw Index String
Brush myBrush = new SolidBrush(Color.FromArgb(102, 106, 114));
if (selected)
{
myBrush = Brushes.White;
}
Font font = new Font(e.Font.FontFamily, e.Font.Size + 6, e.Font.Style);
drawBound.Y += 2;
e.Graphics.DrawString((e.Index + 1) + "", font, myBrush, drawBound, stringFormat);
drawBound.Y -= 2;
// Draw Content String
myBrush = Brushes.Black;
if (selected)
{
myBrush = Brushes.White;
}
font = new Font(e.Font.FontFamily, e.Font.Size + 1, e.Font.Style);
content = getSuggestionWrapping(content, font, entityMode);
drawBound.X += 18;
drawBound.Width -= 18;
e.Graphics.DrawString(content, font, myBrush, drawBound, stringFormat);
if (suggestion.type == Suggestion.ENTITY)
{
int saveX = drawBound.X;
int saveWidth = drawBound.Width;
Entity entity = (Entity)suggestion;
Image icon = null;
int iconWidth=0, iconHeight=0; // Have to manually set the icon size to its actual size.
int offsetY = 0, offsetX = 0;
String entityTypeName = "";
switch (entity.entityType)
{
case Entity.EMAIL:
entityTypeName = "Email";
icon = global::ACPAddIn.Properties.Resources.email_icon;
iconWidth = 16;
iconHeight = 10;
offsetY = 2;
offsetX = -5;
break;
case Entity.URL:
entityTypeName = "Link";
icon = global::ACPAddIn.Properties.Resources.link_icon;
iconWidth = 15;
iconHeight = 6;
offsetY = 4;
offsetX = -5;
break;
case Entity.PLACE:
entityTypeName = "Location";
icon = global::ACPAddIn.Properties.Resources.place_icon;
iconWidth = 8;
iconHeight = 13;
offsetY = 0;
offsetX = -1;
break;
case Entity.NAME:
entityTypeName = "Name";
icon = global::ACPAddIn.Properties.Resources.name_icon;
iconWidth = 16;
iconHeight = 13;
offsetY = 0;
offsetX = -5;
break;
}
drawBound.X += 240;
drawBound.Width -= 240;
e.Graphics.DrawImage(icon, new Rectangle(drawBound.X + offsetX, drawBound.Y + offsetY, iconWidth, iconHeight));
drawBound.X += 12;
e.Graphics.DrawString(entityTypeName, e.Font, myBrush, drawBound, stringFormat);
drawBound.X = saveX;
drawBound.Width = saveWidth;
}
drawBound.Y += getRenderTextHeight(content, listBox1, font) ;
drawBound.Height -= getRenderTextHeight(content, listBox1, font) ;
myBrush = new SolidBrush(Color.FromArgb(54, 96, 137));
font = new Font(e.Font.FontFamily, e.Font.Size - 1, e.Font.Style);
sourceName = getSourceNameWrapping(sourceName, font);
if (selected)
{
myBrush = Brushes.White;
}
e.Graphics.DrawString(sourceName, font, myBrush, drawBound, StringFormat.GenericDefault);
// Draw bottom line if it is not the last entry
if (e.Index != listBox1.Items.Count-1)
{
Pen pen = new Pen(Color.FromArgb(185,187,189), 1);
Point point1 = new Point(0, e.Bounds.Y + e.Bounds.Height - 1);
Point point2 = new Point(e.Bounds.X + e.Bounds.Width - 1, e.Bounds.Y + e.Bounds.Height - 1);
e.Graphics.DrawLine(pen, point1, point2);
}
}
e.DrawFocusRectangle();
}