dcsimg
Results 1 to 1 of 1

Thread: [.NET 3.5+] StyledString (Multi-Colored And Multi-Font GDI+ Strings)

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    [.NET 3.5+] StyledString (Multi-Colored And Multi-Font GDI+ Strings)

    This class enables you to draw GDI+ strings that can basically be anything:

    Name:  StyledString.png
Views: 661
Size:  31.4 KB

    To produce the above example, you would use the following code:

    Code:
    Public Class Form1
    
        '//our class level variable
        Private styledString As StyledString
    
        '//initialize our string here
        Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
            MyBase.OnLoad(e)
    
            '//string will automatically clean up these fonts when
            '//it's Dispose() method is called
            Dim title = New Font("Calibri", 16.75!)
            Dim bolded = New Font(MyBase.Font, FontStyle.Bold)
            Dim boldedAndUnderlined = New Font(MyBase.Font, FontStyle.Bold Or _
                                                            FontStyle.Underline)
    
            '//set the default font and colors for this class which means that
            '//if you add characters to the string without specifying a color or font
            '//it will assign this font instead
            Me.styledString = New StyledString(MyBase.Font, MyBase.ForeColor)
    
            '//functionality is similar to a StringBuilder
            With Me.styledString
    
                '//build title
                .Append("Demonstration of the ", title)
    
                '//string will automatically dispose and clean this font
                .Append("StyledString", New Font(title, FontStyle.Italic Or _
                                                        FontStyle.Underline Or _
                                                        FontStyle.Bold), Color.Blue)
                .AppendLine(" class:", title)
                .AppendLine()
    
                '//build content
                .Append("Red, ", bolded, Color.Red)
                .Append("Orange, ", bolded, Color.Orange)
                .Append("Yellow, ", bolded, Color.Yellow)
                .Append("Green, ", bolded, Color.Green)
                .Append("Blue, ", bolded, Color.Blue)
                .Append("Indigo, ", bolded, Color.Indigo)
                .Append("Violet", bolded, Color.Violet)
                .Append(" ...any color ")
                .Append("YOU", boldedAndUnderlined)
                .AppendLine(" want")
                .AppendLine("in one string! Plus multiple font families and font styles")
                .AppendLine("can be mixed into one string.")
                Dim colors = New Color() {Color.Red, Color.Orange, Color.Yellow, _
                                          Color.Green, Color.Blue, Color.Indigo, _
                                          Color.Violet}
    
                '//string will automatically dispose and clean this font
                .Append("ROYGBIV", New Font("Viner Hand ITC", 32.0!, FontStyle.Bold Or _
                                                                     FontStyle.Underline))
    
                '//apply colors to the string starting at the specified index to the end
                '//of the color collection
                .ApplyColors(colors, .IndexOf("ROYGBIV"))
                .AppendLine()
                .Append("Note: ", bolded)
                .AppendLine("This entire thing is one huge string and it needs to draw")
                .AppendLine("each character individually. And because of that speed will")
                .AppendLine("suffer some.")
            End With
        End Sub
    
        Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
            MyBase.OnPaint(e)
    
            '//draw the string to the canvas
            Me.styledString.Draw(e.Graphics, 10, 10)
        End Sub
    
        Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
            MyBase.OnFormClosed(e)
    
            '//need to dispose this class properly
            Me.styledString.Dispose()
            Me.styledString = Nothing
        End Sub
    
    End Class
    
    The code is fully documented, and is very similar to a StringBuilder. I may one day make a Control wrapper around this so it can be placed on a designer.
    Attached Files Attached Files
    Last edited by ForumAccount; Jun 25th, 2011 at 09:19 PM. Reason: Applied different styling

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width