private Gobicode.Win.HTMLLabel myHTMLLabel;
//This method initialises the specified ListBox for custom painting
//A HTMLLabel control is created for the purpose of painting HTML
//onto the ListBox
private void InitialiseHTMLLabel(ListBox listbox)
{
//Create an HTMLLabel instance which we will use to render HTML
this.myHTMLLabel = new Gobicode.Win.HTMLLabel();
this.myHTMLLabel.AutoSize = false;
//Use the ListBox's Font as the default.
this.myHTMLLabel.Font = listbox.Font;
//
myHTMLLabel.Width = listbox.ClientRectangle.Width;
//Populate the ListBox with simple HTML strings
listbox.Items.AddRange(new object[] {"Different <big>size <big>text</big></big>",
"<b>Bold</b>",
"<u>Underline</u>",
"<i>Italic</i>",
"<sup>super</sup> script",
"<sub>sub</sub> script"});
//Hook MeasureItem and DrawItem events
listbox.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.listBox1_MeasureItem);
listbox.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
//Set the ListBox to be owner drawn
listbox.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
}
//This event handler will set the height of each item before it is drawn.
private void listBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
//Assign the ListBox item's text to the HTMLLabel.
myHTMLLabel.Text = (sender as ListBox).Items[e.Index] as string;
//Measure the size required to render the HTML when the width is constrained to that of the ListBox
Size renderSize = myHTMLLabel.GetRenderSize((sender as ListBox).ClientSize.Width);
e.ItemHeight = renderSize.Height +2;
}
// This event handler draws the HTML representation of the Item's text
private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0)
{
myHTMLLabel.Text = (sender as ListBox).Items[e.Index] as string;
//paint the HTML directly onto the ListBox's canvas
myHTMLLabel.RenderTo(e.Graphics, e.Bounds);
}
}
|