Results 1 to 11 of 11

Thread: [RESOLVED] Two colour text ?

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,424

    Resolved [RESOLVED] Two colour text ?

    Hi,

    Is it possible to create a two colour text ?

    Example:
    Name:  Like This.png
Views: 260
Size:  5.4 KB


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  2. #2

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,301

    Re: Two colour text ?

    This may be easier. I haven't looked closely at either though:

    https://www.codeguru.com/cpp/g-m/gdi...ext-Part-1.htm

  4. #4

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,424

    Re: Two colour text ?

    Quote Originally Posted by jmcilhinney View Post
    This may be easier. I haven't looked closely at either though:

    https://www.codeguru.com/cpp/g-m/gdi...ext-Part-1.htm
    I couldn't work out how to use either of these, especially the one in C++.

    I found something else which suggested the basis for this:
    Code:
        Public Sub TwoTone()
            Dim grp As Graphics = Me.CreateGraphics
            Dim gp As New Drawing2D.GraphicsPath
            Dim useFont As Font = New Font("Times New Roman", 100, FontStyle.Regular)
            grp.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
            grp.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            gp.AddString("sample text", useFont.FontFamily, FontStyle.Regular, 100, _
                                  New Point(0, 0), StringFormat.GenericTypographic)
            grp.DrawPath(Pens.Red, gp)
            Label1.Font = useFont
            useFont.Dispose()
            gp.Dispose()
        End Sub
    It does change the font to Times New Roman and to 100px text height but not any part of the colour.
    I don't really understand how it's trying to outline the text, but I've expected at least two Pens, but I thought it might use the Label's ForeColor as the other.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Two colour text ?

    I just looked at jmc's first link, and they had a lot of examples.
    I looked at the "Drawing Single Outline Text with Generic GDI+" example, and yes they had a C++ example first, but it was immediately followed by a C# version on that same page.

    I just copied the C# code and pasted it into a new Form's paint event. Did a global search and replace (Ctrl-H) removal of the semicolons, and change the C# variable declarations to VB variable declarations. Added the Intelisense suggest Import of System.DRawing.Drawing2D.
    I didn't see the need to create the initial White Brush, since you already have Brushes avialable in all the standard colors, so dropped that. And a few other minor things.

    Bottom line, about 12 minutes of work gave a VB version of the C# example.

    See it if is something like what you're looking for.
    Code:
    Imports System.Drawing.Drawing2D
    
    Public Class Form1
    
      Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
        e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
    
        e.Graphics.FillRectangle(Brushes.White, 0, 0, ClientSize.Width, ClientSize.Height)
     
        Dim myFontFamily As FontFamily = New FontFamily("Arial")
        Dim strformat As New StringFormat
    
        Dim szbuf As String = "Text Designer"
    
        Dim path As New GraphicsPath()
    
        path.AddString(szbuf, myFontFamily, FontStyle.Regular, 48.0F, New Point(10, 10), strformat)
        Using myPen = New Pen(Color.Black, 3)
          e.Graphics.DrawPath(myPen, path)
          e.Graphics.FillPath(Brushes.Yellow, path)
    
          myFontFamily.Dispose()
          path.Dispose()
        End Using
    
      End Sub
    End Class
    Last edited by passel; Jun 2nd, 2020 at 09:27 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,301

    Re: Two colour text ?

    Based on passel's code, I defined this class:
    vb.net Code:
    1. Imports System.Drawing.Drawing2D
    2.  
    3. Public Class BorderedText
    4.  
    5.     Public Property Text As String
    6.     Public Property TextColor As Color
    7.     Public Property BorderColor As Color
    8.     Public Property BorderWidth As Single
    9.     Public Property Font As Font
    10.  
    11.     Public Sub Draw(g As Graphics, location As Point)
    12.         Dim originalSmoothingMode = g.SmoothingMode
    13.         Dim originalInterpolationMode = g.InterpolationMode
    14.  
    15.         g.SmoothingMode = SmoothingMode.AntiAlias
    16.         g.InterpolationMode = InterpolationMode.HighQualityBicubic
    17.  
    18.         Using path As New GraphicsPath,
    19.               textBrush As New SolidBrush(TextColor),
    20.               borderPen As New Pen(BorderColor, BorderWidth)
    21.             path.AddString(Text, Font.FontFamily, Font.Style, Font.Size, location, New StringFormat)
    22.             g.DrawPath(borderPen, path)
    23.             g.FillPath(textBrush, path)
    24.         End Using
    25.  
    26.         g.SmoothingMode = originalSmoothingMode
    27.         g.InterpolationMode = originalInterpolationMode
    28.     End Sub
    29.  
    30. End Class
    I then tested it like this:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private helloText As New BorderedText With {.Text = "Hello", .TextColor = Color.White, .BorderColor = Color.Red, .BorderWidth = 5.0F, .Font = New Font(Font.FontFamily, 50.0F, Font.Style)}
    4.     Private goodbyeText As New BorderedText With {.Text = "Goodbye", .TextColor = Color.Green, .BorderColor = Color.Black, .BorderWidth = 2.0F, .Font = New Font(Font.FontFamily, 25.0F, Font.Style)}
    5.  
    6.     Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint
    7.         helloText.Draw(e.Graphics, New Point(10, 10))
    8.         goodbyeText.Draw(e.Graphics, New Point(100, 150))
    9.     End Sub
    10.  
    11. End Class
    Last edited by jmcilhinney; Jun 2nd, 2020 at 10:24 PM. Reason: Fixed output quality by adding missing Graphics property assignments and used Style and Size of Font.

  7. #7
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: Two colour text ?

    Code:
    Dim grp As Graphics = Me.CreateGraphics
            Dim gp As New Drawing2D.GraphicsPath
            Dim useFont As Font = New Font("Times New Roman", 100, FontStyle.Regular)
            grp.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
            grp.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            gp.AddString("sample text", useFont.FontFamily, FontStyle.Regular, 100, New Point(0, 0), StringFormat.GenericTypographic)
            useFont.Dispose()
            
            grp.FillPath(Brushes.White, gp)
            If CheckBox1.Checked Then
                grp.DrawPath(Pens.Black, gp)
            End If
            gp.Dispose()

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,424

    Re: Two colour text ?

    Thanks Passel, I was struggling with that until John's post.
    Quote Originally Posted by jmcilhinney View Post
    Based on passel's code, I defined this class:
    I then tested it like this:
    Thanks John, That's brilliant. Based on your post I've got my code working pretty much as I wanted.

    'Pretty much'... Is there a way to measure the length of the string in pixels ?
    Code:
        Public Function Gap(ByVal msg As String) As Int32
            Dim spac As Int32
            Using gr As Graphics = Me.CreateGraphics
                spac = CInt(gr.MeasureString(msg, Me.Font).Width)
            End Using
            Return (Me.Width - spac)' \ 2
        End Function
    I tried this but of course it only measures the string in the form's font, not as modified by your method. I've tried substituting various converted variables for the string 'msg' but mostly intellisense doesn't like what I tried, or the conversion doesn't give the actual string.
    I'm trying to calculate the Location.X to place the string central on the form.


    Poppa

    PS:
    I guess it's not really a string is it? What is it... a drawing ?

    Pop
    Last edited by Poppa Mintin; Jun 3rd, 2020 at 09:02 AM. Reason: PS added
    Along with the sunshine there has to be a little rain sometime.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,301

    Re: Two colour text ?

    You would need to create a new Font with the same family, style and size as the one you're going to use to draw the text. If you're using my code then that will be the same Font that you pass to the BorderedText.Font property.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,301

    Re: Two colour text ?

    Quote Originally Posted by Poppa Mintin View Post
    I'm trying to calculate the Location.X to place the string central on the form.
    In that case, you don't need to do any calculations at all. Just change the code to specify a Rectangle to draw the text in instead of using a Point and configure the StringFormat object to centre the text both horizontally and vertically. If you use the whole form area, the text will be centred in the form. Here's an updated BorderText class:
    vb.net Code:
    1. Imports System.Drawing.Drawing2D
    2.  
    3. Public Class BorderedText
    4.  
    5.     Public Property Text As String
    6.     Public Property TextColor As Color
    7.     Public Property BorderColor As Color
    8.     Public Property BorderWidth As Single
    9.     Public Property Font As Font
    10.  
    11.     Public Sub Draw(g As Graphics, location As Point)
    12.         Dim originalSmoothingMode = g.SmoothingMode
    13.         Dim originalInterpolationMode = g.InterpolationMode
    14.  
    15.         g.SmoothingMode = SmoothingMode.AntiAlias
    16.         g.InterpolationMode = InterpolationMode.HighQualityBicubic
    17.  
    18.         Using path As New GraphicsPath,
    19.             textBrush As New SolidBrush(TextColor),
    20.             borderPen As New Pen(BorderColor, BorderWidth)
    21.             path.AddString(Text, Font.FontFamily, Font.Style, Font.Size, location, New StringFormat)
    22.             g.DrawPath(borderPen, path)
    23.             g.FillPath(textBrush, path)
    24.         End Using
    25.  
    26.         g.SmoothingMode = originalSmoothingMode
    27.         g.InterpolationMode = originalInterpolationMode
    28.     End Sub
    29.  
    30.     Public Sub Draw(g As Graphics, layoutRect As Rectangle)
    31.         Dim originalSmoothingMode = g.SmoothingMode
    32.         Dim originalInterpolationMode = g.InterpolationMode
    33.  
    34.         g.SmoothingMode = SmoothingMode.AntiAlias
    35.         g.InterpolationMode = InterpolationMode.HighQualityBicubic
    36.  
    37.         Using path As New GraphicsPath,
    38.             textBrush As New SolidBrush(TextColor),
    39.             borderPen As New Pen(BorderColor, BorderWidth)
    40.             path.AddString(Text,
    41.                            Font.FontFamily,
    42.                            Font.Style,
    43.                            Font.Size,
    44.                            layoutRect,
    45.                            New StringFormat With {.Alignment = StringAlignment.Center,
    46.                                                   .LineAlignment = StringAlignment.Center})
    47.             g.DrawPath(borderPen, path)
    48.             g.FillPath(textBrush, path)
    49.         End Using
    50.  
    51.         g.SmoothingMode = originalSmoothingMode
    52.         g.InterpolationMode = originalInterpolationMode
    53.     End Sub
    54.  
    55. End Class
    That could be refactored to be DRY but I've gone quick and dirty here. I then tested that with this code:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private bt As New BorderedText With {.Text = "Hello World",
    4.                                          .TextColor = Color.White,
    5.                                          .BorderColor = Color.Red,
    6.                                          .BorderWidth = 5.0F,
    7.                                          .Font = New Font(Font.FontFamily, 50.0F, Font.Style)}
    8.     Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    9.         bt.Draw(e.Graphics, ClientRectangle)
    10.     End Sub
    11.  
    12.     Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd
    13.         'Redraw the text after a resize.
    14.         Invalidate()
    15.     End Sub
    16.  
    17. End Class
    That will draw the text in the centre of the form and it will move it if you resize the form.

  11. #11

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,424

    Re: Two colour text ?

    Thanks again John,

    I didn't notice that the form's text was set to 100pt, not 100px !

    All brilliant now.

    Name:  Wednesday 600.jpg
Views: 107
Size:  39.7 KB

    Poppa.
    Last edited by Poppa Mintin; Jun 3rd, 2020 at 09:52 AM.
    Along with the sunshine there has to be a little rain sometime.

Posting Permissions

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



Click Here to Expand Forum to Full Width