Visual Basic 6
![]()
Visual Studio 2019 + XAML(VB.Net or C#)
![]()
Less typing. Works better.
*mic drop*
Code:
Private Type vbRECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type TEXTRUN
TextString As String
FontSize As Long
TextColor As Long
'Note: THIS FIELD IS NOT MEANT TO BE WRITTEN TO BY THE
'PROGRAMMER
CALCULATEDRECT As vbRECT
End Type
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As vbRECT, ByVal wFormat As Long) As Long
Private Declare Function DrawFocusRect Lib "user32" (ByVal hDc As Long, lpRect As vbRECT) As Long
Private Const DT_SINGLELINE = &H20
Private Const DT_CALCRECT = &H400
Private Const DT_CENTER = &H1
Private Const DT_VCENTER = &H4
Private Const DT_BOTTOM = &H8
Private Sub Form_Paint()
Dim text(0 To 2) As TEXTRUN
text(0).FontSize = 40
text(0).TextColor = RGB(255, 0, 0)
text(0).TextString = "Red"
text(1).FontSize = 25
text(1).TextColor = RGB(0, 0, 0)
text(1).TextString = " and "
text(2).FontSize = 40
text(2).TextColor = RGB(0, 0, 255)
text(2).TextString = "Blue"
DrawTextCentered text
End Sub
Private Sub Form_Resize()
Me.Refresh
End Sub
Private Sub DrawTextCentered(text() As TEXTRUN)
Dim previousScaleMode As Long
Dim prevFontSize As Long
Dim prevForeColor As Long
Dim textHeight As Long
Dim textWidth As Long
Dim textLeft As Long
Dim textTop As Long
Dim r As vbRECT
previousScaleMode = Me.ScaleMode
prevFontSize = Me.Font.Size
prevForeColor = Me.ForeColor
'We are assuming that when DrawText called on the Form's DC
'that the MM_TEXT mapping mode is used. MM_MODE is basically saying
'we are working in pixels. Because of this we need our
'Form to be measuring in pixels so we change the ScaleMode otherwise
'the only way to be sure is to import more of the Win32 API to measure
'in pixels.
Me.ScaleMode = vbPixels
For i = LBound(text) To UBound(text)
Me.Font.Size = text(i).FontSize
Me.ForeColor = text(i).TextColor
DrawText Me.hDc, text(i).TextString, -1, text(i).CALCULATEDRECT, DT_CALCRECT
If text(i).CALCULATEDRECT.Bottom > textHeight Then textHeight = text(i).CALCULATEDRECT.Bottom
textWidth = textWidth + text(i).CALCULATEDRECT.Right
Next
textLeft = (Me.ScaleWidth \ 2) - (textWidth \ 2)
textTop = (Me.ScaleHeight \ 2) - (textHeight \ 2)
For i = LBound(text) To UBound(text)
Me.Font.Size = text(i).FontSize
Me.ForeColor = text(i).TextColor
r.Left = textLeft
r.Top = textTop
r.Right = r.Left + text(i).CALCULATEDRECT.Right
r.Bottom = textTop + textHeight
DrawText Me.hDc, text(i).TextString, -1, r, DT_SINGLELINE Or DT_BOTTOM
'DrawFocusRect Me.hDc, r
textLeft = textLeft + text(i).CALCULATEDRECT.Right
Next
Me.ScaleMode = previousScaleMode
Me.Font.Size = prevFontSize
Me.ForeColor = prevForeColor
End Sub

Visual Studio 2019 + XAML(VB.Net or C#)
Code:
<Grid>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" >
<Run FontSize="40" Foreground="Red">Red</Run>
<Run FontSize="20"> and </Run>
<Run FontSize="40" Foreground="Blue">Blue</Run>
</TextBlock>
</Grid>

Less typing. Works better.
*mic drop*