Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
If MilitaryFormat Then
CurrentTime = System.DateTime.Now.ToString("HH:mm:ss")
Else
CurrentTime = System.DateTime.Now.ToString("hh:mm:ss t")
End If
' Get the target area.
Dim target As New Rectangle(20, 20, Me.ClientSize.Width - 45, Me.ClientSize.Height - (DayLabel.Height + 30))
' Make the text in a GraphicsPath
' at approximately the desired height.
Dim the_font As New Font("Arial", _
target.Height, FontStyle.Bold, GraphicsUnit.Pixel)
' Make the StringFormat.
Dim sf As New StringFormat
sf.Alignment = StringAlignment.Center
sf.LineAlignment = StringAlignment.Center
' Add the text to the GraphicsPath.
Dim text_path As New GraphicsPath
text_path.AddString(CurrentTime, _
the_font.FontFamily, CInt(FontStyle.Bold), _
target.Height, New PointF(0, 0), sf)
' Make a transformation to map
' (xmin, ymin)-(xmax, ymax) onto
' the target area.
Dim text_rectf As RectangleF = text_path.GetBounds()
Dim target_pts() As PointF = { _
New PointF(target.Left, target.Top), _
New PointF(target.Right, target.Top), _
New PointF(target.Left, target.Bottom) _
}
e.Graphics.Transform = New Matrix(text_rectf, target_pts)
' Draw the text.
e.Graphics.Clear(Me.BackColor)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
e.Graphics.FillPath(Brushes.White, text_path)
e.Graphics.DrawPath(Pens.White, text_path)
' Reset the transform.
e.Graphics.ResetTransform()
' Draw the bounding rectangle.
'e.Graphics.DrawRectangle(Pens.Red, target)
' Cleanup.
text_path.Dispose()
sf.Dispose()
the_font.Dispose()
End Sub