我创建一个用户控件并添加文本大小属性来了。 我需要知道的文字大小是多大将是绘制它的控制之前,这样我可以相对于其高度和宽度居中上的控制。
我假定这是一个Windows API指挥。 不过,我一直没能找到它的任何信息。
Public Sub DrawImage(ByVal gr As Graphics)
If Me.Image Is Nothing Then Return
Dim r As Rectangle = New Rectangle(8, 8, Me.ImageSize.Width, Me.ImageSize.Height)
Select Case Me.ImageAlign
Case ContentAlignment.TopLeft
r = New Rectangle(4, 4, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.TopCenter
r = New Rectangle((Me.Width / 2 - Me.ImageSize.Width / 2) / 2, 4, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.TopRight
r = New Rectangle(Me.Width - 4 - Me.ImageSize.Width, 4, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleLeft
r = New Rectangle(8, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleCenter
r = New Rectangle(Me.Width / 2 - Me.ImageSize.Width / 2, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.MiddleRight
r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, Me.Height / 2 - Me.ImageSize.Height / 2, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomLeft
r = New Rectangle(8, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomCenter
r = New Rectangle(Me.Width / 2 - Me.ImageSize.Width / 2, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
Case ContentAlignment.BottomRight
r = New Rectangle(Me.Width - 8 - Me.ImageSize.Width, Me.Height - 8 - Me.ImageSize.Height, Me.ImageSize.Width, Me.ImageSize.Height)
End Select
gr.DrawString("Header", New Font("MS SANS SERIF", 8.25, FontStyle.Regular, GraphicsUnit.Point, 1, True), Brushes.Black, 0, 0)
'gr.DrawImage(Me.Image, r)
End Sub
正如你所看到@ gr.DrawString,我需要知道文字有多大预测是让我能规模的控制和中心控制中的文本。