I've noticed that Drawing.Graphics.TextRenderingHint = ClearTypeGridFit does
not seem to be used when StringFormat.FormatFlags = DirectionVertical. This
is best seen by running the following example:
Public Class Form1
Inherits Windows.Forms.Form
Protected Overrides Sub OnSizeChanged(ByVal e As System.EventArgs)
MyBase.OnSizeChanged(e)
Me.Invalidate()
End Sub
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Const text As String = "VWXYZ"
Dim color As Drawing.Color = color.Black
Dim bounds1 As New Drawing.Rectangle(0, 0, (Me.ClientSize.Width \ 3),
Me.ClientSize.Height)
Dim bounds2 As New Drawing.Rectangle(bounds1.Right, 0, bounds1.Width,
Me.ClientSize.Height)
Dim bounds3 As New Drawing.Rectangle(bounds2.Right, 0, bounds1.Width,
Me.ClientSize.Height)
Dim font As New Drawing.Font("Arial", 12, Drawing.FontStyle.Regular,
Drawing.GraphicsUnit.Point) 'Use a font that supports ClearType.
'Technique #1: Draw directly.
Using brush As New Drawing.SolidBrush(color)
Dim stringFormat As New Drawing.StringFormat()
stringFormat.Alignment = Drawing.StringAlignment.Center
stringFormat.LineAlignment = Drawing.StringAlignment.Center
stringFormat.FormatFlags = Drawing.StringFormatFlags.DirectionVertical
Dim temp As Drawing.Text.TextRenderingHint =
e.Graphics.TextRenderingHint
e.Graphics.TextRenderingHint =
Drawing.Text.TextRenderingHint.ClearTypeGridFit 'Make sure that we are using
ClearType.
e.Graphics.DrawString(text, font, brush, bounds1, stringFormat)
e.Graphics.TextRenderingHint = temp
End Using
'Technique #2: Draw to an image, rotate the image and then draw the image.
Dim imgBounds2 As New Drawing.Rectangle(0, 0, bounds2.Height,
bounds2.Width)
Using img As New Drawing.Bitmap(imgBounds2.Width, imgBounds2.Height,
Drawing.Imaging.PixelFormat.Format32bppArgb)
Using gImg As Drawing.Graphics = Drawing.Graphics.FromImage(img)
gImg.TextRenderingHint =
Drawing.Text.TextRenderingHint.ClearTypeGridFit 'Make sure that we are using
ClearType.
'gImg.Clear(Me.BackColor)
Using brush As New Drawing.SolidBrush(color)
Dim stringFormat As New Drawing.StringFormat()
stringFormat.Alignment = Drawing.StringAlignment.Center
stringFormat.LineAlignment = Drawing.StringAlignment.Center
gImg.DrawString(text, font, brush, imgBounds2, stringFormat)
End Using
End Using
img.RotateFlip(Drawing.RotateFlipType.Rotate90FlipNone)
e.Graphics.DrawImageUnscaled(img, bounds2.Location)
End Using
'Technique #3: Create an image of the background, draw to the image,
rotate the image and then draw the image.
'NOTE: The problem with this technique is that we often don't know what
the background is when the text is drawn.
Dim imgBounds3 As New Drawing.Rectangle(0, 0, bounds3.Height,
bounds3.Width)
Using img As New Drawing.Bitmap(imgBounds3.Width, imgBounds3.Height,
Drawing.Imaging.PixelFormat.Format32bppArgb)
Using gImg As Drawing.Graphics = Drawing.Graphics.FromImage(img)
gImg.TextRenderingHint =
Drawing.Text.TextRenderingHint.ClearTypeGridFit 'Make sure that we are using
ClearType.
gImg.Clear(Me.BackColor) 'This effectively draws the background.
Using brush As New Drawing.SolidBrush(color)
Dim stringFormat As New Drawing.StringFormat()
stringFormat.Alignment = Drawing.StringAlignment.Center
stringFormat.LineAlignment = Drawing.StringAlignment.Center
gImg.DrawString(text, font, brush, imgBounds3, stringFormat)
End Using
End Using
img.RotateFlip(Drawing.RotateFlipType.Rotate90FlipNone)
e.Graphics.DrawImageUnscaled(img, bounds3.Location)
End Using
End Sub
End Class
When you show the form you will see three vertically drawn strings. The
left string is drawn using Technique1 which is the technique that I want, but
ClearType does not seem to be used. The middle string is drawn using
Technique2 which is to draw horizontally to a bitmap and then rotate the
bitmap. Technique2 would also be acceptable except that the resulting text
looks poor. The right string is drawn using Technique3 which is the same as
Technique2 except that the background of the control is drawn to the bitmap
first. The text looks good when Technique3 is used, but the technique
requires us to know what the background should look like which is often
difficult or impossible to determine.
A good example of when it would be difficult to implement Technique3 is if
we inherit from Windows.Forms.ToolStripProfessionalRenderer in order to
custom draw menu items. That class provides the OnRenderMenuItemBackground
and OnRenderItemText for drawing the background and text, respectively.
Drawing text in OnRenderMenuItemBackground would be difficult because the
TextRectangle is not specified to that method. On the otherhand, drawing the
background in OnRenderItemText might cause problems because there is the
chance that we would draw over the image (if OnRenderItemText is called after
OnRenderItemImage).
So, what I am looking for is a way to have vertically drawn text use
ClearType without any knowledge of what the background should look like.
Thanks for any help!
Lance