-
Jun 2nd, 2020, 07:55 AM
#1
Thread Starter
PowerPoster
[RESOLVED] Two colour text ?
Hi,
Is it possible to create a two colour text ?
Example:
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Jun 2nd, 2020, 07:58 AM
#2
-
Jun 2nd, 2020, 08:01 AM
#3
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
-
Jun 2nd, 2020, 07:16 PM
#4
Thread Starter
PowerPoster
Re: Two colour text ?
Originally Posted by jmcilhinney
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.
-
Jun 2nd, 2020, 09:17 PM
#5
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
-
Jun 2nd, 2020, 10:11 PM
#6
Re: Two colour text ?
Based on passel's code, I defined this class:
vb.net Code:
Imports System.Drawing.Drawing2D Public Class BorderedText Public Property Text As String Public Property TextColor As Color Public Property BorderColor As Color Public Property BorderWidth As Single Public Property Font As Font Public Sub Draw(g As Graphics, location As Point) Dim originalSmoothingMode = g.SmoothingMode Dim originalInterpolationMode = g.InterpolationMode g.SmoothingMode = SmoothingMode.AntiAlias g.InterpolationMode = InterpolationMode.HighQualityBicubic Using path As New GraphicsPath, textBrush As New SolidBrush(TextColor), borderPen As New Pen(BorderColor, BorderWidth) path.AddString(Text, Font.FontFamily, Font.Style, Font.Size, location, New StringFormat) g.DrawPath(borderPen, path) g.FillPath(textBrush, path) End Using g.SmoothingMode = originalSmoothingMode g.InterpolationMode = originalInterpolationMode End Sub End Class
I then tested it like this:
vb.net Code:
Public Class Form1 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)} 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)} Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles MyBase.Paint helloText.Draw(e.Graphics, New Point(10, 10)) goodbyeText.Draw(e.Graphics, New Point(100, 150)) End Sub 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.
-
Jun 3rd, 2020, 06:31 AM
#7
Lively Member
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()
-
Jun 3rd, 2020, 08:37 AM
#8
Thread Starter
PowerPoster
Re: Two colour text ?
Thanks Passel, I was struggling with that until John's post.
Originally Posted by jmcilhinney
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.
-
Jun 3rd, 2020, 08:55 AM
#9
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.
-
Jun 3rd, 2020, 09:06 AM
#10
Re: Two colour text ?
Originally Posted by Poppa Mintin
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:
Imports System.Drawing.Drawing2D Public Class BorderedText Public Property Text As String Public Property TextColor As Color Public Property BorderColor As Color Public Property BorderWidth As Single Public Property Font As Font Public Sub Draw(g As Graphics, location As Point) Dim originalSmoothingMode = g.SmoothingMode Dim originalInterpolationMode = g.InterpolationMode g.SmoothingMode = SmoothingMode.AntiAlias g.InterpolationMode = InterpolationMode.HighQualityBicubic Using path As New GraphicsPath, textBrush As New SolidBrush(TextColor), borderPen As New Pen(BorderColor, BorderWidth) path.AddString(Text, Font.FontFamily, Font.Style, Font.Size, location, New StringFormat) g.DrawPath(borderPen, path) g.FillPath(textBrush, path) End Using g.SmoothingMode = originalSmoothingMode g.InterpolationMode = originalInterpolationMode End Sub Public Sub Draw(g As Graphics, layoutRect As Rectangle) Dim originalSmoothingMode = g.SmoothingMode Dim originalInterpolationMode = g.InterpolationMode g.SmoothingMode = SmoothingMode.AntiAlias g.InterpolationMode = InterpolationMode.HighQualityBicubic Using path As New GraphicsPath, textBrush As New SolidBrush(TextColor), borderPen As New Pen(BorderColor, BorderWidth) path.AddString(Text, Font.FontFamily, Font.Style, Font.Size, layoutRect, New StringFormat With {.Alignment = StringAlignment.Center, .LineAlignment = StringAlignment.Center}) g.DrawPath(borderPen, path) g.FillPath(textBrush, path) End Using g.SmoothingMode = originalSmoothingMode g.InterpolationMode = originalInterpolationMode End Sub 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:
Public Class Form1 Private bt As New BorderedText With {.Text = "Hello World", .TextColor = Color.White, .BorderColor = Color.Red, .BorderWidth = 5.0F, .Font = New Font(Font.FontFamily, 50.0F, Font.Style)} Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint bt.Draw(e.Graphics, ClientRectangle) End Sub Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles Me.ResizeEnd 'Redraw the text after a resize. Invalidate() End Sub End Class
That will draw the text in the centre of the form and it will move it if you resize the form.
-
Jun 3rd, 2020, 09:39 AM
#11
Thread Starter
PowerPoster
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|