logo


Overview
Features
HTML Tags
Tips
Download
Purchase


HTMLLabel Logo
HTMLLabel  


Tip #1 Use the HTMLLabel control's RenderTo method to draw HTML items in a ListBox

This example sets the ListBox DrawMode property to OwnerDrawVariable and hooks the the MeasureItem and DrawItem events.
The size required for the text is returned by the HTMLLabel GetRenderSize method.
The HTMLLabel RenderTo method paints the HTML using the Graphics object of the ListBox.

To create the example:
  • Create a Form containing a ListBox control named 'listBox1'.
  • Paste the following code in the form.
  • Call InitialiseHTMLLabel(listBox1) from the form's constructor or Load event.

    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);
        }    
    }



    Tip #2 How to display an image which has been embedded as a resource in an assembly.

    We have extended the regular Image <img> tag so that embedded graphic images can be used by the HTMLLabel.
    Files can be embedded within an assembly by adding the file to the project and setting the file's "Build Type" property to "Embedded Resource".
    Note that the assembly must be loaded before the HTMLLabel attempts to find the graphic. Assuming that the assembly is referenced by your application then it will be loaded automatically

    The graphic file is specified using the src attribute of the <img> tag.
    The extended format is:
    src="resource:AssemblyName.Namespace.ResourceName"

    As an example, the file 'Image.jpg' has been embedded in the assembly named 'MyAssembly.dll', the default namespace is 'MyNamespace'.
    This embedded file can be specified using the following HTML
    <img src="resource:MyAssembly.dll.MyNamespace.Image.jpg">


    Tip #3 How to use a font which is shipped with your application

    Using the <font> tag you can specify a .NET Framework compatible font (TrueType) for the HTMLLabel to display. The font must either be installed on the target system or added to the static FontCollection of the HTMLLabel class. This is a PrivateFontCollection object shared by all instances on the control. The FontCollection allows HTMLLabel controls to use a font which you have shipped with your application, the font need not be installed on the target system.
    Please ensure that you are athorised to redistribute the font.

    The following demonstrates adding a font to the static FontCollection.

    Gobicode.Win.HTMLLabel.FontCollection.AddFontFile("My TrueType Font.ttf");

    The font must be added to the collection before it is referenced by a HTMLLabel control. This can be done in a Form's constructor prior to InitializeComponent() being called.

    Now the font can be referenced using the <font> tag within the HTMLLabel's HTML.

    this.htmlLabel1.Text = "<font face="Font Name">Text to display using specified font</font>;



    Tip #4 How to use a font which has been embedded in an assembly.

    If fonts are embedded within you application then use the following method to load the fonts into the HTMLLabel's FontCollection.
    Fonts must be added to the FontCollection before they are referenced by a HTMLLabel control.
    In this example the FontCollection's AddMemoryFont method is used to load the font from memory.

    /// <summary>
    /// TrueType fonts embedded within the executing assembly are added to the FontCollection
    /// </summary>
    private void GetEmbeddedFonts() 
    { 
        Assembly asm = Assembly.GetExecutingAssembly();
     
        //Obtain the names of all resources within the assembly
        string[] resFiles = asm.GetManifestResourceNames();
     
        foreach(string file in resFiles)
        {
            //TrueType fonts use the 'ttf' extension
            if (file.ToLower().EndsWith("ttf"))
            {
                //Create a stream attached to the resource data
                using (Stream fontStream = asm.GetManifestResourceStream(file))
                {
                    byte[] buffer = new byte[fontStream.Length];
     
                    //Read the font data from the stream into our byte array
                    fontStream.Read(buffer, 0, (int)fontStream.Length);                    
     
                    //Allocate memory (unmanaged) for storing the font
                    IntPtr memPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(byte)) * buffer.Length);
     
                    //Copy font bytes from the stream to unmanaged memory
                    Marshal.Copy(buffer, 0, memPtr, buffer.Length);
     
                    //Add the font to the FontCollection, allowing it to be used by HTMLLabel controls
                    Gobicode.Win.HTMLLabel.FontCollection.AddMemoryFont(memPtr, buffer.Length);            
     
                    //Free the memory allocated previously.
                    Marshal.FreeHGlobal(memPtr);
                }
            }
        }
    }